Page 1 of 1

print images from (string) file paths?? (newb)

Posted: Thu Feb 25, 2010 3:40 pm
by MiniMonty
Hi all,

here's the scenario: (all working sweetly so far - question at the end ) !

A user uploads five pictures and makes comments on each picture (via a form).
The pictures are uploaded to a dir. The upload script also to writes a txt file with the filepaths
and the comments (comma delimited) like this:
members/1/images/course_pics/7.jpg,111111,members/1/images/course_pics/9.jpg,22222, (etc., etc,. etc.,)

I can print this onto a page as an array using this:

Code: Select all

 
$uploaded_stuff = file_get_contents("members/$id/images/course_pics/" . $course_num . ".txt"); 
$file_array = explode(",",$uploaded_stuff);
foreach($file_array as $coursework) { 
echo $coursework."<br />\n"; 
}
 
My question is, how do I use the array data to actually display the pictures and comments
rather than just printing the contents of the txt file ?

Best wishes
Monty

Re: print images from (string) file paths?? (newb)

Posted: Thu Feb 25, 2010 4:01 pm
by AbraCadaver
Ideally each image record would be on its own line:

Code: Select all

members/1/images/course_pics/7.jpg,111111
members/1/images/course_pics/9.jpg,22222
Then file() will read each line into an array. Then you can explode on the , to get the two parts. The only problem here is if the comments contain a , what then? You may want to use something else or use the PHP csv functions to read and write your file.

Code: Select all

$lines = file("members/" . $id . "/images/course_pics/" . $course_num . ".txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
 
foreach($lines as $line) {
   $fields = explode(",", $line);
   $path = $fields[0];
   $comment = $fields[1];
   //or
   //list($path, $comment) = $fields;
}