This article introduces the content is about js+php multipart upload large files, has a certain reference value, now share to everyone, have the need for friends can refer to
1. Understanding part
Why can't the server send large files directly? It's about a couple of configurations inside php.ini.
upload_max_filesize = 2M//php maximum acceptable file size Post_max_size = 8M//php Maximum post value to receive ' Memory_limit = 128M//Memory Cap Max_execution_ Time = 30//Maximum execution times
Of course, you can not simply rough the above several values, or server memory resources to eat up is a matter of sooner or later.
Solution Ideas
Fortunately, HTML5 opened the new file API, can also directly manipulate binary objects, we can directly in the browser to achieve file cutting, according to the previous approach to use the Flash scheme, the implementation will be a lot of trouble.
JS Ideas
1. Listen for the onchange event of the upload button
2. Get file objects for files
3. Cut the file object and attach it to the Formdata object
4. Sending the Formdata object to the server via Ajax
5. Repeat steps 3 and 4 until the file is sent out.
PHP Ideas
1. Create an Upload folder
2. Move files from the upload temp directory to the Upload folder
3. After all the file blocks have been uploaded, file synthesis is done.
4. Delete a folder
5. Return the file path after uploading
2.html part
<!doctype html>
3.php section
<?phpclass upload{Private $filepath = './upload ';//upload directory private $tmpPath; PHP File temp directory private $blobNum; The first few files block private $totalBlobNum; 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 if it is the last piece, if it is a file composition and delete the file block private function Filemerge () {if ($this->blobnum = = $this->totalblob Num) {$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, file_append); } $this->deletefileblob (); }}//delete file block Private function DeletEfileblob () {for ($i =1; $i <= $this->totalblobnum; $i + +) {@unlink ($this->filepath. ' /'. $this->filename. ' __ '. $i); }}//Move file Private Function MoveFile () {$this->touchdir (); $filename = $this->filepath. ' /'. $this->filename. ' __ '. $this->blobnum; Move_uploaded_file ($this->tmppath, $filename); //API returns data 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 ($data); }//Create an Upload folder Private function Touchdir () {if (!file_exists ($this->filepath)) {return mkdir ($t His->filepath); }}}//instantiates and obtains system variable parameters $upload = new Upload ($_files[' file '] [' tmp_name '],$_post[' blob_num '],$_post[' total_blob_num '],$_ post[' file_name ');//Call method, return result $upload->apireturn ();
3. Problems with PHP.ini configuration encountered during debugging
3.1
Post_max_size = 10M:
3.2 max_file_uploads:20
3.3
memory_limit:128m; (Explanation: Maximum memory consumed by PHP pages)