Change the maximum upload file size
You need to set the value of upload_max_filesize
and post_max_size
in your php.ini
:
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
After modifying php.ini
file(s), you need to restart your HTTP server to use the new configuration.
If you can't change your php.ini
, you're out of luck. You cannot change these values at run-time; uploads of file larger than the value specified in php.ini
will have failed by the time execution reaches your call to ini_set
.
See the Description of core php.ini
directives.
PHP get actual maximum upload size
Drupal has this implemented fairly elegantly:
// Returns a file size limit in bytes based on the PHP upload_max_filesize
// and post_max_size
function file_upload_max_size() {
static $max_size = -1;
if ($max_size < 0) {
// Start with post_max_size.
$post_max_size = parse_size(ini_get('post_max_size'));
if ($post_max_size > 0) {
$max_size = $post_max_size;
}
// If upload_max_size is less, then reduce. Except if upload_max_size is
// zero, which indicates no limit.
$upload_max = parse_size(ini_get('upload_max_filesize'));
if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = $upload_max;
}
}
return $max_size;
}
function parse_size($size) {
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size.
$size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
if ($unit) {
// Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
}
else {
return round($size);
}
}
The above functions are available anywhere in Drupal, or you can copy it and use it in your own project subject to the terms of the GPL license version 2 or later.
As for parts 2 and 3 of your question, you will need to parse the php.ini
file directly. These are essentially configuration errors, and PHP is resorting to fallback behaviors. It appears you can actually get the location of the loaded php.ini
file in PHP, although trying to read from it may not work with basedir or safe-mode enabled:
$max_size = -1;
$post_overhead = 1024; // POST data contains more than just the file upload; see comment from @jlh
$files = array_merge(array(php_ini_loaded_file()), explode(",\n", php_ini_scanned_files()));
foreach (array_filter($files) as $file) {
$ini = parse_ini_file($file);
$regex = '/^([0-9]+)([bkmgtpezy])$/i';
if (!empty($ini['post_max_size']) && preg_match($regex, $ini['post_max_size'], $match)) {
$post_max_size = round($match[1] * pow(1024, stripos('bkmgtpezy', strtolower($match[2])));
if ($post_max_size > 0) {
$max_size = $post_max_size - $post_overhead;
}
}
if (!empty($ini['upload_max_filesize']) && preg_match($regex, $ini['upload_max_filesize'], $match)) {
$upload_max_filesize = round($match[1] * pow(1024, stripos('bkmgtpezy', strtolower($match[2])));
if ($upload_max_filesize > 0 && ($max_size <= 0 || $max_size > $upload_max_filesize) {
$max_size = $upload_max_filesize;
}
}
}
echo $max_size;
Laravel - validate file size when PHP max upload size limit is exceeded
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 at app/Exceptions/Handler.php
. Update the render
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 the FileException
exception.
public function render($request, Exception $exception)
{
if ($exception instanceof \Symfony\Component\HttpFoundation\File\Exception\FileException) {
// create a validator and validate to throw a new ValidationException
return Validator::make($request->all(), [
'your_file_input' => 'required|file|size:5000',
])->validate();
}
return parent::render($request, $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.)
PHP Change Max File Size Upload
using .htaccess file in the root directory, you can increase the maximum upload size
php_value upload_max_filesize 64M
php_value post_max_size 64M
php_value max_execution_time 300
php_value max_input_time 300
other way change
php.ini file
upload_max_filesize = 128M
post_max_size =64M
max_execution_time = 300
max_input_time = 300
How to increase upload video file size in php?
Add in folder file: .user.ini with
upload_max_filesize = 50M
post_max_size = 200M
Or test file size
if($_FILES['file']['size'] > $bytes){ // upload ... }
- https://www.php.net/manual/en/configuration.file.per-user.php
- https://www.php.net/manual/en/ini.list.php
- https://www.php.net/manual/en/reserved.variables.files.php
Wordpress upload size reduced to under 20MB even though it says the limit is 505MB - happened within last 24hrs
Solution found!
In the end, I went to plesk.com and paid for a support service (only $10/mo) and their developers figured it out in about 6 hours and 8 emails. Turns out, it was an issue with a plesk microupdate that enabled ModSecurity, which applies its own limits on upload size in a config file that loads a module.
****In their words:
One of Modsecurity packages contains the configuration file /etc/httpd/conf.modules.d/10-mod_security.conf which loads security2_module by default
However, Plesk treats modsecurity as disabled by default. Therefore we have such discrepancy: Plesk shows modsecurity as disabled but it is actually enabled on the server. Also, it is not possible to disable modsecurity with the command:
# plesk sbin modsecurity_ctl -d
This behavior has been submitted as a bug PPPM-8557 which is planned to be fixed in one of the future Plesk updates.
So, in order to stop ModSecurity from messing with things, it was temporarily disabled by enabling and disabling it in Plesk UI.
I can confirm that the upload form is now working as it should. Plesk.com paid support WELL worth the money.
Is php.ini upload_max_filesize combined?
Use post_max_size
to set the total and upload_max_filesize
for max per file.
Check: https://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize
upload_max_filesize
int
- The maximum size of an uploaded file. When an int is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used.
And https://www.php.net/manual/en/ini.core.php#ini.post-max-size
post_max_size
int
- Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger thanupload_max_filesize
. Generally speaking,memory_limit
should be larger thanpost_max_size
. When an int is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used. If the size of post data is greater thanpost_max_size
, the$_POST
and$_FILES
superglobals are empty. This can be tracked in various ways, e.g. by passing the$_GET
variable to the script processing the data, i.e.<form action="edit.php?processed=1">
, and then checking if$_GET['processed']
is set.
Related Topics
Checking to See If One Array'S Elements Are in Another Array in PHP
How to Delete Element With Domdocument
How to Create Virtual Host on Xampp
Warning: Domdocument::Loadhtml(): Htmlparseentityref: Expecting ';' in Entity,
How to Clear Apc Cache Entries
Error: "Input Is Not Proper Utf-8, Indicate Encoding !" Using PHP'S Simplexml_Load_String
How to Sanitze User Input in PHP Before Mailing
Retrieving Multiple Result Sets With Stored Procedure in PHP/MySQLi
How to Detect a Create, Update, Delete Query Is Successful in Codeigniter
Can You Store a Function in a PHP Array
How to Sort a Multidimensional Array by One of the Fields of the Inner Array in PHP
How to Get First 5 Characters from String
Can't Access Global Variable Inside Function
Sort and Display Directory List Alphabetically Using Opendir() in PHP
Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error