Php handle large file uploads
Large files can be uploaded using PHP in two ways. Both of them are discussed below − Show
The php.ini file can be updated as shown below − upload_max_filesize = 50M post_max_size = 50M max_input_time = 300 max_execution_time = 300 This should be avoided since it would change the settings of the server and other projects too. Updating the htacess filephp_value upload_max_filesize 50M php_value post_max_size 50M php_value max_input_time 300 php_value max_execution_time 300 Changing the inline setting − ChunkingIn this process, a large file is split into smaller parts and then uploaded. The ‘Plupload’ library can be downloaded and used. $ok, "info"=>$info])); } // invalid upload if (empty($_FILES) || $_FILES['file']['error']) { verbose(0, "Failed to move uploaded file."); } // upload destination $filePath = __DIR__ . DIRECTORY_SEPARATOR . "uploads"; if (!file_exists($filePath)) { if (!mkdir($filePath, 0777, true)) { verbose(0, "Failed to create $filePath"); } } $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : $_FILES["file"]["name"]; $filePath = $filePath . DIRECTORY_SEPARATOR . $fileName; // dealing with the chunks $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0; $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0; $out = @fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab"); if ($out) { $in = @fopen($_FILES['file']['tmp_name'], "rb"); if ($in) { while ($buff = fread($in, 4096)) { fwrite($out, $buff); } } else { verbose(0, "Failed to open input stream"); } @fclose($in); @fclose($out); @unlink($_FILES['file']['tmp_name']); } else { verbose(0, "Failed to open output stream"); } // check if file was uploaded if (!$chunks || $chunk == $chunks - 1) { rename("{$filePath}.part", $filePath); } verbose(1, "Upload OK"); ?> When a file whose size is greater than 500 MB is tried to be uploaded, it successfully gets uploaded.
Updated on 06-Apr-2020 09:01:55
File upload is a common task we do in almost all of our modern web projects. With all different tools available, it is not that hard to implement file upload feature in any language. But, still, when it comes to large file upload things gets a bit complicated. Say, you are trying to upload a fairly large file. You have been waiting for more than an hour already and the upload is at 90%. Then suddenly, your connection drops or browser crashed. The upload is aborted and you need to start from the beginning. Frustrating, isn’t it? Even worse, if you are in a slow connection, like many places in the world, no matter how often you try you will only be able to upload first part of the upload every time. In this post, we will see an attempt to solve this problem in PHP by uploading files in resumable chunks using tus protocol. Resumable File Upload in PHP — DemoWhat is tus?Tus is a HTTP based open protocol for resumable file uploads. Resumable means we can carry on where we left off without re-uploading whole data again in case of any interruptions. An interruption may happen willingly if the user wants to pause, or by accident in case of a network issue or server outage.
Why tus?Quoting from Vimeo’s blog:
Another main benefit of uploading a file this way is that you can start uploading from a laptop and even continue uploading the same file from your mobile or any other device. This is a great way to enhance your user experience. Pic: Basic Tus ArchitectureGetting StartedLet’s start by adding our dependency. $ composer require ankitpokhrel/tus-php tus-php is a framework agnostic pure PHP server and client implementation for the tus resumable upload protocol v1.0.0.
Creating a server to handle our requestsThis is how a simple server looks like. // server.php$server = new \TusPhp\Tus\Server('redis'); You need to configure your server to respond to a specific endpoint. For example, in Nginx, you would do something like this: # nginx.conf Let’s assume that the URL to our server is http://server.tus.local.So, based on above nginx configuration we can access our tus endpoints using http://server.tus.local/files. Now, we have following RESTful endpoints available to work with. # Gather information about server's current configuration Check out the protocol details for more info about the endpoints.
Handling upload using tus-php clientOnce the server is in place, the client can be used to upload a file in chunks. Let us start by creating a simple HTML form to get input from the user. After a form is submitted, we need to follow few steps to handle the upload.
// Tus client$client = new \TusPhp\Tus\Client('http://server.tus.local'); The first parameter in the above code is your tus server endpoint. 2. Initialize client with file metadata To keep an upload unique, we need to use some identifier to recognize the upload in upcoming requests. For this, we will have to generate a unique upload key which can be used to resume the upload later. You can either explicitly provide an upload key or let the system generate a key by itself. // Set upload key and file meta$client->setKey($uploadKey) If you don’t provide upload key explicitly, above step will be something like this: $client->file($_FILES['tus_file']['tmp_name'], 'your file name');$uploadKey = $client->getKey(); // Unique upload key 3. Upload a chunk // $chunkSize is size in bytes, i.e. 5000000 for 5 MB$bytesUploaded = $client->upload($chunkSize); Next time, when you want to upload another chunk you can use same upload key to continue. // To resume upload in next request$bytesUploaded = $client->setKey($uploadKey)->upload($chunkSize); Once the upload
is complete, the server verifies upload against the checksum to make sure the uploaded file is not corrupt. The server will use
Handling upload using tus-js-clientUppy is a sleek, modular file uploader plugin developed by same folks behind tus protocol. You can use uppy to seamlessly integrate official tus-js-client with a tus-php server. That means we are using php implementation of a server and js implementation of a client. uppy.use(Tus, {
Partial UploadsThe tus-php Server supports concatenation extension and is capable of concatenating multiple uploads into a single one enabling clients to perform parallel uploads and to upload non-contiguous chunks. Partial file upload using tus-phpThe full example of partial uploads can be found here. Final WordsThe tus-php project itself is still in its initial stage. Some sections might change in the future. Three different implementation examples can be found in the example folder. Feel free to try and report any issues found. Pull requests and project recommendations are more than welcome. Happy Coding! How can I upload large files over 500MB in PHP?How to upload large files above 500MB in PHP?. By changing the upload_max_filesize limit in the php. ini file.. By implementing file chunk upload, that splits the upload into smaller pieces an assembling these pieces when the upload is completed.. How can I upload 10 MB file in PHP?To increaes file upload size in PHP, you need to modify the upload_max_filesize and post_max_size variable's in your php. ini file. In addition, you can also set the maximum number of files allowed to be uploaded simultaneously, in a single request, using the max_file_uploads .
How do I handle a large file upload?Possible solutions: 1) Configure maximum upload file size and memory limits for your server. 2) Upload large files in chunks. 3) Apply resumable file uploads. Chunking is the most commonly used method to avoid errors and increase speed.
How can I upload more than 2 MB in PHP?by default PHP will not handle file uploads larger than 2MB, if one requires PHP to handle larger files then one must set upload_max_filesize and post_max_size in your php. ini file to be larger than 2MB.
|