Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit file upload type file size in PHP?

Tags:

I have an upload form and am checking the file size and file type to limit the uploaded file to 2 megabytes and either .pdf, .jpg, .gif or .png file types. My goal is to have an alert message displayed to the user if they violate one of these rules.

There are four scenarios:

  1. Correct Size / Correct Type (working)
  2. Correct Size / INCORRECT Type (working)
  3. INCORRECT Size / Correct Type (not working)
  4. INCORRECT Size / INCORRECT Type (not working)

With my current code, it always displays the incorrect "type" message when the file size is greater than 2 megabytes (#4), even if the file type is correct (#3).

Any ideas why?

if (isset ( $_FILES['uploaded_file'] ) ) {      $file_size = $_FILES['uploaded_file']['size'];     $file_type = $_FILES['uploaded_file']['type'];      if (($file_size > 2097152)){               $message = 'File too large. File must be less than 2 megabytes.';          echo '<script type="text/javascript">alert("'.$message.'");</script>';      }     elseif (           ($file_type != "application/pdf") &&         ($file_type != "image/jpeg") &&         ($file_type != "image/jpg") &&         ($file_type != "image/gif") &&         ($file_type != "image/png")         ){         $message = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';          echo '<script type="text/javascript">alert("'.$message.'");</script>';              }         else {         store_uploaded_file($id);     }  }    
like image 612
Michael Avatar asked Feb 05 '12 21:02

Michael


People also ask

How do I change the maximum upload file size in PHP?

To increaes file upload size in PHP, you need to modify the upload_max_filesize and post_max_size variable's in your php. ini file. In addition, you can also set the maximum number of files allowed to be uploaded simultaneously, in a single request, using the max_file_uploads .

How is the size of file restricted in PHP?

By default, PHP has a limit set to 50 MB (megabytes) for uploading through PHP scripts on our servers. If you need a higher limit, you can usually change that through the php. ini file.

What is the maximum file size for file uploading in PHP?

The default PHP values are 2 MB for upload_max_filesize, and 8 MB for post_max_size. Depending on your host, changing these two PHP variables can be done in a number of places with the most likely being php. ini or . htaccess (depending on your hosting situation).


1 Answers

Something that your code doesn't account for is displaying multiple errors. As you have noted above it is possible for the user to upload a file >2MB of the wrong type, but your code can only report one of the issues. Try something like:

if(isset($_FILES['uploaded_file'])) {     $errors     = array();     $maxsize    = 2097152;     $acceptable = array(         'application/pdf',         'image/jpeg',         'image/jpg',         'image/gif',         'image/png'     );      if(($_FILES['uploaded_file']['size'] >= $maxsize) || ($_FILES["uploaded_file"]["size"] == 0)) {         $errors[] = 'File too large. File must be less than 2 megabytes.';     }      if((!in_array($_FILES['uploaded_file']['type'], $acceptable)) && (!empty($_FILES["uploaded_file"]["type"]))) {         $errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';     }      if(count($errors) === 0) {         move_uploaded_file($_FILES['uploaded_file']['tmpname'], '/store/to/location.file');     } else {         foreach($errors as $error) {             echo '<script>alert("'.$error.'");</script>';         }          die(); //Ensure no more processing is done     } } 

Look into the docs for move_uploaded_file() (it's called move not store) for more.

like image 125
Bailey Parker Avatar answered Sep 21 '22 14:09

Bailey Parker