PHP 上传ftp路径文件到外网服务器上 curl base64图片

需求是将ftp路径的图片上传到服务器上。百度的答案太水了。层次不齐。自己写了。

ftp路径文件是局域网下的电子设备返回的。由于外网不能访问局域网的图片,所以要将图片上传到服务器~~~~

先将ftp图片下载到本地,然后本地上传到服务器

* $ftppath ftp文件路径
* $filename 文件原始名
* $path 文件本地保存路径
if(!is_dir($path)){
    mkdir($path,0777,true);
}
$path=$path."/".$filename;//文件保存路径
$mao=file_get_contents($ftppath);//ftp到本地
file_put_contents($path,$mao);
uploadimagetoserver($path,$filename,$timepath);//本地文件上传服务器

 

public function uploadimagetoserver($imgpath,$filename,$timepath){
    $yuming=$this->yuming;//根目录
    if(empty($imgpath) || empty($filename))return false;
    if(empty($timepath))$timepath=date("Ymd");

    $base64_img = $this->base64EncodeImage($imgpath);//调用转码
    $url = $yuming."/rest?method=uploadimg";//外网接口
    header('Content-type:text/html; charset=utf-8'); //声明编码
    $ch = curl_init();
    $curlPost = array('imgBase64'=>$base64_img,'filename'=>$filename,'timepath'=>$timepath);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$curlPost);
    $data =curl_exec($ch);
    curl_close($ch);
}

 

//转码

function base64EncodeImage ($image_file) {
    $base64_image = '';
    $image_info = getimagesize($image_file);
    $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
    $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
    return $base64_image;
}

 

 

//外网接口 保存图片到服务器

$base64_image_content=$_POST['imgBase64'];//base64图片
$filename=$_POST['filename'];//文件名
$timepath=$_POST['timepath'];//路径
$base64_body = substr(strstr($base64_image_content,','),1);
$data= base64_decode($base64_body);//重点

$url="./upload/uface/".$timepath."/".$filename;//服务器保存路径
$res=file_put_contents($url,$data);//重点

尝试过n次PHP post curl上传都是失败的。base64成功。!!!网上的解决办法太水了。气人!

不懂得可以问我。不喜欢勿喷

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值