Effect:
、
Project Structure diagram:
Wangeditor-upload-img.html Code:
Upload.php Code:
<?php$targetdir = ' upload_tmp '; $uploadDir = ' upload '; $cleanupTargetDir = true; if (!file_exists ($targetDir)) {@mkdir ($targetDir);} if (!file_exists ($uploadDir)) {@mkdir ($uploadDir);} if (Isset ($_request["name")) {$fileName = $_request["name"];} elseif (!empty ($_files)) {$fileName = $_files["file"] [" Name "];} else {$fileName = uniqid ("File_");} $fileName = Iconv (' UTF-8 ', ' GB2312 ', $fileName); $filePath = $targetDir. Directory_separator. $fileName; $uploadPath = $uploadDir. Directory_separator. $fileName; $imgUrl = "http://localhost:8088/FileUpload/". $uploadDir. " /". $fileName; echo $imgUrl; $chunk = isset ($_request[" chunk "])? Intval ($_request["Chunk"]): 0; $chunks = isset ($_request["chunks"])? Intval ($_request["chunks"]): 1;if ($cleanupTargetDir) {if (!is_dir ($targetDir) | |! $dir = opendir ($targetDir)) {die (' {')} Jsonrpc ":" 2.0 "," error ": {" code ": +," message ":" Failed to open Temp directory. "}," id ":" id "} '); while (($file = Readdir ($dir))!== false) {$tmpfilePath = $targetDir. DIRECTOry_separator. $file, if ($tmpfilePath = = "{$filePath}_{$chunk}.part" | | $tmpfilePath = = "{$filePath}_{$chunk}.parttmp") {continue;} if (Preg_match ('/\. part|parttmp) $/', $file) && (@filemtime ($tmpfilePath) < time ()-$maxFileAge)) {@unlink ($tmpfilePath);}} Closedir ($dir);} if (! $out = @fopen ("{$filePath}_{$chunk}.parttmp", "WB") {die (' {"JSONRPC": "2.0", "error": {"code": 102, "message": "Fa iled 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 "} '); 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": {"code": 101, "message": "Failed to open Input stream. "}," id ":" id "} ');}} while ($buff = Fread ($in, 4096)) {fwrite ($out, $buff);} @fclose ($out); @fclose ($in); Rename ("{$filePath}_{$chunk}.parttmp", "{$filePath}_{$chunk}.part"); $index = 0; $done = True;for ($index = 0; $index < $chunks; $index + +) {if (!file_exists ("{$filePath}_{$index}.part") {$done = False;bre AK;}} if ($done) {if (! $out = @fopen ($uploadPath, "WB")) {die (' {"JSONRPC": "2.0", "error": {"code": 102, "message": "Failed To open output stream. "}," id ":" id "} ');} if (Flock ($out, lock_ex)) {for ($index = 0; $index < $chunks; $index + +) {if (! $in = @fopen ("{$filePath}_{$index}.par T "," RB ")) {break;} while ($buff = Fread ($in, 4096)) {fwrite ($out, $buff);} @fclose ($in); @unlink ("{$filePath}_{$index}.part");} Flock ($out, lock_un);} @fclose ($out);}
Original video and source code: http://www.kancloud.cn/wangfupeng/wangeditor/65747
Rich Text Editor-wangeditor upload images