PHP Picture Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelli321
Forum Newbie
Posts: 7
Joined: Fri Jul 29, 2011 11:18 pm

PHP Picture Error

Post by michaelli321 »

I am running everything on localhost.

I made a page where you can upload pictures onto a folder on my computer called uploads. I tested that page and everything worked out fine, but when I try to make a table with links to the pictures that opens in a pop-up window, I get an error saying the requested URL was not found on the server.

Here are my two codes:

for uploading:

Code: Select all

<!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=iso-8859-1" />
	<title>Upload an Image</title>
	<style type="text/css" title="text/css" media="all">
	.error {
		font-weight: bold;
		color: #C00
	}
	</style>
</head>
<body>
<?php # Script 10.3 - upload_image.php

// Check if the form has been submitted:
if (isset($_POST['submitted'])) {

	// 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'], "C:/xampp/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_filesize 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 occurred.';
				break;
		} // End of switch.
		
		print '</strong></p>';
		
	} // End of error IF.
	
	// Delete the file if it still exists:
	if (file_exists ($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name']) ) {
		unlink ($_FILES['upload']['tmp_name']);
	}
			
} // End of the submitted conditional.
?>
	
<form enctype="multipart/form-data" action="upload_image.php" method="post">

	<input type="hidden" name="MAX_FILE_SIZE" value="524288">
	
	<fieldset><legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded:</legend>
	
	<p><b>File:</b> <input type="file" name="upload" /></p>
	
	</fieldset>
	<div align="center"><input type="submit" name="submit" value="Submit" /></div>
	<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
for the table:

Code: Select all

<!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=iso-8859-1" />
	<title>Images</title>
	<script language="JavaScript">
	<!-- // Hide from old browsers.
	
	// 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.
	//--></script>
</head>
<body>
<p>Click on an image to view it in a separate window.</p>
<table align="center" cellspacing="5" cellpadding="5" border="1">
	<tr>
		<td align="center"><b>Image Name</b></td>
		<td align="center"><b>Image Size</b></td>
	</tr>
<?php # Script 10.4 - images.php
// This script lists the images in the uploads directory.

$dir = 'C:/xampp/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");
		
		// Calculate the image's size in kilobytes:
		$file_size = round ( (filesize ("$dir/$image")) / 1024) . "kb";
		
		// Make the image's name URL-safe:
		$image = urlencode($image);
		
		// Print the information:
		echo "\t<tr>
\t\t<td><a href=\"javascript:create_window('$image',$image_size[0],$image_size[1])\">$image</a></td>
\t\t<td>$file_size</td>
\t</tr>\n";
	
	} // End of the IF.
    
} // End of the foreach loop.
?>
</table>
</body>
</html>
That last code written with both PHP and JavaScript.
This is what comes up when I click the links:
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.
Error 404
localhost
8/24/2011 3:51:44 PM
Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1

Any help?
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: PHP Picture Error

Post by twinedev »

Quick run through, the one thing that stands out to me is that the other locations where you use $image, you are also including $dir in front of it, so maybe you need it in the code that opens the actual image.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: PHP Picture Error

Post by danwguy »

I would think you would need to be saving them to C:\xampp\htdocs\uploads then when you call the images to load them in the window you would want to use a url, like "localhost/uploads/imagename.png" since it stored locally, but opened via the web o you need a url, not a dir
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: PHP Picture Error

Post by phphelpme »

danwguy wrote:use a url, like "localhost/uploads/imagename.png" since it stored locally
The directory would not be found as you are loading from a browser which would default to root as localhost. So the correct link would be uploads/imagename.ext

My guess would be its not your PHP thats the problem as that all works, its your javascript that does not get told where the file is to be opened.

Lets take your java out of the href for example:

Code: Select all

// Print the information:
                echo "<tr>
<td><a href=\"".$dir."/".$image."\">$image</a></td>
<td>$file_size</td>
</tr>";
Everything works just fine so to me its your javascript thats the issue here.

My advise would be to use css to open in popup instead of new window and scrap java altogether as its not needed for what you are trying to achieve. Plus if java is disabled then at least your images will still load. :)

Best wishes
michaelli321
Forum Newbie
Posts: 7
Joined: Fri Jul 29, 2011 11:18 pm

Re: PHP Picture Error

Post by michaelli321 »

phphelpme wrote:
danwguy wrote:use a url, like "localhost/uploads/imagename.png" since it stored locally
The directory would not be found as you are loading from a browser which would default to root as localhost. So the correct link would be uploads/imagename.ext

My guess would be its not your PHP thats the problem as that all works, its your javascript that does not get told where the file is to be opened.

Lets take your java out of the href for example:

Code: Select all

// Print the information:
                echo "<tr>
<td><a href=\"".$dir."/".$image."\">$image</a></td>
<td>$file_size</td>
</tr>";
Everything works just fine so to me its your javascript thats the issue here.

My advise would be to use css to open in popup instead of new window and scrap java altogether as its not needed for what you are trying to achieve. Plus if java is disabled then at least your images will still load. :)

Best wishes
I could do that, but I am learning PHP right now, and I am using a textbook. The textbook chapter I am on is using PHP and JavaScript, so it would have been pointless to do this chapter without using JavaScript.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: PHP Picture Error

Post by phphelpme »

I can understand that and following the book to the word is good practise.

May I ask what book you are completing?

Best wishes
michaelli321
Forum Newbie
Posts: 7
Joined: Fri Jul 29, 2011 11:18 pm

Re: PHP Picture Error

Post by michaelli321 »

phphelpme wrote:I can understand that and following the book to the word is good practise.

May I ask what book you are completing?

Best wishes
The book is PHP 6 and MySQL 5 by Larry Ullman
Post Reply