File_get_contents: move a file like file: // to the server.

Source: Internet
Author: User
File_get_contents: move the file to the server. This post was last edited by molaifeng at 17:39:38, January 12 ,.

Use the ckeditor editor to copy the image in the word file and check that the source code of the image address is in the format of file: //. then I wrote a method to register an event during pasting, use a regular expression to match similar image addresses, and then asynchronously upload them to the php script file for processing. the processing logic is: read the image address using file_get_contents, and then write it to the specified directory using file_put_contents. because it is tested locally, no problem. Svn went to the server. after testing, we found that the image size was 0 KB. later, we used is_file for testing and printed false. The local size was true. Is it true that file_get_contents could not read the local image on the server.

// Save the image pasted from the word document to the local directory first, and then change its address to return the previous public function tranPic () {// detect $ _ POST = unflip ($ _ POST); $ content = strval ($ _ POST ['HTML ']); if (! $ Content) {echo ''; exit;} // domain name for storing images $ imgPath = C ('tmpl _ PARSE_STRING '); // path after image transfer $ pre = '.. /Uploads/'; $ pic = 'ueditor /'. date ('ymmd', time ()). '/'; mk_dir ($ pre. $ pic); // extract images with regular expressions $ pattern = "/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? :[\. Gif | \. jpeg | \. jpg | \. bmp | \. bnp Paribas | \. png | \. GIF | \. JPG | \. BMP | \. BNP Paribas | \. PNG | \. JPEG]) [\ '| \ "]. *? [\/]?> /"; Preg_match_all ($ pattern, $ content, $ match); // replace the image address with the address foreach in this project ($ match as $ key => & $ value) {if ($ key> 0) {continue;} foreach ($ value as $ k => $ v) {// get the image suffix, the first five characters of the main image are file: if (substr ($ match [1] [$ k], 0, 5) = 'File :') {$ ext = pathinfo ($ match [1] [$ k], PATHINFO_EXTENSION); $ data = file_get_contents ($ match [1] [$ k]); $ filename = uniqid (). '. '. $ ext; file_put_contents ($ pre. $ pic. $ filename, $ data); $ content = str_replace ($ v, str_replace ($ match [1] [$ k], $ imgPath ['.. /Uploads/']. $ pic. $ filename, $ v), $ content) ;}} echo $ content ;}


Reply to discussion (solution)

Yes, file_get_contents cannot read the local image on the server.
However, you can consider transferring data through a local server.

So, if you want to transfer data through a local server, give me a line of thinking, or do you have any other methods !!!

I used another method to upload a file using the curl post in php. I found that the file still cannot be uploaded. the error code is 26.

Try ajax post submission

Useless. I finally found that php could not read files starting with file: // on the server.

I just called it again and found that I could not read the local image on the server. it seems that this method is not good. is there any better way?

Related Article

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.