elgato454 Posted November 2, 2014 Share Posted November 2, 2014 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 More sharing options...
elgato454 Posted November 6, 2014 Author Share Posted November 6, 2014 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 More sharing options...
Larry Posted November 6, 2014 Share Posted November 6, 2014 Could you provide a bit more details? For example, what are the URLs involved (especially the one giving the error)? What's your server setup? Link to comment Share on other sites More sharing options...
elgato454 Posted November 6, 2014 Author Share Posted November 6, 2014 This is the directory in which the pictures are in: C:\xampp\htdocs\phpmysql\php-work\ch11\uploads Of course, I've changed the directory three times and I'm still getting the error message instead of the pictures I uploaded. Link to comment Share on other sites More sharing options...
Larry Posted November 7, 2014 Share Posted November 7, 2014 Okay. What are the URLs involved and what is the web root? Link to comment Share on other sites More sharing options...
elgato454 Posted November 11, 2014 Author Share Posted November 11, 2014 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 More sharing options...
Larry Posted November 12, 2014 Share Posted November 12, 2014 That does help, but how about the URLs? The http:// whatever that shows in the browser's address bar. Link to comment Share on other sites More sharing options...
elgato454 Posted November 12, 2014 Author Share Posted November 12, 2014 For Script 11.2, the URL is this: http://localhost/phpmysql/php-work/ch11/upload-image.php For Script 11.3, the URL is: http://localhost/phpmysql/php-work/ch11/function.js For Script 11.4, this is the URL: http://localhost/phpmysql/php-work/ch11/images.php Hope that helps. Link to comment Share on other sites More sharing options...
elgato454 Posted November 18, 2014 Author Share Posted November 18, 2014 I went and reconfigured the httpd.conf file as per Appendix A. I still got nowhere. Keep in mind that I am EXTREMELY new to this. I probably forgot to mention this in my earlier posts. Link to comment Share on other sites More sharing options...
Larry Posted November 21, 2014 Share Posted November 21, 2014 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 More sharing options...
elgato454 Posted November 23, 2014 Author Share Posted November 23, 2014 So I managed to follow the steps outlined in that last post and came up with this URL: http://localhost/phpmysql/php-work/ch11/show_image.php?image=unavailable.png Link to comment Share on other sites More sharing options...
Larry Posted November 24, 2014 Share Posted November 24, 2014 To be clear, that's the URL that's being loaded in the browser when you're getting that error message? Link to comment Share on other sites More sharing options...
elgato454 Posted November 25, 2014 Author Share Posted November 25, 2014 Yes, that is correct. Link to comment Share on other sites More sharing options...
Larry Posted November 27, 2014 Share Posted November 27, 2014 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 More sharing options...
elgato454 Posted November 28, 2014 Author Share Posted November 28, 2014 I can confirm that I have a C:\xampp\htdocs\phpmysql\php-work\ch11\uploads\unavailable.png file. I don't have a show_image.php file, however. Maybe that's where we're going wrong. Link to comment Share on other sites More sharing options...
Larry Posted November 30, 2014 Share Posted November 30, 2014 Yes, the show_image.php omission is probably the problem. Link to comment Share on other sites More sharing options...
elgato454 Posted December 15, 2014 Author Share Posted December 15, 2014 After working on the show_image.php code, I got everything working. LESSON LEARNED: Finish each chapter BEFORE testing your code. Period. Link to comment Share on other sites More sharing options...
Larry Posted December 15, 2014 Share Posted December 15, 2014 Awesome. Glad to hear it's working! Link to comment Share on other sites More sharing options...
Recommended Posts