Jump to content

Recommended Posts

Trying to get my first upload form running and running in to a little bit of trouble  ???

 

<?php


if (isset($_POST['submit'])) {



if (move_uploaded_file ($_FILES['thefile']['tmp_name'], "/temp/{$_FILES['thefile']['name']}")) {

	echo "it worked";

} else {

	echo "it didn't work: <br /><br />";

	switch ($_FILES['thefile']['error']) {
		case 1:
					echo "upload_max_filesize";
					break;

		case 2:
					echo "MAX_FILE_SIZE";
					break;

		case 3:
					echo "partially uploaded";
					break;

		case 4:
					echo "no upload at all";
					break;
	}


}



}


echo "

<form action=\"upload.php\" enctype=\"multipart/form-date\" method=\"post\">

<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"30000\" />

<input type=\"file\" name=\"thefile\" />

<input type=\"submit\" name=\"submit\" value=\"Upload\" />

</form>

";




?>

 

 

When I try to upload a file, all that gets echoed is: it didn't work

So I am not sure what to do really :( Thanks for any help in advance

Okay the form-data was the first problem, having date was a simple typo. Now I am getting error message for case 2 back. I changed my hidden field to 300000 instead of 30000, and still get the error. Is this number the problem or something else? File size is like 1.04 MB

 

 

edit: I tried a file that was like 124KB and it just returns a "it didn't work" with no case error.

The value is in bytes only. It is not possible to use K, M, or G -

 

The MAX_FILE_SIZE hidden field (measured in bytes) must precede the file input field, and its value is the maximum filesize accepted by PHP. Fooling this setting on the browser side is quite easy, so never rely on files with a greater size being blocked by this feature. The PHP settings for maximum-size, however, cannot be fooled. This form element should always be used as it saves users the trouble of waiting for a big file being transferred only to find that it was too big and the transfer failed.

My file upload.php is in my main html directory, also in my main html directory is a folder "temp". I set it to 777 so that it was fully writable. When I say case 2, in my code case 2 goes to the MAX_FILE_SIZE, which is easily fixable, I'll just set that higher. Though now that I don't get that error, I'm just getting "it didn't work: " and no case error. I'm not sure why?

You need to check for upload errors first before you attempt to do a move_uploaded_file() and you need to check for all possible error values or you need a default case statement that at least tells you what error number occurred.

 

Add the following two lines after your <?php tag to see any php errors (such as the /temp problem that BlueSkyIS suggested) -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

Okay, got that in:

 

Warning: move_uploaded_file(/temp/car.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/virtual/site21/fst/var/www/html/upload.php on line 9

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpxkGAWr' to '/temp/car.jpg' in /home/virtual/site21/fst/var/www/html/upload.php on line 9

 

Is it saying my file name is wrong or that the directory doesn't exist? The route to my html directory is /var/www/html/ and there is a folder called temp in it.

 

Should it be:

if (move_uploaded_file ($_FILES['thefile']['tmp_name'], "/temp/{$_FILES['thefile']['name']}")) {

changed to:

if (move_uploaded_file ($_FILES['thefile']['tmp_name'], "/home/virtual/site21/fst/var/www/html/temp/{$_FILES['thefile']['name']}")) {

 

 

edit: made the change that I just asked about above and it fixed the problem, image uploaded with ease :D Thanks for the troubleshooting help!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.