Jump to content

Uploading files error


phil88

Recommended Posts

Hey everyone,

 

I'm having an intermittent problem with my upload script. It works all the time I've tested it both locally and in its live environment, and it works nearly 100% of the time in its live environment during normal usage. I've coded it so it logs any errors as and when they happen, and the log tells me that move_uploaded_file fails so returns false and that $_FILES['Filedata']['tmp_name'] is empty. I think the empty tmp_name is causing move_uploaded_file to fail, but I can't work out the cause of that problem.

 

This is the code in question:

public function upload() {
if (!empty($_FILES)) {
    $uploadDir = 'uploads';
    $subDir = uniqid();
    $targetPath = $uploadDir . '/' . $subDir;
    $mkDir = mkdir($targetPath);
    $targetFile =  $targetPath . '/' . $_FILES['Filedata']['name'];
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $mvFile = move_uploaded_file($tempFile,$targetFile);
    if(!$mkDir || !$mvFile) {
	// Something went wrong
	$debug['FILES'] = $_FILES;
	$debug['mkdir'] = var_export($mkDir, true);
	$debug['mvFile'] = var_export($mvFile, true);
	Application_Models_Log::append($debug);
	echo '1';
    }else {
	echo $targetFile;
    }
}
    }

 

The server's error_log doesn't contain anything that happened at the time of the error and as I've mentioned, I've been unable to recreate the problem. Does anybody know why the tmp_name would be empty on rare occasions? Or am I going about this in the wrong way? What else could be the problem?

Link to comment
Share on other sites

What are the contents of the other elements of $_FILES when this happens? Specifically, "error" and "size"?

 

It could be that the uploaded file exceeded the server's allowed size or failed in some other way. The "error" element will give you an indication of why it failed (see the manual). You should always check the "error" element before trying to move an uploaded file.

if ( (!empty($_FILES)) and ($_FILES["error"] == UPLOAD_ERR_OK) ) {

If the "error" is zero and the "size" is NOT zero, I would check whether the mkdir() worked and the permissions are correct. This does not seem to be the problem since it "usually works" and tmp_name is empty, but I don't know what else could be wrong.

Link to comment
Share on other sites

Thanks for your reply (:

 

The error value was 1, which I didn't realise was more meaningful that a 'yes, there was an error' until I just read that page in the manual you linked to. It would seem the uploaded files were bigger than the php.ini will allow.

 

Causes of errors are always more obvious when someone else points them out :D

 

Thanks again!

Link to comment
Share on other sites

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.