I know you can control the size of uploads in PHP using $_FILES['userfile']['size'] > XXX
My question I suppose is performance related.
When you upload a file, my understanding is the whole file gets uploaded to a temporary location, and then you have access to $_FILES
What happens if a user attempts to upload a 10gb file? (as an example of a very large file)
If a large file is attempted to be uploaded, does this waste server bandwidth as the file needs to be uploaded before it can be processed/validated.
I know PHP has like timeouts etc but I'm curious if there is a performance impact from users attempting to upload very large files, even if (for example) the max file size is 2mb.
Is this a concern or something unavoidable and just to not worry.
Thanks.
Both apache and php have max-post limitation to prevent such behavior.
from php.ini:
; Maximum allowed size for uploaded files.
upload_max_filesize = 4M
; Maximum size of POST data that PHP will accept.
post_max_size = 8M
Actually, the [size] isn't there for control, it's simply the size of the uploaded file. By the time your script gets fired up to check that, PHP (and the webserver) have already handled the uploaded and applied their own internal limits (Apache's LimitRequestBody, PHP's upload_max_size, etc...).
PHP will allow all uploads to proceed, if they've been enabled via the file_uploads INI setting. Since you can't really trust the client, the client-provided size will be ignored and the upload will proceed until it either completes or hits the upload limit. So, yes, it can waste your bandwidth.
If you allow uploads, then it can be abused. But, there's no real difference between someone uploading a 10gig file or someone doing a POST with 10gig of bogus data. Either way, you've got 10gig of data coming your way.
Related
I am trying to upload large files (5-8Mb) in my Symfony2 projects. For some reason anytime I upload anything larger than 2Mb the script bombs out. I can upload anything smaller than 2Mb. I have changed my php.ini file and php -i reflects these changes. I have restarted apache. What else do i need to do to make these larger file upload work. Thanks.
Ran into this issue before; try a chunking method of uploading. Most likely the script is exceeding the max execution time. So (without knowing what your logic looks like) have a function that sends the request (say 1MB) to another file that reads the input stream of the binary data within a while loop and continues to execute as long as the content length of the binary data does not equal zero. The while loop writes the data to a temp file and when it hits zero saves it to a permanent directory (or whatever you want to do from there) You can increase the max memory allowed and execution time in php.ini but this introduces security issues and is not recommended.
In your current PHP config, you probably limited the max upload size to 2M, so Symfony display the wrong error.
So check your php.ini file (/etc/php5/apache2/php.ini on Linux) and increase max_upload_size to fit your field :
upload_max_filesize = 20M
Don't forget to restart apache : apache2ctl restart
Now it should work !
Any idea on how big the file size could one file uploaded using php and html5?
And
Is there any suggestions on good components or example to do this?
thanks a lot!
On the server side the maximum upload size is limited by php post_max_size and upload_max_filezize.
Also your webserver can limit the maximum size of your post body. E.g. Apache limitrequestbody which defaults to 0 = unlimited or nginx client_max_body_size which defaults to 2MB
If you are planning to upload large files using html5 you might want to have a look at file.slice which is supported by all modern browsers
Support for .slice in the File API
Firefox supports the Blob API and the .slice APIs that come with it. This can help people who want to process parts of large File objects from JavaScript without having to load the whole file into the memory. People who reliably upload large files can use some server and JS code to split a large file into sections and upload chunks, including re-retrying failed sections, or even uploading several sections, in parallel.
Using this, you could upload giant files in chunks and merge them on the server-side again.
EDIT
Found this great article which explains html5 uploads by streaming via xhr
http://www.webiny.com/blog/2012/05/07/webiny-file-upload-with-html5-and-ajax-using-php-streams/
This procedure has a very low memory footprint, you might still run into the webserver and php upload limits because this is done with a single request. The code should give you an idea on how the whole technology works.
PHP's pretty crappy when it comes to large file uploads, particularly because you have to a memory limit higher than the size of the file. As well, Apache on 32bit systems tends to have a 2gig file limit itself, so even if PHP could handle the upload, Apache will choke.
You need to set the value of upload_max_filesize and post_max_size in your php.ini :
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
If you can't change your php.ini, you're out of luck. You cannot change these values at run-time; uploads of file larger than the value specified in php.ini will have failed by the time execution reaches your call to ini_set.
See the Description of core php.ini directives.
The maximum size of an uploaded file is integer.
When an integer is used, the value is measured in bytes
The maximum file size value is defined in phi.ini file.
search this in php.ini
Maximum allowed size for uploaded files.
upload_max_filesize = 32M
My hosts server has an .ini upload_max_filesize of 4mb. I have written some PHP which checks the file size of the upload and throws an error if that size is exceeded. Problem is if the file they are trying to upload is larger than the .ini setting of 4mb my code doesnt get executed as the $_FILES variable shows file size of 0 and error code 1. Is there a way i can check the size of an upload that is bigger than what is set in .ini??
Thanks
Paul
You cannot check whether a file is bigger than allowed size before the user has tried to upload it.
PHP is a server-side programming language, it takes the flow control after the webserver handled the request completely.
And your code gets the control after some internal php routines have been performed (such as handling file uploads, slashing request strings, filling superglobals, etc)
So the most correct scenario for you to follow is to check if error appeared during file upload and behave accordingly.
What is standard size to upload an image.When user are uploading a large image to my website upload time error is occurring.I am resizing image at the time of uploading also.I want to modify my code as if large image will go to upload then image upload should not occur.
So what is standard size of image to upload on server for any website ? So that way i will fix my code.
Thanks in advance
php.ini controls file uploads with several configuration directives.
file_uploads controls whether uploading files is allowed. Yes, 1 is the default.
upload_tmp_dir is the temporary directory used for storing files when doing file upload. Must be writable by whatever user PHP is running as. If not specified PHP will use the system's default. The default is NULL.
upload_max_filesize is what you're interested in. It's the largest upload allowed. The default is 2M. If you supply only an integer, it is assumed to be bytes. Or you can change it using a shortcut, like this:
upload_max_filesize = 10M
max_file_uploads is the max number of files that can be uploaded simultaneously. The default is 20.
Another setting that affects uploading files is the max size of POST data which is controlled with post_max_size.
If you do not need / want to change your php.ini file, you can set these values for the duration of your scripts execution using string ini_set ( string $varname , string $newvalue ).
This returns the old value if you succeeded in changing the value, and it returns false if the change didn't succeed. So, to change upload_max_filesize to 10M you could do :
if ( ini_set('upload_max_filesize', '10M') )
{
// Do stuff that requires big files to be uploaded
}
As a foot note to changing these memory values:
PHP allows shortcuts for bit values, including K (kilo), M (mega) and G (giga). PHP will do the conversions automatically if you use any of these. Be careful not to exceed the 32 bit signed integer limit (if you're using 32bit versions) as it will cause your script to fail.
Most times php is configured to accept no more the 2mb files.
Check here for solution
For the memory problem try:
ini_set("memory_limit","80M"); //or any amount of ram but do not go to high
Your thumbnailer probably uses to much ram because of the high resolution of your image.So giving more memory to php is a quick hack to make it work. But big enough images will still crash.
I want to upload and store video files to my server using PHP. Could any one please provide me some example about how to upload a large file using PHP?
Please keep in mind that these files are generally larger than 200 MB.
I think the question is fairly limited, is it the post size where there are problems? Uploading files of this size, really should be handled by something else than the normal upload control. You should see if you can give the user a progress on the upload, because otherwise users probably will cancel the upload if it takes too long.
First hit on google: Google search
http://bluga.net/projects/uploadProgressMeter/
Look at this move_uploaded_file, but if it's so big, it needs to be allowed in php.ini to upload this big files. And maybe you will have problem with Apache as well, because default time when it disconnects you is 5 minutes.
You will need to edit PHP.INI and change those two parameters:
upload_max_filesize = 500M
post_max_size = 500M
...for a maximum upload filesize of 500MB.
If you have a properly set-up server you can put php.ini files into your htdocs root, and it will be effective.