Example of setting up the multipart upload function for large file cutting in PHP,
Background
In website development, file upload is a common function. I believe many people will encounter this situation and want to upload a file, and then the webpage prompts "this file is too large ". In general, we need to limit the size of the uploaded files to prevent unexpected situations.
However, in some business scenarios, uploading large files is necessary, such as email attachments or internal OA.
Problem
Why can't the server directly transfer large files? It depends on several configurations in php. ini.
Upload_max_filesize = 2 M // PHP maximum acceptable file size post_max_size = 8 M // maximum POST value received by PHP 'memory _ limit = 128 M // maximum memory size max_execution_time = 30 // maximum execution time
Of course, you cannot simply or roughly increase the values above. Otherwise, it will sooner or later be a problem to eat up the server memory resources.
Solution
Fortunately, HTML5 has opened a new file api, and can also directly operate binary objects. We can achieve FILE cutting on the browser side, and use the Flash solution according to the previous practice, implementation will be a lot of trouble.
JS ideas
1. Listen to the onchange event of the upload button
2. Get the FILE object of the FILE
3. Cut the FILE object and attach it to the FORMDATA object.
4. Send the FORMDATA object to the server through AJAX
5. Repeat Steps 3 and 4 until the file is sent.
PHP ideas
1. Create an upload folder
2. Move the file from the temporary upload directory to the upload folder
3. After uploading all file blocks, merge the files.
4. delete folders
5. Return the uploaded file path.
DEMO code
Front-end code
<! Doctype html>
PHP code
<? Phpclass Upload {private $ filepath = '. /upload'; // upload directory private $ tmpPath; // temporary PHP file directory private $ blobNum; // The number of file blocks private $ totalBlobNum; // The total number of file blocks private $ fileName; // file name public function _ construct ($ tmpPath, $ blobNum, $ totalBlobNum, $ fileName) {$ this-> tmpPath = $ tmpPath; $ this-> blobNum = $ blobNum; $ this-> totalBlobNum = $ totalBlobNum; $ this-> fileName = $ fileName; $ this-> moveFile (); $ this-> fileMerge ();} // Determine whether it is the last part. if yes, merge the file and delete the file block. private function fileMerge () {if ($ this-> blobNum ==$ this-> totalBlobNum) {$ blob = ''; for ($ I = 1; $ I <= $ this-> totalBlobNum; $ I ++) {$ blob. = file_get_contents ($ this-> filepath. '/'. $ this-> fileName. '__'. $ I);} file_put_contents ($ this-> filepath. '/'. $ this-> fileName, $ blob); $ this-> deleteFileBlob () ;}// delete a file block private function deleteFileBlob () {for ($ I = 1; $ I <= $ this-> totalBlo BNum; $ I ++) {@ unlink ($ this-> filepath. '/'. $ this-> fileName. '__'. $ I) ;}// move the file private function moveFile () {$ this-> touchDir (); $ filename = $ this-> filepath. '/'. $ this-> fileName. '__'. $ this-> blobNum; move_uploaded_file ($ this-> tmpPath, $ filename);} // API returns public function apiReturn () {if ($ this-> blobNum ==$ this-> totalBlobNum) {if (file_exists ($ this-> filepath. '/'. $ this-> fileName) {$ data ['code'] = 2; $ Data ['msg '] = 'success'; $ data ['file _ path'] = 'HTTP ://'. $ _ SERVER ['HTTP _ host']. dirname ($ _ SERVER ['document _ URI ']). str_replace ('. ', '', $ this-> filepath ). '/'. $ this-> fileName;} else {if (file_exists ($ this-> filepath. '/'. $ this-> fileName. '__'. $ this-> blobNum) {$ data ['code'] = 1; $ data ['msg '] = 'Waiting for all '; $ data ['file _ path'] = '';} header ('content-type: application/json'); echo json_encode ($ da Ta);} // create the upload folder private function touchDir () {if (! File_exists ($ this-> filepath) {return mkdir ($ this-> filepath );}}} // instantiate and obtain the system variable passing parameter $ upload = new Upload ($ _ FILES ['file'] ['tmp _ name'], $ _ POST ['blob _ num'], $ _ POST ['total _ blob_num '], $ _ POST ['file _ name']); // call a method, returned result $ upload-> apiReturn ();
Problems
This is just a simple DEMO, and there are many improvements to be made. For example, if the uploaded folder is put together with a temporary file, the user cancels it halfway and does not send a request to clean it, which may easily cause file redundancy. JS uses a synchronization model. A file needs to be uploaded in sequence. This will cause the entire browser to be blocked during the upload process. pressing the button may take several seconds to respond, poor user experience. When we really need to productize the product, we need to consider a variety of situations. Of course, as an example, it is good to guide everyone to understand the idea of multipart upload.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.