I have my website hosted at a shared server. The maximum upload limit is 5MB. All the validations and file uploading works fine if the uploaded file is under 5MB. But when a file greater than 5MB is uploaded, i see this . How can I validate or force the file to be under the upload limit from server?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
in your controller
and in php.ini
In laravel you can validate like this -
The value is in kilobytes. i.e. max:10240 = max 10 MB. And in jquery -
binds to onchange event of your input field
Hope this will help you.
post_max_size and upload_max_filesize are directives you can set to configure your php.ini file OR .htaccess file OR httpd.conf file.
php.ini example:
.htaccess example:
httpd.conf example:
You don't seem interested in changing the PHP limits to allow larger files. It looks to me like you want your max upload to be 5MB, and return a proper response if it is over that.
You can handle the
FileException
exception inside your exception handler atapp/Exceptions/Handler.php
. Update therender
method to add in the code you need. For example, if you'd like to return a validation exception, you will need to handle the validation inside the exception handler for theFileException
exception.This is untested, but should give you the general idea.
You can also do client side validation via javascript, so that a file that is too large is never actually sent to your server, but javascript can be disabled or removed by the client, so it would be good to have nice server side handling set up.
For the client side validation, if you attach an event handler to the "change" event for the file input, you can check the file size using
this.files[0].size
, and perform any additional actions after that (disable form, remove uploaded file, etc.)You could do this with jQuery / javascript (Which means that the file will not be uploaded but you can check the file before it is sent to your PHP script)
But always do serverside validation anyways. Incase you move to a stronger website and still want your website to work without any quirks.
All of this is untested, but should work.
you can check size of your file by max validator