Page 1 of 1

Newbie Help, take pitty please. (getimagesize + readdir)

Posted: Mon Oct 03, 2005 2:08 pm
by Blyant
Below is my first Php effort, I know its full of errors and I hope someone can help. I want to print out through PHP an XML file to read into my Flash document. I want to get the file names in a specific directory, which I have done successfully using "readdir". I'm trying to use unsuccessfully "getimagesize" in the same "while" loop to return the width of the images.

Code: Select all

<?php
echo "<?xml version=\"1.0\" encoding =\"ISO-8859-1\" ?>\n";
echo "<images>\n";
if ($handle = opendir('images')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
			$width = getimagesize($file);
			echo "<myImage Path=\"images/" . "$file\"" . " Width=\"" . "$width\"" . " />\n";
       }
   }
   closedir($handle);
}
echo "</images>\n"

?>
My Xml file produced from the Php script, has this error included for each file in the directory and the width value is blank.

Code: Select all

<br />
<b>Warning</b>:  getimagesize(1.jpg): failed to open stream: No such file or directory in <b>/home/1/k/kjellman/Temp/imageList.php</b> on line <b>7</b><br />
<myImage Path="images/1.jpg" Width="" />
:cry: :cry:

Posted: Mon Oct 03, 2005 2:34 pm
by Blyant
I don't know if this is pretty code..... but I got it to work. Please post up if there is a better way to use/ write this. Any help much appreciated!

Code: Select all

<?php
echo "<?xml version=\"1.0\" encoding =\"ISO-8859-1\" ?>\n";
echo "<images>\n";
if ($handle = opendir('images')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
			list($width, $height) = getimagesize("images/".$file);
			echo "<myImage Path=\"images/" . "$file\"" . " Width=\"" . "$width\"" . " Height=\"". "$height\"" ." />\n";
       }
   }
   closedir($handle);
}
echo "</images>\n"

?>