HTML Input: uploading multiple files maxes at 20 - php

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.

Related

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.

Request Entity Too Large with small files

I know there are many questions about Request Entity Too Large on internet but i could not find right answer for my problem ;)
I`m using HTML file input tag to let users upload their images .
<input type = 'file' class = 'upload-pic' accept='image/*' id ='Fuploader' name = 'pro-pic'><br>
There is nothing wrong with files less than 2 MB which is allowed by my site
But the problem is if some one decide to upload larger file like 5.10 MB , i can handle it by php and warn user that this file is too large
if($_FILES['pro-pic']['size'] > 2000000)
{
die("TOO LARGE");
}
But my problem is by uploading 5.10 MB file , Request entity too large error will be lunched and rest of my php code won`t work
I have checked post_max_size and upload_max_filesize they are both set to 8MB
But i get Error on 5.10MB !
And I need to find way to handle files even larger than 8MB because there is no way to guess what user may try to upload ;) and i don`t want them to get dirty and broken page because of REQUEST ENTITY TOO LARGE ERROR
Is there any way too fully disable this Error Or set upload_max_filesize and post_max_size to infinity ?
You need to set SecRequestBodyAccess Off.
Check the link i have given ..it would help you
https://serverfault.com/questions/402630/http-error-413-request-entity-too-large

PHP file input number limited to 20

I have multiple file uploads but the $_FILES seems to be chunked after 20 inputs
var_dump($_FILES); //gives array(20){ ... } although there are more file inputs
This is the public php_info
Which parameter is responsible for this limit ?
As a workaround, I used some javascript:
<input id="input_fileXX" type="file" class="input_file">
----
$('.input_file').on('change',function(){
$(this).prop('name',$(this).prop('id'));
});
This allows to only send the inputs that have change. This is useful when you can't change php.ini.
http://www.php.net/manual/en/features.file-upload.multiple.php
Since PHP 5.2.12, the max_file_uploads configuration setting acts as a limit on the number of files that can be uploaded in one request. You will need to ensure that your form does not try to upload more files in one request than this limit.

What is causing the file upload limit issue in 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.

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