Jump to content

Multiple File upload - need help with validating file size


izbryte

Recommended Posts

Hello! I have this validation script that seems to work great until I add the size validation. I'm ready to pull my hair out! Can someone tell me what I'm doing wrong?

 

if (isset($_POST['Submit'])) {
$user_id = $userdata[user_id];
    $number_of_file_fields = 0;
    $number_of_uploaded_files = 0;
    $number_of_moved_files = 0;
    $uploaded_files = array();
$max_filesize = 5242880; // Maximum filesize in BYTES (currently 5MB).
    $upload_directory = dirname(__file__) . '/'.$user_id.'/'; //set upload directory
if (!is_dir($upload_directory)) {
    mkdir($upload_directory, 0777, true);
}

    for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
        $number_of_file_fields++;
        if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
            $number_of_uploaded_files++;
		if($_FILES['images']['size'] > $max_filesize){
			echo "<b class='red'>Max file size is 5MB.</b><br/>";
			$sz = true;
		}
		$ext = validate_extension($_FILES['images']['name'][$i]);
		if (($ext == true) && ($sz == true)){
			$uploaded_files[] = $_FILES['images']['name'][$i];
			if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
				$number_of_moved_files++;
           		 }
		}else { echo "<b class='red'>File extention error. Only .doc, .pdf, .jpg and .gif files are allowed. </b><br/>";
		}

        }

    }
if ($number_of_uploaded_files >= 1){
	echo "Number of files submitted:<b class='red>".$number_of_uploaded_files."</b><br/>";
	echo "Number of successfully uploaded files:<b class='red>".$number_of_moved_files."</b><br/><br/>";
	echo "Uploaded File Name(s):<br/>" . implode('<br/>', $uploaded_files);
}


}

 

As of now it results in every uploaded file returning the error "Max file size is 5MB."

Link to comment
Share on other sites

You should add some debugging into that message to narrow down the issue.

 

echo "<b class='red'>Max file size is 5MB.</b><br/>";
echo "Debug: {$_FILES['images']['size']} > $max_filesize<br>";

 

See what that returns. If it's not what you expect, you should verify that you are using the array correctly.

 

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.