Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a form to upload a file:

HTML
<form enctype="multipart/form-data" method="post" action="pphoto.php">
Upload Photo: <input name="photo" type="file" required> By: <input name="by" type="text" required>
</form>


These is the action of the form:
PHP
<?php

if($_FILES['photo']['name'])
{
	//if no errors...
	if(!$_FILES['photo']['error'])
	{
		//now is the time to modify the future file name and validate the file
		$new_file_name = strtolower($_FILES['photo']['name']); //rename file
		if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
		{
			$valid_file = false;
			$message = 'Oops!  Your file\'s size is to large.';
		}else { $valid_file = true; }
		
		//if the file has passed the test
		if($valid_file)
		{
			//move it to where we want it to be
			move_uploaded_file($_FILES['photo']['name'], './images/'.$new_file_name);
			$message = 'Congratulations!  Your file was accepted.';
		}
	}
	//if there is an error...
	else
	{
		//set that to be the returned message
		$message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
	}
}
echo $message;

$myFile = "photos.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "<img src=\"../photos/". $_FILES['photo']['name'] ." \">";
fwrite($fh, $stringData);
fclose($fh);

?>

What happens is it uploads than checks file size and for errors, if all is good it moves it to the images directory. When i upload an image nothing appears in the image directory.

Why is this happenning?
Posted
Comments
Sergey Alexandrovich Kryukov 5-Nov-13 20:14pm    
Can you debug it or use debug output? Does your execution even reach the line "move_unloaded_file"? Do you see "Congratulation! ..."?
—SA
G4mm4R4y 5-Nov-13 20:16pm    
Im not sure i have anything to debug with. Im limited to wamp server, notepad++, and dreamweaver which may be able to debug but im not sure how to work dreamweaver.
Sergey Alexandrovich Kryukov 5-Nov-13 20:23pm    
I would advise to debug everything locally first. Install some HTTP server (better be Apache), PHP, some IDE with a debugger. This all does not weight much. If you work blindfolded, it’s much harder to develop. Instead of finding the fix you need, stop what you are doing and setup a good working environment for you. Don’t rush: slower means getting final results sooner.
—SA
G4mm4R4y 5-Nov-13 20:29pm    
Oh if an http server with php is your means of a way of debugging thats already up, i tested it and it gave the message "Congratulations! Your file was accepted." But the file doesnt appear in my local web directory or its following subfolders. Dreamweaver reported no code errors.
Sergey Alexandrovich Kryukov 5-Nov-13 20:45pm    
The missing link is: how about debugging?..
And yes, check up relative path './images' . $new_file_name.., output it all...
—SA

1 solution

When a file is uploaded to the server, it will be temporarily stored in a PHP temp folder on the server. This temporary file disappears when the script ends. To store the uploaded file we need to copy it to a different location. The name of this temporary copy is : $_FILES['photo']['tmp_name']

Hope the following will helps.

C#
//move_uploaded_file($_FILES['photo']['name'], './images/'.$new_file_name);

move_uploaded_file($_FILES['photo']['tmp_name'], './images/'.$new_file_name);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900