Uploading a large file with PHP loses other input values - php

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 = ?

Related

Let user change contents of their own (big) file

I've got a web server that accepts a file from the user. It is then processed and the user gets a chance to modify it. The file format is not comfortable for the user to operate with, so my PHP code parses the file and creates a form that would let the user change the file contents. The form is then submitted and a new, modified file is created. In the worst case scenario, the file provided can have up to 160 input fields per row and up to 500 rows which results in 80k of input fields. Even though this sounds horrible, it has to be like that (So all 500 lines have to appear on the same page). The problem arises because of PHP's max_input_vars which is set to 1000. There is no way I can modify it. Therefore I would like to know if there is a way to bypass that restriction or if there is any other, better way of implementing this.

Count and limit the number of files uploaded (HTML file input)

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.

html image file upload failing silently

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?

Multi file upload with PHP/Javascript and no flash

I'm trying to make a webpage that allows the uploading of multiple files at the same times. I will limit the file extensions to the most common images like JPG, JPEG, PNG and GIF.
I've done some research on this and everywhere I look it's flash this and flash that.
I don't want to use flash really. Especially with Flash 10, which disables the most common used method to enable multifile upload.
What I'm looking for is a way to keep creating more and more input fields, each with a browse button and then with one final upload button at the bottom of the form. Creating the new input fields with a Javascript is nog big deal really.
So I'm wondering how this works. Do I need to give all file-input fields the same name atribute so I can use 1 piece of PHP code to solve this? Or Is there some way for PHP to detect howmany files have been sumbitted and simply put the code for parsing a file inside a for-loop?
You can keep adding 'file' inputs but use a name of something like 'upload[]'
<input type="file" name="upload[]">
Then in $_FILES['upload'] you will have an array of files you can loop over like
foreach ($_FILES['upload'] as $file) {
echo $file['size'];
}
Here is the algorithm:
You add the new file input fields to your form. Each of this field MUST have a unique name. Then, on the server side, you loop through the $_FILES array looking how many files have been uploaded and handling them.
In using JavaScript to add new upload fields, you could also have JavaScript update some "hidden" input field with the number of upload fields in the form. That way, once you click Submit, that hidden value should be submitted and it will be trivial to parse out the $_FILES array for all the uploaded files.
If you have a reasonable maximum limit on the number of files, it's probably better to include that many file upload fields in the static form, then use the JavaScript to hide the superfluous ones, than to create them all dynamically. Then the form can still work when JavaScript is unavailable.
Side note: technically, single HTML file upload forms are already multiple file upload forms. According to the HTML form encoding standard, one should be able to select multiple files in the Browse dialogue box, and they'd be submitted as a multipart/mixed MIME structure.
However, almost nothing actually supports this. Older versions of Opera do on the client-side (and, I think, an ancient test browser of some sort, possibly Viola), and a few form-parsing components on the server-side, but not the PHP built-ins. In any case the UI is not very usable, so you're not missing much.

No error when uploading a really big file in PHP?

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.

Categories