Jump to content
Larry Ullman's Book Forums

Script 11.4 - Error 404 Upon Opening New Window


Recommended Posts

Good Afternoon!

 

I'm having trouble executing Script 11.4 in Chapter 11.  The code for Script 11.2 reads:

<?php  #  Script 11.2 - upload-image.php
 
// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Check for an uploaded file:
if (isset($_FILES['upload'])) {
// Validate the type.  Should be JPEG or PNG.
$allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG','image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
if (in_array($_FILES['upload']['type'], $allowed)) {
// Move the file over.
if(move_uploaded_file($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name']}")) {
echo '<p><em>The file has been uploaded!</em></p>';
} // End of move IF.
else {
// Invalid type
echo '<p class="error">Please upload a JPEG or PNG image.</p>';
}
} // End of isset($_FILES['upload'] IF.
 
// Check for an error:
if ($_FILES['upload']['error'] > 0) {
echo '<p class="error">The file could not be uploaded because: <strong>';
 
// Print a message based upon the error.
switch($_FILES['upload']['error']) {
case 1:
print 'The file exceeds the upload_max_file size setting in php.ini.';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
break;
case 3:
print 'The file was only partially uploaded.';
break;
case 4:
print 'No file was uploaded.';
break;
case 6:
print 'No temporary folder was available.';
break;
case 7:
print 'Unable to write to the disk.';
break;
case 8:
print 'File upload stopped.';
break;
default:
print 'A system error occured.';
break;
} // End of error IF.
 
// Delete the file as if it still exists:
if (file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name']) )
{
uplink($_FILES['upload']['tmp_name']);
}
 
} // End of the submitted conditional
}
}
 
?>
 
This is my Script 11.3:
 
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 popup window:
popup = window.open(url, "ImageWindow", specs);
popup.focus();
}
 
Finally, this is Script 11.4:
 
<?php # Script 11.4 - images.php
// This script lists the images in the uploads directory.
 
$dir = '../uploads'; // Define the directory to view.
 
$files = scandir($dir); // Read all the images into an array.
 
// 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.
?>
 
The problem is, when I click on any of the image links in Script 11.4, it shows a "Error 404 - File Not Found" message instead of the picture itself.  It also says something about the directory being "wrong or outdated".  I have not been able to fix this error, regardless of what I do.
 
Could you give me any pointers as to what might be wrong?  Thanks!
 
 
Link to comment
Share on other sites

This is my error message:

 

Object not found!

 

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

 

If you think this is a server error, please contact the webmaster.

Link to comment
Share on other sites

The root folder is this:

 

C:\xampp\htdocs

 

Originally, I had my temporary files stored in the default folder; but, like I said, I set two temp file folders up inside my directory.  The first one is:

 

C:\xampp\htdocs\phpmysql\php-work\uploads

 

The second one is the one I have listed in the previous post:

 

C:\xampp\htdocs\phpmysql\php-work\ch11\uploads

 

I keep going back and forth between folders and, for some reason, I'm still not getting the picture to display.

 

Does this help?

Link to comment
Share on other sites

Okay, so the first thing I'll say is you should stop just randomly trying things (different folders and such). That's only going to confuse matters and when you do finally get it working, you won't know why. Much better to come up with a plan and pursue it until it works so you know *why* it works. 

 

Next, let's go with C:\xampp\htdocs\phpmysql\php-work\ch11\uploads as your images directory and stick with it. Can you confirm that there are files in it? (Using the file system.)

 

And, finally, can you provide an example URL that's giving an error (i.e., what URL is showing in the address bar when you see that "object not found" message)? You may need to enable the address bar if this is the popup window.

Link to comment
Share on other sites

Okay, then there are many problems going on. First, can you confirm that the file 

C:\xampp\htdocs\phpmysql\php-work\ch11\show_image.php

exists?

 

And you should have a C:\xampp\htdocs\phpmysql\php-work\ch11\uploads\unavailable.png file, too.

Link to comment
Share on other sites

  • 3 weeks later...
 Share

×
×
  • Create New...