图片迁移

1. tp5 上传图片

$info = $file->move('文件路径');

2. 上传成功之后调用方法迁移图片(迁移方法在目的地服务器):

$url = 'http://destination.com/psms/upload/uploadImg?path=要保存图片的文件夹目录';
$data = array( 'file' => new \CURLFile(realpath('图片路径 + 名称')) );
$ch = curl_init();
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT,"TEST");
$result = curl_exec($ch);
// $error = curl_error($ch);
curl_close($ch); // 关闭CURL会话
if($result){
   $return = 1;
}

3. 接收图片方法:

/**
     * Notes    : 添加单张图片
     * User     : SuLi
     * DateTime : 2018/6/8 16:48
     * @return int
     */
    public function uploadImg(){
        $path = $this->request->param('path'); // 接收 GET 方式传递过来的数据
        // file_put_contents('json/'.time().".json", json_encode($_FILES));    // 把指定的内容写入指定的文件

        $tmp_name = $_FILES['file']['tmp_name'];
        $name = $_FILES['file']['name'];

        is_dir($path) OR mkdir($path, 0777, true);

        $return = move_uploaded_file($tmp_name, $path.'/'.$name);
        // $return = copy($tmp_name, 'audit/'.$name);
        if($return){
            return 1;
        }else{
            return 0;
        }

    }


/**
     * Notes    : 添加多张图片(未用到)
     * User     : SuLi
     * DateTime : 2018/6/8 16:48
     * @return int
     */
    public function uploadAllImg(){
        $time = time();
        $path = $this->request->param('path'); // 接收 GET 方式传递过来的数据
        // file_put_contents($time.".json", $path);
        // file_put_contents('json/'.$time.".json", json_encode($_FILES));    // 把指定的内容写入指定的文件

        $tmp_name = $_FILES['file']['tmp_name'];
        $name = $_FILES['file']['name'];

        // $dir = './'.$path;
        // is_dir($dir) OR mkdir($dir, 0777, true);
        is_dir($path) OR mkdir($path, 0777, true);

        $i = 0;
        foreach ($name as $key=>$val) {
            $return = move_uploaded_file($tmp_name[$i], $path.'/'.$name[$i]);
            if($return){
                $i++;
            }else{
                break;
            }
            unset($return);
        }
        // $return = copy($tmp_name, 'audit/'.$name);
        if($i == count($name)){
            // file_put_contents('json/'.$time."_success.json", 'OK!');
            return 1;
        }else{
            // 删除已经保存的图片
            // file_put_contents('json/'.$time."_error.json", 'NO!');
            return 0;
        }

    }

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值