Rich Text Editor-wangeditor upload images

Source: Internet
Author: User
Tags flock fread try catch

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.