我创建了一个将编辑器远程图像自动下载到我的服务器的功能。
关于保存img部分
$write_fd = fopen($pic_name,"wb");
fwrite($write_fd, $this->CurlGet($pic_item));
fclose($write_fd);
现在我们有一个额外的服务器来保存我们网站的所有文件。
如何编辑保存图像代码以发送到其他服务器?
这里所有的PHP代码,
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);
}
}
打电话。
$folder = 'data/'.$id.'/';
$dlimg = new AutoImgDL($content,$folder);
$content = $dlimg -> output();
这些代码适用于本地服务器
如何保存到data.xxx.com(跨域)?
ftp或curl?
任何人都可以给我一些想法或指导吗?