What is causing the file upload limit issue in php? - php

I'm reading this line in Linux. However, when I'm echoing this in the browser, nothing shows up. Is there something wrong with how I used the echo line?
// relevant code snippets
$mypic = $_FILES['upload']['name'];
$temp = $_FILES['upload']['tmp_name'];
$type = $_FILES['upload']['type'];
/*$finfo=finfo_open(FILEINFO_MIME_TYPE);
$type=finfo_file($finfo,$temp);
finfo_close($finfo);*/
echo "<pre>"; print_r($_FILES);
echo $mypic."<br />";
echo $type."<br />";
echo $_FILES['upload']['error']."<br />";
echo var_dump($type)."<br />";
If you suspect something is wrong with how I'm handling file inputs in another file, I've included that php file in this link.
<form ENCTYPE="multipart/form-data" method="post" action="insert.php">
Name: <input type="text" name="name" maxlength="15" /><br />
Email: <input type="text" name="email" maxlength="30" /><br />
Password: <input type="password" name="password" maxlength="15" /><br />
Confirm Password: <input type="password" name="cpassword" maxlength="15" /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="10000">
Choose your picture: <input type="file" name="upload"><p>
<input type="submit" name="submit" value="Register" /><br />
<p>
<center><h3><?php include("links.php"); ?></h3></center>
</form>
Here is the printout that I'm seeing:
Array (
[upload] => Array
(
[name] => protest.jpg
[type] =>
[tmp_name] =>
[error] => 2
[size] => 0
)
) protest.jpg
2 string(0) ""
------------------Update as of 9:40 p.m. May 5, 2012-------------------------
I tried an icon and found no problems other than permissions settings (I think I can solve this on my own for the time being). However, I'm still stuck on setting the file size. I followed Peter Stuart's instructions and got the following printout:
Apparently, the file size limits in these two settings are more than enough to handle the original images I had (which are under 200 kb). I don't know what more I can do in this case.

The file type is empty for the same reason that the filesize is 0 and the error is 2.
From Error Messages Explained:
UPLOAD_ERR_FORM_SIZE Value: 2; The uploaded file exceeds the
MAX_FILE_SIZE directive that was specified in the HTML form.
You have your max size set to 10000, which is in bytes, so that's roughly 10Kb. If it's a photo taken on any modern digital cam (over 4mgpx) it will probably need to be at least ten times that size. Just leave out the max size for now until you get a rough average of the image size people are submitting. PHP has a max upload size of its own to avoid tying up the line for too long.
To avoid issues like this in the future (not knowing if the file upload was successul), you probably want to wrap your code in something like:
$upload_error[0] = "AOK";
$upload_error[1] = "Server says: File too big!";
$upload_error[2] = "Browser says: File too big!";
$upload_error[3] = "File upload didn't finish.";
$upload_error[4] = "Ummm.... You forgot the file.";
$upload_error[6] = "Oops. Webmaster needs to fix something.";
$upload_error[7] = "Server says: I'm stuffed. Email webmaster.";
$upload_error[8] = "Server says: Not gonna do it. Webmaster needs to fix something.";
if($_FILES['upload']['error'] == 0) {
//do your thing and move it to the database.
} else {
$error_num = $_FILES['upload']['error'];
echo $upload_error[$error_num];
}

I would check your PHP upload size limit in your PHP ini file. It can cause adverse problems :)
If you create or go into your php.ini file and make sure the settings are as follows:
upload_max_filesize = 5M
post_max_size = 5M

The order of settings are also matter, and it should be like
upload_max_filesize = 5M
post_max_size = 5M
And I always got maximum size error when post_max_size place before upload_max_filesize.

This problem has 7 years but I stopped with it without finding a clear procedure to understand it. This is How I controlled it:
The sintom was that I could upload SMALL images (less than 2MB) BUT not bigger than 2MB. It's important to identify perfectly the ERROR. In this case "UPLOAD_ERR_FORM_SIZE:" The sintom in the DEBUG was that $_FILES["image"]["type"] = "", (ridiculous) and I knew that was a .JPG image for sure.
SOLUTION: Using XAMPP, STOP it. Configure php.ini, go to "upload_max_filesize=2M" which means that the file you try to upload has a limit of 2 Megabytes, So you Will change it to (for example) 3M. After that, I started again XAMPP, and proceeded to upload an image of 2.5 MB, and was successful.
Im sorry but my status can't show images of configuration in this comment.

Related

Server side file validation

I'm trying to make file upload to server by form:
<form action="send_valid.php" method="POST" enctype= "multipart/form-data">
<br>
<input type="file" name="pdf" id="pdf" accept="application/pdf"/>
<input type="hidden" name="MAX_FILE_SIZE" value="10000000"/>
<input type="submit" value="Wyƛlij">
</form>
and I want to allow user to send only pdf files of a max size 10Mb.
My php configuration for uploads is:
file_uploads = On
upload_tmp_dir = "E:\Xampp\tmp"
upload_max_filesize = 11M
max_file_uploads = 20
post_max_size = 12M
To check file size I use:
if($_SERVER["REQUEST_METHOD"] == "POST"){
var_dump($_FILES);
if(extract($_FILES)){
if($pdf['size']>10000000){
echo "File size is too large!";
}
}
Now I want to show user an error (for now) with echo when file is too big. It works fine if it is lower than 10Mb (even the code above works when I change size to 1Mb and file is larger then it will display echo), but for files of 10Mb and above it produces that error:
Warning: POST Content-Length of 11450416 bytes exceeds the limit of 8388608 bytes in Unknown on line 0
array(0) { }
I don't have any clue why it shows it exceeds 8Mb since in configs I couldn't find 8Mb anywhere.
Where can be the problem? Is there a way to catch an upload that exceeds configuration setting to not show user the php server error?
And if I want to make file validation does above method and checking file extension with for examle:
$ext = pathinfo($_POST['pdf'], PATHINFO_EXTENSION);
is it enough? Any insight on file validation would be really helpful.
Probably this
ini_set('post_max_size', '512M');
ini_set('upload_max_filesize', '512M');
Change 512 to any of you want.
Update the values of post_max_size and upload_max_filesize in your php configuration file.
Note that the values are measured in bytes.
Reference
http://php.net/manual/en/ini.core.php#ini.post-max-size
http://php.net/manual/en/ini.core.php#ini.upload-max-filesize
Put this code before move_uploaded_file function
if($_FILES['pdf']['size']>10000000) {
exit("File size is too large!");
}
Thanks

Force efficient small file upload for IE9/PHP/jQuery

I want to have a small upload limit (e.g. 100 kb for testing, perhaps 6 Mb ultimately). The size of the upload can be checked:
before the upload
fail when too much is uploaded
after the upload
If the user is trying a 1 Gb file ideally (1) should happen so that the file isn't uploaded at all. If not, (2) should happen so that it doesn't take long before the user knows the file is too big. I'd like to avoid the other possibility (3).
In HTML5 but not in IE9 the filesize can be checked before uploading using:
this.files[0].size
Get file size before uploading
In IE9 the following might work if the security settings are adjusted:
var objFSO = new ActiveXObject("Scripting.FileSystemObject"); var filePath = $("#" + fileid)[0].value;
var objFile = objFSO.getFile(filePath);
var fileSize = objFile.size; //size in kb
Ideally I'd like to use a method that works with IE9. I've heard about flash files being used. I'd like a method that is separate - not using a big plugin.
Here is my code. At the moment it uploads the whole file before it checks if the file size is too big.
<?php
if (isset($_FILES['myfile'])) {
if ($_FILES['myfile']['error'] == UPLOAD_ERR_FORM_SIZE) {
// $_FILES['myfile']['size'] is 0
echo 'Error: file is too big!<br>';
}
else if ($_FILES['myfile']['size'] > 100000) {
echo 'File size is too big!<br>';
}
else {
echo 'File uploaded ok.<br>';
}
}
var_dump($_FILES);
?>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input type="file" name="myfile" />
<input type="submit" value="Submit" />
</form>
The small upload limit is just for one form so I don't want to change the global PHP settings file.
I'm not sure using MAX_FILE_SIZE is a good idea - the person still has to upload the entire file and the filesize data is lost (I might want to tell people how big their upload was)
ini_get('post_max_size') returns 8M and ini_get('upload_max_filesize') returns 32M. If files that are over 8 Mb try to be uploaded the file is still fully uploaded (even 40+ Mb). After larger than 8 Mb files are uploaded var_dump($_FILES) returns an empty array.
If the file is bigger than about 600 Mb I get a 413 error... that is (1).
So I want to do (1) or (2) in IE9, PHP and jQuery.

php upload file does't work because of content

I'm trying to upload some Excel files on the server, but unfortunately is doesn't work for some files.
my html code looks like this:
<form action="my_upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
php file looks like this
echo '<pre>';
print_r($_FILES);
echo '</pre>';
and the output
Array
(
[file] => Array
(
[name] => speeds.xls
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
It is not file path, naming, size or rights problem but it seems that is a file content problem. I'm saying that is a file content problem because the upload succeeds in some cases. Also if I re-save the Excel file that dind't work in the first place, then the upload file succeed.
How can I solve this problem? Why $_FILES['file']['error'] = 1. How to prevent that?
*Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.*
The error code indicates that the file you're trying to upload is larger than the file upload size limit set in php.ini
You can get a full list of these codes here
You need to change the limit in php.ini to allow larger uploads. If you're on a hosted system you might need to check with them hoe to do this: arrangements vary from host to host.

HTML Input: uploading multiple files maxes at 20

I have an html input field, such as
<form method="post" action="process.php" enctype="multipart/form-data">
<div>
<h3>Files:</h3>
<input type="file" multiple="multiple" name="image[]" />
<input type="submit" value="Upload Image" />
</div>
</form>
And I want the user to be able to upload multiple files at once. My php for this uses a for loop to cycle through all the files, gathers information on each one, and then uploads them one by one.
for($i = 0;$image['name'][$i] == true;$i++)
{
//code
}
But this won't upload more than 20, ending with an error, Notice: Undefined offset: 20 in F:\www\hdp\process.php on line 39. Now, if I were to upload 5 images, it would give me Notice: Undefined offset: 5 in F:\www\hdp\process.php on line 39, but that would be ok because it would still upload all 5 photos (0,1,2,3,4). I need it to upload all the photos the user adds.
I know uploading lots of files at once could be a bad idea, but it is just the site admin, and it's a photography portfolio site. So he needs to be able to upload a lot of photos at once. And if it's important, they are being uploaded to a MySQL database.
max_file_uploads in php.ini setting was the cause. Change it to what you want and it will work.
I'm a bit confused by the 'wont upload more than 20' part... what are you basing this on? You're getting an error either way it seems.
Change your for loop to check for isset($image['name'][$i]) instead, and you will no longer get an error.
for($i = 0; isset($image['name'][$i]); $i++)
{
//code
}
As mentioned by yes123, you risk hitting the maximum POST size and should check php.ini.
Check your php.ini settings for post_max_size
Also
upload_max_filesize
memory_limit
max_execution_time
max_input_time
Similar to Fosco's solution, but works differently (uses sizeof and a int to int comparison instead of an isset)
Neither this nor his are necessarily any better, just different ways to tackle the problem.
for($i = 0, $size = sizeof($image['name']); $i < $size; $i++)
{
//code
}
And no, it doesn't run sizeof() each iteration. That's an easy way to screw over performance.

Upload size problem in PHP and MySql

I am uploading files to a MySql DB through PHP.
I am able to upload files upto 1MB size (found out by trial and error).
Files greater than 1 MB in size are not getting uploaded.
The MySql error printed by mysql_error() function in PHP is:
MySQL server has gone away
Can anybody please help me with this?
The MySql server is up and running only for requests > 1MB it is giving this error.
Regards,
Mayank.
P.S.:
I am using a form to upload the file.
<FORM METHOD="post" ACTION="fileUpload.php" ENCTYPE="multipart/form-data">
<INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="300000000">
<INPUT TYPE="hidden" NAME="action" VALUE="upload">
Description: <TEXTAREA NAME="txtDescription" ROWS="1" COLS="80"></TEXTAREA>
<INPUT TYPE="file" NAME="binFile" ID="binFile">
<INPUT TYPE="submit" NAME="Upload" VALUE="Upload">
</FORM>
Your sql query probably exceeds the max_allowed_packet size in which case the server will disconnect.
You might be interested in mysqli_stmt::send_long_data which allows you to send parameters longer than max_allowed_packet in chunks.
Update: "How can i change it? Is using mysqli is the only option?"
Afaik the value can't be altered on a per-session base, i.e. if you cannot change the server configuration (my.cnf or startup parameters) the value will be read-only. edit: As the comment suggests you can change the global value of the mysql server after it has been started if you have the proper permissions.
PDO/PDO_MYSQL (as of phpversion 5.3.0) doesn't seem to support send_long_data, but I'm not sure about that either. That would leave mysqli as the only option. I've recently noticed that Wez Furlong joined stack overflow. Since he is one of the authors of the PDO implementation he might know (though he did not write the pdo_mysql module).
(Completely untested and ugly) example
// $mysqli = new mysqli(....
$fp = fopen($_FILES['binFile']['tmp_name'], 'rb') or die('!fopen');
//$result = $mysqli->query('SELECT ##max_allowed_packet') or die($mysqli->error);
//$chunkSize = $result->fetch_all();
//$chunkSize = $maxsize[0][0];
$chunkSize = 262144; // 256k chunks
$stmt = $mysqli->prepare('INSERT INTO foo (desc, bindata) VALUES (?,?)') or die($mysqli->error);
// silently truncate the description to 8k
$desc = 8192 < strlen($_POST['txtDescription']) ? $_POST['txtDescription'] : substr($_POST['txtDescription'], 0, 8192);
$stmt->bind_param('sb', $desc, null);
while(!feof($fp)) {
$chunk = fread($fp, $chunkSize);
$stmt->send_long_data(1, $chunk) or die('!send_long_data.'.$stmt->error);
}
$result = $stmt->execute();
In order to upload large files to your server with PHP, you need to change 2 parameters in your php.ini file.
; Maximum allowed size for uploaded files.
upload_max_filesize = 50M
; Maximum size of POST data that PHP will accept.
post_max_size = 50M
50M = 50Mb

Categories