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:
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); } }
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 .
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With