tcnjdeluca Posted October 4, 2009 Share Posted October 4, 2009 Hello, I have a php upload file that seems to randomly create files with zero bytes. That is the file is on the server but it is empty. I can not recreate the issue in testing, but I can confirm it is happening with my users. Here is my script; -- any suggestions? /// The Upload file script/// if (isset($maxsize)){ $filename = $_FILES['uploadedfile']['name']; $pattern = '/^[a-zA-Z0-9_.-]{4,60}$/'; $valid = TRUE; $valid = $cp = checkLength($_FILES['uploadedfile']['name'], 1, 60); $em = preg_match($pattern, $filename); $valid = $valid && $em; if ($valid){ $newdir2 = $final_dir."/"; $newdir = "dropbox/".$classid."/".$studentid."/"; $target_path = $newdir2.$_FILES['uploadedfile']['name']; $target_path2 = $newdir.$_FILES['uploadedfile']['name']; $file_name = $_FILES['uploadedfile']['name']; if (file_exists($target_path)){ $error[] = "I am sorry, you allready have a file by that name uploaded."; $smarty->assign('error', $error);// Display Template $smarty->display('studrop.tpl'); exit;} //$target_path = $fullname ; if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { $up = true; } else{ $error[] = "There was an error uploading your file."; } Quote Link to comment https://forums.phpfreaks.com/topic/176482-solved-upload-script-intermittently-creates-files-with-zero-bytes/ Share on other sites More sharing options...
RussellReal Posted October 5, 2009 Share Posted October 5, 2009 how canyou confirm its happening to your users? #2 I don't see the point in the redundancy here: $filename = $_FILES['uploadedfile']['name']; $pattern = '/^[a-zA-Z0-9_.-]{4,60}$/'; $valid = TRUE; $valid = $cp = checkLength($_FILES['uploadedfile']['name'], 1, 60); $em = preg_match($pattern, $filename); $valid = $valid && $em; they both do the same stuff.. what you SHOULD do is this: $filename = $_FILES['uploadedfile']['name']; if (preg_match("/^[a-z0-9_.-]*?\.[a-z0-1]{1,4}$/i", $filename)) { // everything else } and honestly why re-declare $filename like 3 times? just use 1 and to be honest try this: <?php // The Upload file script/// if (isset($maxsize)) { $filename = $_FILES['uploadedfile']['name']; if (preg_match("/^[a-z0-9_.-]*?\.[a-z0-1]{1,4}$/i", $filename)) { $newdir2 = $final_dir."/"; $newdir = "dropbox/".$classid."/".$studentid."/"; $target_path = "$newdir2$filename"; $target_path2 = "$newdir$filename"; if (file_exists($target_path)){ $error[] = "I am sorry, you allready have a file by that name uploaded."; $smarty->assign('error', $error);// Display Template $smarty->display('studrop.tpl'); } else { if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { $up = true; } else{ $error[] = "There was an error uploading your file."; } } } } unlink($_FILES['uploadedfile']['tmp_name']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/176482-solved-upload-script-intermittently-creates-files-with-zero-bytes/#findComment-930352 Share on other sites More sharing options...
tcnjdeluca Posted October 5, 2009 Author Share Posted October 5, 2009 Thank You for the code snippet, I agree my code was messy. I can only confirm that there are empty files uploaded to the system. I do not know what causes the empty file and I suppose that it could be user error, but I do not see what would cause that. I have cleaned up the code as per you suggestions. Hopefully this will help. Quote Link to comment https://forums.phpfreaks.com/topic/176482-solved-upload-script-intermittently-creates-files-with-zero-bytes/#findComment-930368 Share on other sites More sharing options...
tcnjdeluca Posted October 5, 2009 Author Share Posted October 5, 2009 Solved: I require my users to upload file names without spaces. When using FireFox some users would simply delete the spaces in the path to the file they were trying to upload. This would create an empty file with the new name they just made in the path. I am adding a function to check for empty files and warn users. Quote Link to comment https://forums.phpfreaks.com/topic/176482-solved-upload-script-intermittently-creates-files-with-zero-bytes/#findComment-930895 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.