Jump to content
Larry Ullman's Book Forums

Scripts 11.3 - 11.5 Return Empty Image Files


Recommended Posts

OS: Windows 7

php version: 5.5.8 (running on EasyPHP Dev Server 14.1 VC11)

Browser: Firefox 40.0.2  (same results in Chrome and IE)

 

I am having problems with the show_image script in chapter 11.

 

When I select an image from the list it opens a popup window of the appropriate size but with no image. In firefox I get the error message: 'The image "http://127.0.0.1/scripts/site/show_image.php?image=Picture.png"cannot be displayed because it contains errors.'

 

When I use the Web Developer menu to View Response Headers all is as expected except that it lists Content-Length: 0. I commented out the header calls and replaced them with echos:

 

echo "Content-Type: {$info['mime']}</br>";
echo "Content-Disposition: inline; filename=\"$name\"</br>";
echo "Content-Length: $fs";

 

Which return the values you would expect:

 

Content-Type: image/png
Content-Disposition: inline; filename="Picture.png"
Content-Length: 35880

 

I changed the Content-Disposition to attachment. The files download as normal with the appropriate file names but have a size of 0. When I try to open them I get the error message: "Windows Photo Viewer can't display this picture because the file is empty." 

 

I double checked the files in the upload folder and they are all normal .png files.

 

My code is below.

 

Any advice you can offer would be very gratefully received.

 

Adam

 

show_image.php

 

<?php

$name = FALSE;
if (isset($_GET['image'])) {
//echo 'Image set </br>';
$ext = strtolower(substr($_GET['image'], -4));
if (($ext == '.jpg') OR ($ext == 'jpeg') OR ($ext == '.png')) {
//echo 'Correct ext </br>';
$image = "../uploads/{$_GET['image']}";
if (file_exists($image) && (is_file($image))) {
//echo 'File exists </br>';
$name = $_GET['image'];
}
}
}
if (!$name) {
//echo 'Unavailable';
$image = 'images/unavailable.png';
$name = 'unavailable.png';
}
$info = getimagesize($image);
$fs = filesize($image);
 
header ("Content-Type: {$info['mime']}\n");
header ("Content-Disposition: inline; filename=\"$name\"\n");
header ("Content-Length: $fs");
 
//echo "Content-Type: {$info['mime']}</br>";
//echo "Content-Disposition: inline; filename=\"$name\"</br>";
//echo "Content-Length: $fs";
 

images.php

 

<!DOCTYPE html PUBLIC "-//W3C// DTD XHTML 1.0 Transitional//EN" "http:/www.w3.org/TR/xhtml/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="en" lang="en"
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Images</title>
<script type="text/javascript" charset="utf-8" src="js/function.js"></script>
</head>
<body>
<p>Click on an image to view it in a separate window.</p>
<ul>
<?php # Script 11.4 - images.php
$dir = '../uploads';
$files = scandir($dir);
foreach ($files as $image) {
if (substr($image, 0 , 1) != '.') {
$image_size = getimagesize ("$dir/$image");
$image_name = urlencode($image);
echo "<li><a href=\"javascript:create_window('$image_name',$image_size[0],$image_size[1])\">$image</a></li>\n";
}
}
?>
</ul>
</body>

 

</html>

 

function.js

 

// Script 11.3 - function.js
 
function create_window (image, width, height) {
width = width + 10;
height = height + 10;
 
if (window.popup && !window.popup.closed) {
window.popup.resizeTo(width, height);
}
var specs = "location=no, scrollbars=no, menubars=no, toolbars=no, resizeable=yes, left=0, top=0, width=" + width + ", height=" + height;
 
var url = "show_image.php?image=" + image;
 
popup = window.open(url, "ImageWindow", specs);
popup.focus();
 

 

}
Link to comment
Share on other sites

By way of follow-up, I just moved on to 11.6 and now also get the following error message when I run Images.php:

 

  • Warning: filesize(): stat failed for ../uploads/image in C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\scripts\Sept16\site\images.php on line 19
  • Agenda.png 0kb (26 October, 2016 17:16:53)

I guess the two issues might be connected?

Link to comment
Share on other sites

  • 3 weeks later...
  • 4 weeks later...
I've the same issue with this code show_image.php
The image in the uploads folder is fine but show_image shows error.
The image "http://localhost/sch...e=acc_page.png"cannot be displayed because it contains errors. (firefox error message, small square box displayed in chrome) 
I then downloaded the image file and opened it but shows error on opening it.
I opened the image in text editor (notepad++) and this is what i found - 
The difference between the two image is the image type name at the beginning - the one with the error begins with empty space (about a -tab)
(" ‰PNG" vs "‰PNG")
Using the text editor i removed the beginning space of the image with the error and the image was back to normal.
Any thoughts where the code might be wrong?
Here is the code:
<?php
# Script 11.5 - show_image.php
// this page displays an image

// flag variable
$name = FALSE;

// check for an image name in the URL
if (isset($_GET['image']))
{
// make sure it has an image's extensions:
$ext = strtolower (substr($_GET['image'], -4));

if (($ext == '.jpg') OR ($ext == 'jpeg') OR ($ext == '.png'))
{
// full image path
$image = "../../../uploads/{$_GET['image']}";

// check that the image exists and is a file
if (file_exists($image) && (is_file($image)))
{
// set the name as this image:
$name = $_GET['image'];
}
// end of file_exist() IF
}
// end of $ext IF
}
// end of isset($_GET['image']) IF

// if there was a problem use the default image
if(!$name)
{
$image = 'images/unavailable.jpg';
$name = 'unavailable.jpg';
}

// get the image information:
$info = getimagesize($image);
$fs = filesize($image);

// send the content information
header ("Content-Type: {$info['mime']}\n");
header ("Content-Disposition: inline; filename=\"$name\"\n");
header ("Content-Length: $fs\n");

// send the file:
readfile ($image);

I don't see anything amiss in your code. The problem may be upon upload. Does this happen with every image?

Link to comment
Share on other sites

  • 2 weeks later...

Hi, phpadawan

Just checking in to see if you've solved the issue. The above post from admin (#4) is actually from me. (Sorry I had to do that, had issues with replying).

Anyway I solved after about 3-4 weeks when I revisited my code. I had this habit of starting the php opening tag after a tab (4 spaces on my system) in the text editor. But now I've learned (the hard way, despite the many warnings in the book) that, that was the cause of the underlying issue. Removed the indentation, and problem solved.

You might not have made the same error as mine but it's worth a shot.

 

Thanks alot Larry Ullman for your great book.

Edited by alex_r
Link to comment
Share on other sites

 Share

×
×
  • Create New...