phil88 Posted February 18, 2011 Share Posted February 18, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/228052-uploading-files-error/ Share on other sites More sharing options...
DavidAM Posted February 18, 2011 Share Posted February 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228052-uploading-files-error/#findComment-1175977 Share on other sites More sharing options...
phil88 Posted February 18, 2011 Author Share Posted February 18, 2011 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 Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/228052-uploading-files-error/#findComment-1175989 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.