The thinkPHP5 framework integrates plupload to implement the image batch upload function,
This example describes how to integrate the thinkPHP5 framework with plupload to implement the image batch upload function. We will share this with you for your reference. The details are as follows:
Download plupload http: // http // www.plupload.com from the official website
Or click here to download.
Here we use pluploadQueue
Introduce the corresponding css and js on the HTML page, and then modify the sample code to your own code.
<Link rel = "stylesheet" href = "/assets/plupupload/css/jquery.plupload.queue.css" rel = "external nofollow" type = "text/css" media = "screen"/> <div class = "form-box-header">
Plupload integration:
<? Php/** File Upload ** Donald * 2017-3-21 */namespace app \ backend \ logic; use think \ Model; class Plupupload extends Model {public function upload_pic ($ file_type = "data "){#!! IMPORTANT :#!! This file is just an example, it doesn' t inmarshate any security checks and #!! Is not recommended to be used in production environment as it is. Be sure #!! Revise it and customize to your needs. // Make sure file is not cached (as it happens for example on iOS devices) header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header ("Last-Modified: ". gmdate ("D, d m y h: I: s "). "GMT"); header ("Cache-Control: no-store, no-cache, must-revalidate"); header ("Cache-Control: post-check = 0, pre-check = 0 ", false); header (" Pragma: no-cache ");/* // Support CORS heade R ("Access-Control-Allow-Origin: *"); // other CORS headers if any... if ($ _ SERVER ['request _ method'] = 'options') {exit; // finish preflight CORS requests here} * // 5 minutes execution time @ set_time_limit (5*60); // Uncomment this one to fake upload time // usleep (5000 ); // Settings // reset the upload path $ uploads = config ('uploads _ dir'); if (! Empty ($ file_type) {$ uploads = $ uploads. $ file_type. "/". date ("Ymd") ;}$ targetDir = $ uploads; // $ targetDir = 'uploads'; $ cleanupTargetDir = true; // Remove old files $ maxFileAge = 5*3600; // Temp file age in seconds // Create target dir if (! File_exists ($ targetDir) {@ mkdir ($ targetDir);} // Get a file name if (isset ($ _ REQUEST ["name"]) {$ fileName = $ _ REQUEST ["name"];} elseif (! Empty ($ _ FILES) {$ fileName = $ _ FILES ["file"] ["name"];} else {$ fileName = uniqid ("file _");} // rename the file $ fileName_arr = explode (". ", $ fileName); $ fileName = myrule (). ". ". $ fileName_arr [1]; // rule () Please refer to my previous blog thinkphp and upload multiple image files with duplicate names at the same time. $ filePath = $ targetDir. DIRECTORY_SEPARATOR. $ fileName; // Chunking might be enabled $ chunk = isset ($ _ REQUEST ["chunk"])? Intval ($ _ REQUEST ["chunk"]): 0; $ chunks = isset ($ _ REQUEST ["chunks"])? Intval ($ _ REQUEST ["chunks"]): 0; // Remove old temp files if ($ cleanupTargetDir) {if (! Is_dir ($ targetDir) |! $ Dir = opendir ($ targetDir) {die ('{"jsonrpc": "2.0", "error": {"code": 100, "message ": "Failed to open temp directory. "}," id ":" id "} ');} while ($ file = readdir ($ dir ))! = False) {$ tmpfilePath = $ targetDir. DIRECTORY_SEPARATOR. $ file; // If temp file is current file proceed to the next if ($ tmpfilePath = "{$ filePath }. part ") {continue;} // Remove temp file if it is older than the max age and is not the current file if (preg_match ('/\. part $/', $ file) & (filemtime ($ tmpfilePath) <time ()-$ maxFileAge) {@ unlink ($ tmpfilePath );}} closedir ($ dir);} // Open tem P file if (! $ Out = @ fopen ("{$ filePath}. part", $ chunks? "AB": "wb") {die ('{"jsonrpc": "2.0", "error": {"code": 102, "message ": "Failed to open output stream. "}," id ":" id "} ');} if (! Empty ($ _ FILES) {if ($ _ FILES ["file"] ["error"] |! Is_uploaded_file ($ _ FILES ["file"] ["tmp_name"]) {die ('{"jsonrpc": "2.0", "error": {"code ": 103, "message": "Failed to move uploaded file. "}," id ":" id "} ');} // Read binary input stream and append it to temp file if (! $ In = @ fopen ($ _ FILES ["file"] ["tmp_name"], "rb") {die ('{"jsonrpc": "2.0 ", "error": {"code": 101, "message": "Failed to open input stream. "}," id ":" id "} ') ;}} else {if (! $ In = @ fopen ("php: // input", "rb") {die ('{"jsonrpc": "2.0", "error ": {& quot; code & quot;: 101, & quot; message & quot;: & quot; Failed to open input stream. "}," id ":" id "} ') ;}} while ($ buff = fread ($ in, 4096) {fwrite ($ out, $ buff );} @ fclose ($ out); @ fclose ($ in); // Check if file has been uploaded if (! $ Chunks | $ chunk ===$ chunks-1) {// Strip the temp. part suffix off rename ("{$ filePath }. part ", $ filePath);} // Return Success JSON-RPC response die ($ filePath); // Return the result directly here // die ('{" jsonrpc ":" 2.0 ", "result ":"'. $ filePath. '"," id ":" id "}');}}
Finally, the Controller or Model gets the result and saves it.
$ Images = $ request-> post ('images/A'); // You must note that thinkphp cannot obtain data when retrieving the post array by name, add/a after name to obtain the array. For details, see typeCastmodel ('photoimage')-> query_insert ($ images, $ id) of the Request. // insert images in batches.
/*** Forced type conversion * @ param string $ data * @ param string $ type * @ return mixed */private function typeCast (& $ data, $ type) {switch (strtolower ($ type) {// array case 'A': $ data = (array) $ data; break; // number case 'D ': $ data = (int) $ data; break; // floating point case 'F': $ data = (float) $ data; break; // Boolean case 'B ': $ data = (boolean) $ data; break; // string case's ': default: if (is_scalar ($ data) {$ data = (string) $ data ;} else {throw new \ InvalidArgumentException ('variable type error :'. gettype ($ data ));}}}