Php: What's the Point of Upload_Err_Ini_Size

PHP: What's the point of UPLOAD_ERR_INI_SIZE?

UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

This make sense when your POST_MAX_SIZE is bigger than UPLOAD_MAX_FILESIZE.

I tried in an environment where POST_MAX_SIZE is 128MB, then i set UPLOAD_MAX_FILESIZE to 1MB

Here's what i got (as expected):


Contents of $_POST:
Array
(
[random_field] => You should see this field in the $_POST superglobal.
)

Contents of $_FILES:
Array
(
[upload_test] => Array
(
[name] => Scan.tiff
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)

Although we don't get the size of the file, we do know that it's exceeding the upload_max_filesize.

Error UPLOAD_ERR_INI_SIZE Value: 1; when try to upload a image using php to FTP

It seems there is a problem with the upload code ftp_put(), FTP_BINARY and nothing to do with the php.ini OR upload size, here is final code.

<?php
$ftp_server = "ftp.xxxx.com";
$ftp_user_name = "ftpusername";
$ftp_user_pass = "ftppass";
$destination_file = "/public_html/" . $_FILES['file']['name'];
$source_file = $_FILES['file']['tmp_name'];

// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}

// upload the file
if (ftp_put($conn_id, $destination_file, $source_file, FTP_ASCII)) {
echo "successfully uploaded $destination_file\n";
} else {
echo "There was a problem while uploading $destination_file\n";
}

// close the FTP stream
ftp_close($conn_id);
?>

Get file size of attempted upload when UPLOAD_ERR_INI_SIZE error occurs?

You may consider using $_SERVER['CONTENT_LENGTH']. It has some overhead and represents the total size of a POST request, but in some situations this will be acceptable.

PHP upload_max_filesize

Check these setting in your php.ini also:
post_max_size, upload_max_filesize and memory_limit in php.ini.
post_max_size should be greater than upload_max_size.

and if these does not solve problem then check here for more details: http://www.satya-weblog.com/2007/05/php-file-upload-and-download-script.html

Is it necessary to check file size against php.ini and manually?

(1) The second step is not necessary but you have to separate the UPLOAD_ERR_INI_SIZE case from UPLOAD_ERR_FORM_SIZE case because they have different meanings as the manual says
http://php.net/manual/en/features.file-upload.errors.php

(2) the value of the entry upload_max_filesize at php.ini file is written using this notation:

XY: where X is an integer and Y is the unit, Y can be G(giga bytes), M (mega bytes)...etc, read the manual for more clarification
http://php.net/manual/en/faq.using.php#faq.using.shorthandbytes, so using (int)(ini_get('upload_max_filesize')); will not be safe all times and requires extra handling

Detect if uploaded file is too large

You could check the $_SERVER['CONTENT_LENGTH']:

// check that post_max_size has not been reached
// convert_to_bytes is the function turn `5M` to bytes because $_SERVER['CONTENT_LENGTH'] is in bytes.
if (isset($_SERVER['CONTENT_LENGTH'])
&& (int) $_SERVER['CONTENT_LENGTH'] > convert_to_bytes(ini_get('post_max_size')))
{
// ... with your logic
throw new Exception('File too large!');
}

How to see the error message in $_FILE array in PHP

You can use this array to display file-upload error messages:

$error_messages = array(
UPLOAD_ERR_OK => 'There is no error, the file uploaded with success',
UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded',
UPLOAD_ERR_NO_FILE => 'No file was uploaded',
UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder',
UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk',
UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload',
);

// prints "The uploaded file exceeds the upload_max_filesize directive in php.ini"
echo $error_messages[$_FILES['error']];

Is it possible to force PHP code to continue running, if a php.ini directive such as upload_max_filesize is exceeded when uploaded file exceeds this?

Violating upload_max_filesize doesn't abort the script execution. It'll just cause the error key in $_FILES to become UPLOAD_ERR_INI_SIZE.

Most likely, you're hitting an entirely different limit. For example, Apache has
LimitRequestBody and PHP itself has post_max_size or even max_file_uploads. In general, those directives don't abort the script either but simply wipe out the data excess.

My advice is to locate all the directives that may affect file uploads, set them one by one to an artificially low limit and verify what effect they have in your data. A check I typically do is to verify if $_SERVER['CONTENT_LENGTH'] is greater than zero for a request where $_SERVER['REQUEST_METHOD'] is POST but $_POST is empty. Aside that, checking error in $_FILES should have you fairly covered.



; Allow enough time for the upload to complete
max_input_time = 1200
; Max file size
upload_max_filesize = 50M
; Greater than upload_max_filesize to ease diagnose slightly large files
post_max_size = 100M
if (
empty($_FILES) &&
empty($_POST) &&
isset($_SERVER['REQUEST_METHOD']) &&
strtolower($_SERVER['REQUEST_METHOD'])=='post'
) {
// Error: post body was too large
// Bytes sent (and discarded): filter_input(INPUT_SERVER, 'CONTENT_LENGTH')
} elseif (
isset($_FILES['foo']) &&
$_FILES['foo']['error'] != UPLOAD_ERR_NO_FILE
){
// Upload
if ($_FILES['foo']['error'] === UPLOAD_ERR_OK) {
// Successful upload
}else{
// Failed upload
}
} else {
// No upload
}


Related Topics



Leave a reply



Submit