Home > Article > Backend Development > 有关CURL 将图片上传去远程服务器
我自己东拼西拼的弄了个功能
是把编辑器中有远程图片就下载到本地主机的一个小功能
正正常常的,好好的可以用,可是图片越来越多,需要把网页服务器 和 附件服务器分开保存
所以需要上传到远程的服务器中
本来我自动下载的类里面是用了最基本的fopen等等来保存,如下
$write_fd = fopen($pic_name,"wb"); fwrite($write_fd, $this->CurlGet($pic_item)); fclose($write_fd);
header('content-type:text/html;charset=utf8'); $curl = curl_init(); $data = array('img'=>'@'. $this->CurlGet($pic_item)); curl_setopt($curl, CURLOPT_URL, "http://xxxx.com/autodl.php?dir=$this->folder"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); $result = curl_exec($curl); curl_close($curl); echo json_decode($result);
<?phpheader("Access-Control-Allow-Origin: http://www.gamertb.com");header("Content-Type: applicaton/json"); if($_FILES) { // print_r($_FILES); $filename = $_FILES['img']['name']; $tmpname = $_FILES['img']['tmp_name']; $dir = $_GET['dir']; if(move_uploaded_file($tmpname, dirname(__FILE__).$dir.$filename)) { echo json_encode('上传成功'); } else { $data = json_encode($_FILES); echo $data; } }?>
整个自动下载的类
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 idprivate 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();
甚至放弃到想用ftp function 来完成,也没成效
Warning: ftp_put() expects parameter 3 to be a valid path, string given 这错误网上很少~也没找到确认答案到底是错什么
$host = 'ftp.XXXX.com';$usr = 'xxxx@xxxx.com';$pwd = 'xxxxxxx';$local_file=$this->CurlGet($pic_item);$ftp_path = '/'.$this->folder.$pic_name;$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");ftp_login($conn_id, $usr, $pwd) or die("Cannot login");$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);ftp_close($conn_id);
$data = array('img'=>'@'. $this->CurlGet($pic_item));
$this->CurlGet($pic_item) 返回的是什么?
$data = array('img'=>'@'. $this->CurlGet($pic_item));
$this->CurlGet($pic_item) 返回的是什么?
$data = array('img'=>'@'. $this->CurlGet($pic_item));
这里需要绝对路径的文件名
$data = array('img'=>'@'. $this->CurlGet($pic_item));
这里需要绝对路径的文件名
文件名和网址还是有区别的吧?
文件名和网址还是有区别的吧?
啊对了~版主我明白你的意思
你说我要加上类似这样的句子
$data = array('img'=>'@'. dirname(__FILE__).'/img/login.gif');
但问题是....
我在作操时根本还未有绝对路径
我原来的代码(整体) 在#1
63行开始~用CURL下载
所以我才会试用CURL的$this->CurlGet($pic_item)才填在这位置
如果我是不在自己主机上的话,等于远程再上传去远程的话我该如何是好?
你自己的服务器为 A,远程服务器为 B
那么 $this->CurlGet($pic_item) 取回的是 B 中的图片数据
需要保存成文件于 A,然后再 $data = array('img'=>'@'. '保存的文件名');
才可以通过 curl 上传
如果想直接发送数据可话,可见我 blog 中的另类做法 http://blog.csdn.net/xuzuning/article/details/7444709
图片需要使用绝对地址,不是网址
例如 获取当前目录的图片绝对地址,可以这样写: dirname(__FILE__).'/文件名';
谢谢两位版主的意见
我试了xuzuning大哥的blog中的那个做法
加入了在我的类中,试了好多次都不成功
我的案例是否建立个临时目录再上去会更简单?
在我的类中再加多一个FUNCTION(步骤)做 需要保存成文件于 A,然后再 $data = array('img'=>'@'. '保存的文件名'); ?
能否跟少少提示?