About UEditor editor remote image upload failed to resolve the solution

  • 2020-05-19 04:25:03
  • OfStack

Remote image uploading is a very interesting thing. For example, if you copy a file from another website, if there is a picture in the text, the editor will automatically extract the picture and upload it, so you don't have to worry about the remote image being invalid and you can't view it locally.

Upon inspection, it was found that the operation page of remote image upload was: getRemoteImage.php. After opening it, we first configured savePath, because different users need to store it in different directories to avoid confusion and facilitate management

Modified code:

 
// Remote capture image configuration  
if(isset($_SESSION['admin'])){ 
$myPath = '//www.ofstack.com/../dofiles/ueditorUpload/admin/'.$_SESSION['admin']['id'].'/'; 
}else if(isset($_SESSION['user'])){ 
$myPath = '//www.ofstack.com/../dofiles/ueditorUpload/user/'.$_SESSION['user']['id'].'/'; 
}else{ 
$myPath = '//www.ofstack.com/../dofiles/ueditorUpload/unkonw/'; 
} 
$config = array( 
"savePath" => $myPath , // Save the path  
"allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" ) , // File allowed format  
"maxSize" => 3000 // File size limits, units KB 
); 


Then there is the problem. In UEditor, files and pictures are uploaded through Uploader.class.php, which is the php class, but remote pictures are not.

I found in line 85 that when creating the path, I simply used mkdir to create it, because mkdir could not create a path with hierarchy, so it failed to copy the remote image upload if the path did not exist.

Knowing the problem is easy to deal with, I will write a function to create a file directory in a loop (since I wrote it before, I will use it directly here) :

 
// Continuously create hierarchical folders  
function recursive_mkdir($folder){ 
$folder = preg_split( "/[\\\\\/]/" , $folder ); 
$mkfolder = ''; 
for($i=0; isset($folder[$i]); $i++){ 
if(!strlen(trim($folder[$i]))){ 
continue; 
} 
$mkfolder .= $folder[$i]; 
if(!is_dir($mkfolder)){ 
mkdir("$mkfolder",0777); 
} 
$mkfolder .= DIRECTORY_SEPARATOR; 
} 
} 

Then modify line 85:
 
// Create a save location  
$savePath = $config[ 'savePath' ]; 
if ( !file_exists( $savePath ) ) { 
recursive_mkdir($savePath); 
//mkdir( "$savePath" , 0777 ); 
} 

So if I do 1, I'm done.

This problem has also been submitted to baidu official, hope to correct.

The test version of UEditor is 1.2.3.0. If there are some problems with the previous version, we can modify it according to the modification idea.

Related articles: