Hello,

I am starting to learn PHP & have been doing an exercise where I display all the images from a folder. All the image boxes are displaying but with a question mark in each (ie no image) and the warning:

Warning: filesize() [function.filesize]: stat failed for photosDir/filename.jpg in C:\xampp\htdocs[COLOR="Red"]mydir[/COLOR]\gallery.php on line 66
0 bytes

The code is as follows and line 66 is the line:

<?php echo round(filesize($photosList[$x])) . ' bytes'; ?>

I have tried resizing the images but to no avail. Any help much apreciated, thanks

Col

<?php
//define location of images
$photosDir = './images';

//definewhich file extensions are images
$photosExt = array('gif', 'jpg', 'jpeg', 'tif', 'tiff', 'bmp', 'png');

//initialize array to hold filenames of images found
$photosList = array();

//read directory contents
//build photo list
if (file_exists($photosDir)) {
	$dp = opendir($photosDir) or die ('ERROR: cannot open directory');
	while ($file= readdir($dp)) {
		if ($file != '.' && $file != '..') {
			$fileData = pathinfo($file);
			if (in_array($fileData['extension'], $photosExt)) {
				$photosList[] = "photosDir/$file";
			}
		}
	}
	closedir($dp);
} else {
	die ('Error: Directory does not exist');
}
//iterate over photo list
//diaplay ecah image and filename
if (count($photosList) > 0) {
	for ($x=0; $x<count($photosList); $x++) {
		?>

    <li>
    <img height="150" width="200"
    src="<?php echo $photosList[$x]; ?>" />
    <?php echo basename($photosList[$x]); ?><br />
    <?php echo round(filesize($photosList[$x])) . ' bytes'; ?>



    </li>
    <?php
}
} else {
	die('error: no images found in directory');
}
?>
</ul>

    Looks like this line:

    $photosList[] = "photosDir/$file";

    should be:

    $photosList[] = "$photosDir/$file";

      brilliant, thanks so much anakadote.

      note to self: check then check again for typos before wasting people's time on forums.

      much appreciated

      Col

        Sure, and it's not a waste of time. Everybody needs another set of eyes once in a while. Don't forget to mark this thread as resolved under "Thread Tools".

          Write a Reply...