Page 1 of 1
Display image from PHP script
Posted: Thu Nov 20, 2003 11:51 am
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
Posted: Thu Nov 20, 2003 11:56 am
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
Posted: Thu Nov 20, 2003 12:23 pm
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:
Posted: Thu Nov 20, 2003 12:38 pm
by SteveO
did you remember to close the line where the include is?
include(this.php); <--- ?
Posted: Thu Nov 20, 2003 12:47 pm
by Mo
C'mon, I am a newbie, but I know that much. Yes, I did.
What does this mean?
Posted: Fri Nov 21, 2003 2:01 am
by vigge89
Mo wrote:C'mon, I am a newbie, but I know that much. Yes, I did.
What does this mean?
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
?>
Posted: Fri Nov 21, 2003 9:50 am
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!
Posted: Fri Nov 21, 2003 10:12 am
by vigge89
glad to help, and also, it's vigge, not viggie

Posted: Fri Nov 21, 2003 9:33 pm
by Mo
vigge89,
my apologies