Hi all I need to be able to have a page with an input box that i can past a list of picture names. On hitting submit the code runs through the list and writes the list as links to a file checking to see if it already exists. Is that easy to do? Any help appreciated.
What did you mean saying "write the list of picture names as links"? How does a picture name become a link?
sorry...i should have been more specific. I mean that in the input box i can list have a list for example: 1.jpg 2.gif 3.jpeg etc etc...It would loop through list and write to sitemap.html (for example) a list like this: <a href="http://mysite/foldername/1.jpg">1.jpg</a> <a href="http://mysite/foldername/2.gif">2.gif</a> ...etc Its basically a sitemap generator for any list . Hope that makes sense.
I think it is just that what you want: <html> <head> <title>Links To File</title> </head> <body> <?php if(isset($_POST["submit"])){ $filename = "sitemap.html"; $href = '<a href="http://mysite/foldername/%s">%s</a><br />'; $list = explode("\n",$_POST["list"]); $handle = fopen($filename,"a+"); if(filesize($filename)>0){ if($handle){ while(!feof($handle)){ $line = fgets($handle,filesize($filename)); for($i=0; $i<count($list); $i++){ if(sprintf($href,trim($list[$i]),trim($list[$i]))==trim($line)){ unset($list[$i]); } } } } } $string = ""; foreach($list as $link){ $string .= "\n".sprintf($href,trim($link),trim($link)); } fwrite($handle,$string); fclose($handle); } ?> <form method="post"> <div><textarea name="list" id="list" cols="30" rows="10"></textarea></div> <div><input type="submit" name="submit" value="Submit"></div> </form> </body> </html> Code (markup):
I forgot to write how to use it. Just enter in the textfield the picture names (each picture name in the new line) and submit that.
@s_ruben You don't need so much code <html> <head> <title>Links To File</title> </head> <body> <?php if(isset($_POST['submit']) && !empty($_POST['list'])){ $list = str_replace(array("\r\n", "\r"), "\n", strip_tags($_POST['list'])); $list = explode("\n", $list); foreach($list as $picturename) { $picturename = preg_replace("/($picturename*)/", "<a href=\"http://mysite/foldername/$1\">$1</a>", $picturename); ob_start(); echo $picturename."\n"; $list = ob_get_contents(); } ob_end_clean(); file_put_contents("sitemap.html", $list); } ?> <form method="post"> <div><textarea name="list" id="list" cols="30" rows="10"></textarea></div> <div><input type="submit" name="submit" value="Submit"></div> </form> </body> </html> PHP:
Woops, I need to start testing my code!. This should work: <html> <head> <title>Links To File</title> </head> <body> <?php if(isset($_POST['submit']) && !empty($_POST['list'])){ $list = str_replace(array("\r\n", "\r"), "\n", strip_tags($_POST['list'])); $list = explode("\n", $list); foreach($list as $picturename){ if(!stristr(file_get_contents('sitemap.html'), $picturename)){ $picturename = preg_replace("/($picturename*)/", "<a href=\"http://mysite/foldername/$1\">$1</a>", $picturename); $list = $picturename."\n"; $fp = fopen('sitemap.html', 'a'); fwrite($fp, $list); fclose($fp); } } } ?> <form method="post"> <div><textarea name="list" id="list" cols="30" rows="10"></textarea></div> <div><input type="submit" name="submit" value="Submit"></div> </form> </body> </html> PHP:
@danx10 File cannot contain the same links. If you enter the same link it must not be written in the file again.
Maybe, but the OP didn't provide this info, however I've modified the post to prevent this from happening, thanks
Thank you both for your help ! Got it working and very much appreciate. Rep added...although danx10 you helped me on something else recently so it wont let me rep you again so soon !