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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

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

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply