bschultz Posted June 9, 2010 Share Posted June 9, 2010 I found a photo slideshow script at http://www.zinkwazi.com/wp/scripts/. If I want photo descriptions, I have to create a text file with the following format: greg.jpg;Me dog.png;My dog John cat; tux.jpg;My friend Tux I would like to create that text file on the fly. I can read all image files in the directory with this... <?php $dir = opendir ("/my/directory/here"); while (false !== ($file = readdir($dir))) { if (strpos($file, '.gif',1)||strpos($file, '.jpg',1)||strpos($file, '.JPG',1) ) { echo "$file <br />"; } } ?> I also know how to write to a text file...but how can I take the file name garnered from the above code...and create a form with an editable description that can be added? I tried echo "$file[0] <br />"; echo "$file[1] <br />"; But it echoed this... s c s c b l Thanks! Quote Link to comment Share on other sites More sharing options...
bschultz Posted June 9, 2010 Author Share Posted June 9, 2010 I should add that eventually, I'll need to add file uploads (which isn't hard)...and adding to / editing the text file once new pics are added. So, when you load the "edit" file for photo descriptions, it will need to keep the value that's already added for that file name. Quote Link to comment Share on other sites More sharing options...
dabaR Posted June 9, 2010 Share Posted June 9, 2010 Hm...I'm working on a similar thing. I decided to not provide any of the features you are building. I would be interested in seeing the code once you are done. To make a form, you can't possibly echo $file[0]. $file is a string, $file[0] means first character of $file. You have to echo <form....><input type="text"... /></form> Am I missing something in your question, or are you really asking about basics of HTML forms? Quote Link to comment Share on other sites More sharing options...
bschultz Posted June 9, 2010 Author Share Posted June 9, 2010 I didn't word that very well. I need to load the file names into an array...and then echo the result in the various form fields using the array value. I used <?php $dir = opendir ("/my/directory/here"); while (false !== ($file = readdir($dir))) { if (strpos($file, '.gif',1)||strpos($file, '.jpg',1)||strpos($file, '.JPG',1) ) { echo "$file[0] <br />"; ////// this is where I used this code...not in the html form echo "$file[1] <br />"; ////// this is where I used this code...not in the html form } } ?> to test this...and it didn't work. Quote Link to comment Share on other sites More sharing options...
dabaR Posted June 9, 2010 Share Posted June 9, 2010 Oh OK. <?php $dir = opendir ("/my/directory/here"); $files = array(); while (false !== ($file = readdir($dir))) { if (strpos($file, '.gif',1)||strpos($file, '.jpg',1)||strpos($file, '.JPG',1) ) { $files[] = $file; } } foreach ($files as $file_name) { echo $file; } ?> Quote Link to comment Share on other sites More sharing options...
bschultz Posted June 9, 2010 Author Share Posted June 9, 2010 While that printed the directory names...it didn't put them in an array that could be used in the form field's <?php $dir = opendir ("."); //code changed to use current directory $files = array(); while (false !== ($file = readdir($dir))) { if (strpos($file, '.gif',1)||strpos($file, '.jpg',1)||strpos($file, '.JPG',1) ) { $files[] = $file; } } foreach ($files as $file_name) { echo $file[0]; echo "<br />"; echo $file[1]; } //should this be $file_name instead of $file? It didn't work either, but you've never used the foreach value. ?> Quote Link to comment Share on other sites More sharing options...
dabaR Posted June 9, 2010 Share Posted June 9, 2010 Yes, I meant to use $file_name there. Well, what kind of an array can be used in the form field's? <-- grammatically incorrect, BTW. Quote Link to comment Share on other sites More sharing options...
bschultz Posted June 9, 2010 Author Share Posted June 9, 2010 I need $file_names[0] to be the first file name found...$file_names[1] to be the second...$file_names[2] to be the third...etc. The code above printed a blank page when I added the array [#]'s in the code... Quote Link to comment Share on other sites More sharing options...
dabaR Posted June 9, 2010 Share Posted June 9, 2010 <?php $dir = opendir ("."); //code changed to use current directory $file_names = array(); while (false !== ($file = readdir($dir))) { if (strpos($file, '.gif',1)||strpos($file, '.jpg',1)||strpos($file, '.JPG',1) ) { $file_names[] = $file; } } ?> Quote Link to comment Share on other sites More sharing options...
bschultz Posted June 9, 2010 Author Share Posted June 9, 2010 I'm starting to get mad...!!! This <?php $dir = opendir ("."); //code changed to use current directory $file_names = array(); while (false !== ($file = readdir($dir))) { if (strpos($file, '.gif',1)||strpos($file, '.jpg',1)||strpos($file, '.JPG',1) ) { $file_names[] = $file; } echo $file_names[0]; echo "<br />"; echo $file_names[1]; } ?> displayed this screened product for sale.JPG screened product for sale.JPG screening black dirt.JPGscreened product for sale.JPG screening black dirt.JPGscreened product for sale.JPG screening black dirt.JPGscreened product for sale.JPG screening black dirt.JPG Quote Link to comment Share on other sites More sharing options...
dabaR Posted June 9, 2010 Share Posted June 9, 2010 Well, you probably want the echo $file_names[0]; echo "<br />"; echo $file_names[1]; outside of the while loop to start. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 9, 2010 Share Posted June 9, 2010 Why are people still using the opendir & readdir loops to get an array of files in a directory when the glob function does it for you. <?php $file_names = glob('./*.{jpg,JPG,gif}',GLOB_BRACE); echo '<pre>' . print_r($file_names,true) . '</pre>'; ?> Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.