I have a form where users can enter multiple images to upload along with a bunch of other information. It looks like there is some condition that is causing the last few images not to upload, for some cases. For example, testing the form when uploading about 5 images, they all upload fine. When there are more, somewhere between 10 and 20-some, and this number does vary, then the last few may not upload. I've printed out the $_FILES array, and the selected images don't appear at all. So it looks like they aren't even being sent.
These images are quite small, only about 10-40k. My upload_max_filesize is 2M, and post_max_size is 8M, so that can't be it. Not only that, but these same images still fail to upload when the previous ones in the form are not uploaded - if you leave the first 20 file inputs blank, and select something for the 21st, that image still doesn't upload. And this has been tested with multiple images, so there isn't something wrong with the image file. Note - there aren't exactly 21 file inputs - it's variable, the user can add more or delete some.
I've also tested multiple browsers, and multiple computers. The only thing is that this problem happens only on my production server and has never occurred on my development server, so that could be a clue. It's also always the inputs at the end of the form, sometimes the last one, sometimes several, depending upon the form inputs being tested.
Is there any inherent limit to the number of file inputs you can have in a single form? I haven't pinpointed an exact number that causes the issue.
http://www.php.net/manual/en/ini.core.php
max_file_uploads 20 PHP_INI_SYSTEM
Available since PHP 5.2.12.
Could be max_execution_time for the receiving function. I think the default is often 30 seconds, and perhaps a large upload is taking longer than that?
If your dev server is localhost, that could be quite speedy and not have surfaced the issue until a true remote server was used?
Related
We're developing a website where users may change their slider images. (fullscreen slider) Each image is around 2000px to 2000px and we allow users to upload as many images as they want in our HTML form (capped to 10).
Their upload speed will be pretty slow and I believe we'll easily pass the max_execution_time of PHP which is default to 30 seconds. We will also let users to upload some .rar/.zip files in the future capping at 100MB.
We had few ideas but I wanted to ask SO for a better solution/reviews.
We can change 30 seconds for alot higher value since we have access to PHP.ini and let users upload all images at once, but that may create performance related issues in long term. (This is not an option!)
We can make use of javascript in client size. Foreach image paths specified in HTML form, javascript may post it with XMLHttpRequest one by one and expect a response. If the response is true, javascript moves to the next images and attempts to upload it. (Profit: each image will start php itself and get their own 30 seconds lifetime.)
Javascript solution won't work in file uploads when the file is above 50MB. Customers are usually capping at 25kbps upload speed in target region so there is no way they can upload 50MB in 30 seconds. Similar to #2, we may use a script where uploaded file saves in bytes every 30 seconds and client continues to push remaining bytes, or anything alike.
Basically, how would you complete this task?
We don't want to rely on PHP.ini, so increasing max_execution_time shouldn't be an option.
Should we go with #2 for image uploads and what can you suggest for #3?
Take a look into chunked uploads.
You'll need to use some sort of uploader script like JUpload or plUpload. You can specify how large the chunk of a file should be sent to the server. For example, if you have a 10MB file, you can chunk it to 1MB, so 10 1MB chunks would be uploaded to the server. In the case of a slow connection, just make the chunks smaller like 500KB.
I made a simple script that uploads file that are submitted through a simple html form,
It works fine with files that are smaller than 5 mega bytes.
When I upload larger files they start to screw things up..
I thought it might be that the execution time is too short, so I used this:
set_time_limit(500);
Sadly, it didn't make any difference.
In the form, I have a hidden input that helps me detect if the form was submitted
Next, I checked the apache error log and I found that that input was null,
So it seems like the long execution time loses the other values..
is there any simple solution for this? or shall I just change the way I detect if the form was submitted?
Thanks
Please check you php settings in php.ini for maximum post size with post_max_size = ?
I am looking for a multiple file uploader that can upload limited number of files with following specifications.
Something like the plugin given below, that shows a upload button as well, and uploads all the files using some jquery or ajax working (without the use of flash) showing a slider to show the uploading in process, and on event complete shows them just below the input box. With a cross shown in some corner with respect to each file, for the user to unselect that particular file.
And also make sure, the number of files doesn't exceeds the provided number.
http://www.fyneworks.com/jquery/multiple-file-upload
I used this plugin and it does the main job (select multiple files and limit them), but my script fails when the size of the files is too large and they are too many in the number.
Anything that could do the job, near around expected conditions is welcome. Also, the script should be php compatible.
Something that can limit the number of files and upload them to the server and just show them below the input button would do. I will manage the rest. But it should be easy to use.
I think you should look at jQuery File Upload (link to its demo).
It seems to do all that you are asking for and more. It also works well with PHP. You can set the size limit for files.
It does use Twitter Bootstrap, so it looks great by default. But if you want to change the styling (for example replacing the Cancel button with an X) you should be able to do that in CSS.
It does not use Flash
It works with PHP
It allows you to limit by file size and number of files (see documentation)
It allows you Drag and Drop (not one of your requirements, but cool nonetheless)
It shows progress for individual files and an overall progress
I have been working with this plugin and so far it is been great!
www.uploadify.com
I have that basic, well known multiple file upload form. Something like that...
<input type="file" multiple="multiple" name="something[]" />
Is there a way (preferable a hard coded option) to limit the number of files that user can select? By limit i mean strictly blocked, not just a warning if number is exceeded.
Thanks in advance. :)
You can implement a javascript solution to check the number of files that have been selected, which you can then use to forbid uploading to the server. There are really only going to be client-side solutions to this problem though, as there is really nothing stopping a user from posting these files to your php script anyway. You can specify a maximum upload size limit, but it isn't specific to the number of files that are being uploaded.
Your best bet is to implement some javascript checking, specifying a reasonable maximum upload size for your http server (or in PHP), and then ignoring any file uploaded if it exceeds your maximum count.
Read about the HTML5 File API here to restrict the count of selected files: http://dev.w3.org/2006/webapi/FileAPI/
Here is the php.ini docs which explain how to make a size limitation on uploads: http://php.net/manual/en/ini.core.php
As was suggested in the comments, check out: http://php.net/manual/en/ini.core.php#ini.max-file-uploads
we all know that in an array first element is stored in 0 th index, 2nd in 1st index......nth in (n-1)th index
now you cant take count($_FILES['something']) which will always return 5 since there are 5 keys in the array
now the question is what does $_FILES['something']['name'][0] contains? => name of first file uploaded
so check like
if(empty($_FILES['something']['name'][0]))
{
//checking whether a single file uploaded or not
//if enters here means no file uploaded
//so if you want you may put a check/validation here to make file
//upload a must in your website
}
if(isset($_FILES['something']['name'][5]))
{
//checking whether 6 files uploaded or not
//so here you can put a check/validation to restrict user from
//uploading more than 5 files
}
Pure Js alternatives that do that and much more:
FineUploader. Demo
bluimp's jQuery File Upload Plugin. Usage: jquery file upload restricting number of files
They are also available at https://cdnjs.com/
Some plugins seem to assist with this, such as: Uploadify (flash based). However I haven't used this one or others enough to know if they how restricting they can limit uploads.
More info on Uploadify Multiple Uploads.
I have a PHP form for uploading files and it works fine and displays an error message if something went wrong. This is all good.
The problem is when I test with a really big file, it just refreshes the page as if I hadn't sent a file at all, and none of the $_POST variables from the form are even sent to the server.
I want to display an error to the user, letting them know that the file is too big. However, I can't do this.
Does anyone know what is happening?
Check your PHP ini file, which governs how large a file PHP will allow to be uploaded. These variables are important:
max upload filesize (upload_max_filesize)
max post data size (post_max_size)
memory limit (memory_limit)
Any uploads outside these bounds will be ignored or errored-out, depending on your settings.
This section in the docs has the best summary: http://ca3.php.net/manual/en/features.file-upload.common-pitfalls.php
EDIT: Also note that most browsers won't send uploads over 2GB in size. This link is outdated, but gives an idea: http://www.motobit.com/help/scptutl/pa98.htm. Anyone have a better source of info on this?
There are also limits that can be imposed by the server, such as Apache: http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestbody
To truly see what's going on, you should probably check your webserver logs, check the browser upload limit (if you're using firefox), and try seeing if print_r($_FILES) generates any useful error numbers. If all else fails, try the net traffic monitor in firebug. The key is to determine if the request is even going to the server, and if it is what the request (including headers) looks like. Once you've gotten that far in the chain, then you can go back and see how PHP is handling the upload.
Your $_POST is most likely empty because the upload exceeded the post_max_size directive:
From PHP's directives page:
If the size of post data is greater
than post_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.
you can not know the size of the file that is being uploaded, until it gets fully uploaded. so if you want to determine the file size, and then show an error for large files, first you must have the file uploaded, then check the file size. inorder to have a large file uploaded, you should set 2 settings in your php configuration file, php.ini.
open it up and search for "upload_max_filesize" and set the value to maximum value you want. then you must set the maximum value for POST parameters, because uploaded files are recieved via HTTP POST method. find "post_max_size" and set it to a large value to.
apart form the options as the former users answered:
max upload filesize (upload_max_filesize)
max post data size (post_max_size)
memory limit (memory_limit)
there is also one, when the file is very large, or the line is busy, there need to much time to execute this operation, which will hit to ceil of the script execution limit.
max_execution_time
I would implement a simple script that does regular ajax calls to know how much of the upload has been done. You can then implement some sort of progress bar. There are a couple of examples on the net:
http://martinjansen.com/2007/04/28/file-upload-progress-bars-with-php/
max upload filesize (upload_max_filesize)
max post data size (post_max_size)
are the directives you need to set. I have tested it with multiple OS, browsers etc and these are the only two thing you need be worried about.