Display image from PHP script

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
Mo
Forum Newbie
Posts: 21
Joined: Thu Nov 06, 2003 7:21 am

Display image from PHP script

Post by Mo »

The following script stores the image filenames into an array from a given directory.

Code: Select all

<?php
$allowedExtensions = array('.jpg', '.jpeg', '.png', '.bmp'); 
// $dir = "images/gage/";
$dh = opendir($dir);
$i=1;

while (($filename = readdir($dh)) !== false) {
    if (is_file($dir.$filename)) {
           $extension = strrchr($filename, '.');
           if (in_array($extension, $allowedExtensions)) {
                $image[$i]=$filename;
                $i++;
           }
    }
}
closedir($dh); 
?>
I would like to include() this script in another file and display the images in a table format.

This is where I am stuck
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Go along this line:

this.php

Code: Select all

<?php
$allowedExtensions = array('.jpg', '.jpeg', '.png', '.bmp');
// $dir = "images/gage/";
$dh = opendir($dir);
$i=1;

while (($filename = readdir($dh)) !== false) {
    if (is_file($dir.$filename)) {
           $extension = strrchr($filename, '.');
           if (in_array($extension, $allowedExtensions)) {
                $image[]=$filename;
                $i++;
           }
    }
}
closedir($dh);
?>
that.php

Code: Select all

<?php
include('this.php');
echo <<< START
<table cellpadding="0" cellspacing="0" border="0">
START;
   foreach($images as $image) {
      echo <<< CELL
      <tr>
         <td>
            <img src="$image" alt="" />
         </td>
      </tr>
CELL;
   }
echo <<< END
</table>
END;
?>
Hope it helps,

-Nay
Mo
Forum Newbie
Posts: 21
Joined: Thu Nov 06, 2003 7:21 am

Post by Mo »

Nay,
Thanks for your help but no, it did not work. I got the following error:

Parse error: parse error, expecting `','' or `';'' in /home/babejor/public_html/test2.php on line 10

Line 10 is the line with the following code:

Code: Select all

echo <<< START
User avatar
SteveO
Forum Newbie
Posts: 12
Joined: Wed Nov 19, 2003 2:27 am

Post by SteveO »

did you remember to close the line where the include is?
include(this.php); <--- ?
Mo
Forum Newbie
Posts: 21
Joined: Thu Nov 06, 2003 7:21 am

Post by Mo »

C'mon, I am a newbie, but I know that much. Yes, I did.

What does this mean?

Code: Select all

echo <<< START
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Mo wrote:C'mon, I am a newbie, but I know that much. Yes, I did.

What does this mean?

Code: Select all

echo <<< START
it's heredoc, look at Nay's signature and check out the link in it.
From what i know heredoc is used when you want to for example echo something, and you dont want to use quotes or anything inside it,
heredoc starts with "echo <<<", and then you can use any string, like "END". Until PHP finds the first "END;", it will go on echoing everything between.

Now, im not sure if you understand what i wrote, since I only know the basics of heredoc...

Example:

Code: Select all

<?php

echo <<< END //start
<HTML>
<BODY>
<a href="somepage.php">Link</a><br>
Hello $name!<br>
</BODY>
</HTML>
END; //End

?>
Mo
Forum Newbie
Posts: 21
Joined: Thu Nov 06, 2003 7:21 am

Post by Mo »

viggie89,
Yes I understand. Thank you. I clicked on the link from Nay's responce. It says that it is extremely important that there are no spaces or other characters before or after the <<<END statement.

It finally worked.

Thanks to all!
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

glad to help, and also, it's vigge, not viggie :P
Mo
Forum Newbie
Posts: 21
Joined: Thu Nov 06, 2003 7:21 am

Post by Mo »

vigge89,
my apologies
Post Reply