0

I create a function to auto download the editor remote images to my server.

about save img part

$write_fd = fopen($pic_name,"wb");  
fwrite($write_fd, $this->CurlGet($pic_item));  
fclose($write_fd);

Now we have a additional server for save all files for our site.

How to edit my save images code to send to other server?

all php code here,

class AutoImgDL {

private $content;
private $folder;
private $home_url;
private $loacl_host;

public function __construct($content,$folder,$dir_level="../../"){
    $this->content = $content;
    $this->folder = $folder;
    $this->dir_level = $dir_level;
    $this->loacl_host = "abc.com";
    $this->get_unique = $this->get_unique();
    $this->home_url = 'http://data.abc.com/'.$this->folder;
}

//create microtime to set unique id
private function get_unique(){  
    list($msec, $sec) = explode(" ",microtime());  
    return $sec.intval($msec*1000000);  
} 

private function get_pic($content) {
    $pattern_src = '/< *img[^>]*src *= *["\']?([^"\']*)/i';  
    $num = preg_match_all($pattern_src, $content, $match_src);  
    //get all images
    $arr_src_remote=$match_src[1];
    //img type
    $pattern_type = '/(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png)/'; 

    //foreach images file name   123456789.img_type
    $arr = "";
    foreach($arr_src_remote as $pic_item){

        preg_match("/^(http:\/\/)?([^\/]+)/i",$pic_item, $matches);
        $host = $matches[2];
        preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
        if ( $matches[0] != $this->loacl_host ) {               
            $num = preg_match_all($pattern_type, $pic_item, $match_type);  
            $unique_name = $this->get_unique().$match_type[1][0];
            $pic_name = $this->dir_level.$this->folder.$unique_name;
            $img_url = $this->home_url.$unique_name;
            //save images
            $write_fd = fopen($pic_name,"wb");  
            fwrite($write_fd, $this->CurlGet($pic_item));  
            fclose($write_fd);

        }else{
            $img_url = $pic_item;
        }
        $arr .= $img_url.'-';
    }

    //chage img new url
    $new_arr = explode('-',$arr);
    array_pop($new_arr);

    return $op = str_replace($arr_src_remote,$new_arr,$content);

}    

// CURL  
private function CurlGet($url){
    $url=str_replace('&','&',$url);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);  
    curl_setopt($curl, CURLOPT_HEADER, false);   
    curl_setopt($curl, CURLOPT_REFERER,$url);  
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2)");  
    curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');  
    curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);  
    $values = curl_exec($curl);  
    curl_close($curl);  
    return $values;  
}

// output and run 
public function output(){
    return $this->get_pic($this->content);
}

}

call it.

    $folder = 'data/'.$id.'/';
    $dlimg = new AutoImgDL($content,$folder);
    $content = $dlimg -> output();

These code is work for down to local server

how to save to data.xxx.com (cross-domain)?

ftp or curl?

anybody can give me some idea or guide?

Sky
  • 117
  • 1
  • 8
  • 1
    Well, you would need to interface with some kind of upload service on the remote server. If that server is running FTP, that is your solution right there. Maybe it is running some kind of HTTP upload service, or SSH. If it is not, you need to get access to server by some other means. Ask the administrator of that server how to best solve the problem. – demonkoryu Apr 13 '16 at 21:01
  • @demonkoryu thanks. I had using a jquery+html5 upload to remote is work! on the file server php just use $_POST for receive. and header("Access-Control-Allow-Origin: http://www.example.com"); How can I do for this class? – Sky Apr 13 '16 at 21:06
  • If I understood you correctly, you need to POST the files to that remote server. You can find a solution for that here: http://stackoverflow.com/a/15200804/177002 Also, read the comments to that answer. – demonkoryu Apr 14 '16 at 09:42

0 Answers0