File_get_contents move a file like file: // file to the server. how can this problem be solved?

Source: Internet
Author: User
File_get_contents: move the file to the server & nbsp; use the ckeditor editor to copy the image in the word file and check that the source code of the image address is file: in this form, I wrote a method to register an event during pasting, use a regular expression to match similar image addresses, and then asynchronously upload it to the php script file for processing. the processing logic is, use file_get _ file_get_contents to move a file similar to file: // to the server.
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 a local directory, and then change its address to return
Public function tranPic (){

// Detection
$ _ POST = unflip ($ _ POST );
$ Content = strval ($ _ POST ['HTML ']);
If (! $ Content ){
Echo '';
Exit;
}

// Domain name for storing images
$ ImgPath = C ('tmpl _ PARSE_STRING ');

// The path after the image is saved
$ Pre = '../Uploads /';
$ Pic = 'ueditor/'. date ('ymd', time ()).'/';
Mk_dir ($ pre. $ pic );

// Extract the image using 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 in this project.
Foreach ($ match as $ key =>&$ value ){

If ($ key> 0 ){
Continue;
}

Foreach ($ value as $ k => $ v ){

// Obtain 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;
}

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

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.