Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi Larry,

 

I really enjoy your book, but I am having trouble viewing my image after clicking on it.  When I comment out the failsafe> unavailable image, I receive a block of code

 

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: image in C:\wamp\www\Website\show_image.php on line <i>38</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>250856</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\Website\show_image.php' bgcolor='#eeeeec'>..\show_image.php<b>:</b>0</td></tr>
</table></font>
<br />

<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: getimagesize(): Filename cannot be empty in C:\wamp\www\Website\show_image.php on line <i>38</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>250856</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\Website\show_image.php' bgcolor='#eeeeec'>..\show_image.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>251904</td><td bgcolor='#eeeeec'><a href='http://www.php.net/getimagesize' target='_new'>getimagesize</a>
(  )</td><td title='C:\wamp\www\Website\show_image.php' bgcolor='#eeeeec'>..\show_image.php<b>:</b>38</td></tr>
</table></font>
<br />

<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: image in C:\wamp\www\Website\show_image.php on line <i>39</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>250856</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\Website\show_image.php' bgcolor='#eeeeec'>..\show_image.php<b>:</b>0</td></tr>
</table></font>
<br />

<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: image in C:\wamp\www\Website\show_image.php on line <i>47</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>250856</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\Website\show_image.php' bgcolor='#eeeeec'>..\show_image.php<b>:</b>0</td></tr>
</table></font>
<br />

<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: readfile(): Filename cannot be empty in C:\wamp\www\Website\show_image.php on line <i>47</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>250856</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\Website\show_image.php' bgcolor='#eeeeec'>..\show_image.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0007</td><td bgcolor='#eeeeec' align='right'>252840</td><td bgcolor='#eeeeec'><a href='http://www.php.net/readfile' target='_new'>readfile</a>
(  )</td><td title='C:\wamp\www\Website\show_image.php' bgcolor='#eeeeec'>..\show_image.php<b>:</b>47</td></tr>
</table></font>

 

Here is my code:

 

images.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/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>
<!--Include the JavaScript file:-->
<script type="text/javascript" charset="utf-8" src="js/function.js"></script>
</head>

<body>
<p>Click on an Image to view in a seperate window:</p>

<ul>

    <?php #Script 11.4 - Images.php
        //This script lists the images in the uploads directory:
        
        $dir = '../uploads';  //Define the directory to pull the images to view
        
        $files = scandir($dir); //Read all of the images into the array, $files
        
        //Display each image caption as a link to the JavaScript function:
        foreach ($files as $image){
            
            if (substr($image, 0, 1) !='.'){ //Ignore anything starting with a period.
            
                //Get the image's size in pixels:
                $image_size = getimagesize("$dir/$image");
                
                //Make the image's name URL-safe:
                $image_name = urlencode($image);
                
                //Print the information
                echo "<li><a href=\"javascript:create_window('$image_name',$image_size[0],
                $image_size[1])\">$image</a></li>\n";
                } //End of the IF.
                
            }//End of the foreach loop
        ?>
        </ul>
        </body>
        </html>

 

function.js

 

// Script 11.3 - function.js

//Make a pop-up window function:
function create_window (image, width, height){

    //Add some pixels to the width and height:
    width= width + 10;
    height = height + 10;
    
    //If the window is already open, resize it to the new dimensions:
    if (window.popup && !window.popup.closed){
        window.popup.resizeTo(width, height);}
        
    //Set the window properties:
    var specs = "location=no, scrollbars= no, menubars=no, toolbars= no, resizable=yes,
        left=0, top=0, width =" + width +", height=" + height;
        
    //Set the URL:
        var url = "show_image.php?image=" + image;
        
    //Create the pop-up window:
        popup = window.open(url, "ImageWindow", specs);
        popup.focus();
        
        } //End of function

 

show_image.php

 

<?php #Script 11.5 - show_image.php
//This page displays an image.

$name= FALSE; //Flag Variable:

//Check for an image name in the URL:
if (isset($_GET['image'])) {

    //Make sure it has an image's extension:
    $ext = strtolower ( substr ($_GET['image'], -4));
    
    if (($ext == '.jpg') or ($ext == 'jpeg') or ($ext == '.png')or ($ext == '.JPG')
    or ($ext == '.JPEG')or ($ext == '.PNG')){
    
        //Check that the image is a file on the server (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_exists() 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.png';
        $name = 'unavailable.png';
        
        }
        
    //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);

 

 

Link to comment
Share on other sites

You revive an error message back. Make sure to read and understand it:

 

 

Notice: Undefined variable: image in C:\wamp\www\Website\show_image.php on line 38

 

That basically means that the variable $image is not defined where you are trying to use it's value at line 38 in the script show_image.php. As with pretty much all error messages in PHP, this one is also mostly self-explanatory. 

 

Without knowing your line numbers, I'm betting this is related to where you bind your GET variable "image" to the variable image. When you call this script, make sure you do it as show_image.php?image=image.jpg (a valid image value)

Link to comment
Share on other sites

Sorry, here is the code with numbers.

 

Show_image.php

<?php #Script 11.5 - show_image.php
//This page displays an image.

$name= FALSE; //Flag Variable:

//Check for an image name in the URL:
if (isset($_GET['image'])) {

	//Make sure it has an image's extension:
	$ext = strtolower ( substr ($_GET['image'], -4));
	
	if (($ext == '.jpg') or ($ext == '.jpeg') or ($ext == '.png')){
	
		//Check that the image is a file on the server (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_exists() 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.png';
		$name = 'unavailable.png';
		
		}
		
	//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);

images.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/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>
<!--Include the JavaScript file:-->
<script type="text/javascript" charset="utf-8" src="js/function.js"></script>
</head>

<body>
<p>Click on an Image to view in a seperate window:</p>

<ul>

	<?php #Script 11.4 - Images.php
		//This script lists the images in the uploads directory:
		
		$dir = '../uploads';  //Define the directory to pull the images to view
		
		$files = scandir($dir); //Read all of the images into the array, $files
		
		//Display each image caption as a link to the JavaScript function:
		foreach ($files as $image){
			
			if (substr($image, 0, 1) !='.'){ //Ignore anything starting with a period.
			
				//Get the image's size in pixels:
				$image_size = getimagesize("$dir/$image");
				
				//Make the image's name URL-safe:
				$image_name = urlencode($image);
				
				//Print the information 
				echo "<li><a href=\"javascript:create_window('$image_name',$image_size[0],
				$image_size[1])\">$image</a></li>\n";
				} //End of the IF.
				
			}//End of the foreach loop
		?>
		</ul>
		</body>
		</html>

Function.js

// Script 11.3 - function.js

//Make a pop-up window function:
function create_window (image, width, height){

	//Add some pixels to the width and height:
	width= width + 10;
	height = height + 10;
	
	//If the window is already open, resize it to the new dimensions:
	if (window.popup && !window.popup.closed){
		window.popup.resizeTo(width, height);}
		
	//Set the window properties:
	var specs = "location=no, scrollbars= no, menubars=no, toolbars= no, resizable=yes,
		left=0, top=0, width =" + width +", height=" + height;
		
	//Set the URL:
		var url = "show_image.php?image=" + image;
		
	//Create the pop-up window:
		popup = window.open(url, "ImageWindow", specs);
		popup.focus();
		
		} //End of function

It looks like the image is properly referenced in the show_image.php, but when I click on the link I do not get the pop-up.

Link to comment
Share on other sites

Aha. Keep sure you do your String concatenation correctly. The script is literally looking for a file with the name of '../uploads/{$_GET['image']}' right now. You must do one of the following:

 

1. Concatenate:

$image = '../uploads/' . $_GET['image'];

 

2. String with variable support (double quotes)

$image = "../uploads/{$_GET['image']}"; // Notice the double quotes

 

If there's not any other errors, that should work.

  • Upvote 2
Link to comment
Share on other sites

 Share

×
×
  • Create New...