Jump to content

Limiting File Size for Uploads Not Working


codeline

Recommended Posts

I've got a typical form with an input type="file" for users to upload photos to my site (2mb max to be exact). If you view my code below, you'll notice that I set a variable as the uploaded file's size, and a variable for the max file size I want in bytes. When making sure that the uploaded file's size is LESS than the limit size I set, it should push an error.

 

However, I've noticed that my variable $uploadSize that is supposed to grab the upload file size is only returning "0" (zero). I've tried var_dump($_FILES) to see what was going on and it shows the array with the proper name of the uploaded file, etc. but the file size returns 0.

 

So any file size I upload will bypass my test to see if the file size is less than the limit size. I've tested uploading images 2mb or less and the photos have successfully been queried, moved and resized. However, if I try and upload images LARGER than 2mb, the form still queries all the inputted data into the database but the image isn't successfully moved.

 

I've used this same form and approach on a previous project and I didn't have any trouble. Can I get your guys' eyes on this and see if I'm missing anything small?

 

<?php

if(isset($_POST['submit'])){

// ------------------------------------------------------------- //

// A.   SET VARIABLES
// A1.  set variables for inputted data
$first = filter_var($_POST['first'], FILTER_SANITIZE_STRING);
$last = filter_var($_POST['last'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$email2 = filter_var($_POST['email2'], FILTER_SANITIZE_STRING);
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$description = filter_var($_POST['description'], FILTER_SANITIZE_STRING);

// A2.  set variables for uploaded submission
$uploadPath = $_FILES['uploadFile']['tmp_name'];
$uploadSize = $_FILES['uploadFile']['size'];
$uploadLimit = 2097152;	/* 2mb max file size */

// A3.  create error array
$errors = array();

// ------------------------------------------------------------- //

// B.   VALIDATE FIELDS  
// B1.  validate required fields
if (empty($first)){
	array_push($errors, 'first');	
}
if (empty($last)){
	array_push($errors, 'last');	
}
if (empty($email)){
	array_push($errors, 'email');
}
if (empty($email2)){
	array_push($errors, 'email2');	
}
if (empty($name)){
	array_push($errors, 'name');	
}

// B2.  validate emails
if ($email != $email2){
	array_push($errors, 'emailmismatch');	
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
	array_push($errors, 'invalidemail');	
}

// B3.  validate uploaded image file size
if ($uploadSize > $uploadLimit){
	array_push($errors, 'filesize');	
}


// ------------------------------------------------------------- //

// if no errors, continue query
if (sizeof($errors) == 0){
	// continue query
}

}

?>

 

<form method="post" enctype="multipart/form-data" id="submit-form">
    <label for="first">First Name:</label>
    <input name="first" type="text" value="<?php echo $first; ?>"<?php if(in_array('first', $errors)){ echo ' class="error"'; } ?>>
    
    <label for="last">Last Name:</label>
    <input name="last" type="text" value="<?php echo $last; ?>"<?php if(in_array('last', $errors)){ echo ' class="error"'; } ?>>
    
    
    <label for="email">Email Address:</label>
    <input name="email" type="text" value="<?php echo $email; ?>"<?php if(in_array('email', $errors)){ echo ' class="error"'; } else if (in_array('emailmismatch', $errors)){ echo ' class="error"'; } ?>>
    
    <label for="email2">Confirm Email Address:</label>
    <input name="email2" type="text" value="<?php echo $email2; ?>"<?php if(in_array('email2', $errors)){ echo ' class="error"'; } else if (in_array('emailmismatch', $errors)){ echo ' class="error"'; } ?>>
    
    <br><br><br><br>
    
    <label for="name">Your Photo Name:</label>
    <input name="name" type="text" value="<?php echo $name; ?>"<?php if(in_array('name', $errors)){ echo ' class="error"'; } ?>>
    
    <label for="description">Describe The Photo: <span class="optional">(optional)</span> <div class="right"><span class="optional">300 characters max</span></div></label>
    <textarea name="description" onKeyDown="limitInput(this.form.description,this.form.countdown,300);" 
onKeyUp="limitInput(this.form.description,this.form.countdown,300);"><?php echo stripslashes($description); ?></textarea>
    
    <label for="upload">Photo Image: <span class="optional">(.JPG's only, max 2mb file size)</span></label>
    <input type="hidden" name="MAX_FILE_SIZE" value="2097152">
    <input id="uploadFile" name="uploadFile" type="file"<?php if(in_array('badimage', $errors)){ echo ' class="error"'; } ?> />
    
    <input type="submit" name="submitFeature" class="submit" value="Submit Your Feature">
</form>

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.