
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to upload large files above 500MB in PHP?
Large files can be uploaded using PHP in two ways. Both of them are discussed below −
- 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.
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 file
php_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 −
<?php // changing the upload limits ini_set('upload_max_filesize', '50M'); ini_set('post_max_size', '50M'); ini_set('max_input_time', 300); ini_set('max_execution_time', 300); // destination folder is set $source = $_FILES["file-upload"]["tmp_name"]; $destination = $_FILES["file-upload"]["name"]; // uploaded folder is moved to the destination move_uploaded_file($source, $destination); ?>
Chunking
In this process, a large file is split into smaller parts and then uploaded. The ‘Plupload’ library can be downloaded and used.
<?php // the response function function verbose($ok=1,$info=""){ // failure to upload throws 400 error if ($ok==0) { http_response_code(400); } die(json_encode(["ok"=>$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.
- Related Articles
- How to download large files through PHP script?
- How to upload multiple files and store them in a folder with PHP?
- How to upload files using Selenium Webdriver?
- How to upload files using jQuery Dropzone Plugin?
- How to Increase File Upload Size in PHP
- How does selenium webdriver upload files to the browser?
- How to upload files in Laravel directly into the public folder?
- How to stream large .mp4 files in HTML5?
- Anyone Can Upload Files In Your Dropbox
- Upload file with php to another php server
- How to Handle Large CSV files with Pandas?
- How we can compress large Python files?
- How to split or break large files into pieces in Linux?
- How to Upload Image into Database and Display it using PHP
- Sending large files over internet

Advertisements