Jump to content

[SOLVED] Upload Script Intermittently creates files with zero bytes


tcnjdeluca

Recommended Posts

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.";



}

Link to comment
Share on other sites

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']);
?>

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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. 

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.