copy() public méthode

给资源进行重命名,本质为move操作。
public copy ( $from_bucket, $from_key, $to_bucket, $to_key, $force = false ) : mixed
$from_bucket 待操作资源所在空间
$from_key 待操作资源文件名
$to_bucket 目标资源空间名
$to_key 目标资源文件名
Résultat mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
 /**
  * 复制文件到另外一个位置
  *
  * @param $fromBucket
  * @param $fromFile
  * @param $toBucket
  * @param $toFile
  * @return \Qiniu\Storage\成功返回NULL|string
  */
 public function copy($fromBucket, $fromFile, $toBucket, $toFile)
 {
     $bucketManager = new BucketManager($this->auth);
     $error = $bucketManager->copy($fromBucket, $fromFile, $toBucket, $toFile);
     if ($error !== null) {
         return $error;
     } else {
         return "Success";
     }
 }
Exemple #2
0
//初始化BucketManager
$bucketMgr = new BucketManager($auth);
//你要测试的空间, 并且这个key在你空间中存在
$bucket = 'Bucket_Name';
$key = 'php-logo.png';
//获取文件的状态信息
list($ret, $err) = $bucketMgr->stat($bucket, $key);
echo "\n====> {$key} stat : \n";
if ($err !== null) {
    var_dump($err);
} else {
    var_dump($ret);
}
//将文件从文件$key 复制到文件$key2。 可以在不同bucket复制
$key2 = 'php-logo2.png';
$err = $bucketMgr->copy($bucket, $key, $bucket, $key2);
echo "\n====> copy {$key} to {$key2} : \n";
if ($err !== null) {
    var_dump($err);
} else {
    echo "Success!";
}
//将文件从文件$key2 移动到文件$key3。 可以在不同bucket移动
$key3 = 'php-logo3.png';
$err = $bucketMgr->move($bucket, $key2, $bucket, $key3);
echo "\n====> move {$key2} to {$key3} : \n";
if ($err !== null) {
    var_dump($err);
} else {
    echo "Success!";
}
 public function copy($path, $newpath)
 {
     $r = $this->bucketManager->copy($this->bucket, $path, $this->bucket, $newpath);
     return is_null($r);
 }
 /**
  * 复制海报图片到指定款号下
  **/
 function actionCopyPosterImg()
 {
     $ret = array('status' => 0, 'mag' => '');
     try {
         $brandnumber = trim(Yii::app()->request->getParam("brandnumber"));
         $img_type = Yii::app()->request->getParam("img_type");
         $id = Yii::app()->request->getParam("id");
         if (empty($brandnumber) || empty($img_type) || empty($id)) {
             throw new Exception('传参失败');
         }
         //查询当前款号是否存在
         $brandnumberorder = new brandnumberorder();
         $brandnumber_ret = $brandnumberorder->select_all_brandnumber($_SESSION['brandid'], array('brandnumber' => $brandnumber));
         if (count($brandnumber_ret) == 0) {
             throw new Exception('款号不存在');
         }
         //获取品牌 SKU 规则
         $brand_ret = Brand::brandSelectById($_SESSION['brandid']);
         if (count($brand_ret) == 0) {
             throw new Exception('');
         }
         $code_start = $brand_ret[0]['code_start'];
         $code_end = $brand_ret[0]['code_end'];
         $SKU = $brandnumber;
         if ($code_start != 0 || $code_end != 0) {
             //设置了款号规则后 按规则提取款号
             $SKU = $this->SKCToSKU($code_start, $code_end, $brandnumber);
         }
         //获取订单名
         $orderobj = Yii::app()->db->createCommand()->select('ordername')->from('erp_order')->where("id=" . $brandnumber_ret[0]['orderid'])->queryRow();
         //拼接图片的路径
         $img_src = 'orderlist/' . $orderobj['ordername'] . '/' . $SKU . '/' . $brandnumber;
         $qiniu = new erp_qiniu();
         //七牛的接口类
         $brand_qiniu_account = $qiniu->getAccountByBrand($_SESSION['brandid']);
         //获取品牌的七牛子账号信息
         if (count($brand_qiniu_account) == 0) {
             throw new Exception('品牌暂未绑定七牛帐号');
         }
         $img_type_arr = explode(',', $img_type);
         $poster = new posterclass();
         //获取品牌勾选的图片
         $poster_ret = $poster->select_all_poster($_SESSION['brandid'], -1, array('id' => explode(',', $id)));
         if ($poster_ret['page_sum'] > 0) {
             $clothesorder = new clothesorderclass();
             $clothesimage = new clothesimageclass();
             //获取品牌的七牛权限
             $atch = new Auth($brand_qiniu_account[0]['access_key'], $brand_qiniu_account[0]['secret_key']);
             //创建七牛资源类
             $Bucket = new BucketManager($atch);
             $clothes_ret = $clothesorder->select_Single_clothes($_SESSION['brandid'], array('brandnumber' => $brandnumber));
             //查看衣服是否已添加
             $insert_id = 0;
             if (empty($clothes_ret)) {
                 //衣服没有添加 就新增加
                 $insert_id = $clothesorder->create_erp_clothes($SKU, $brandnumber, $brandnumber_ret[0]['orderid'], $_SESSION['brandid']);
             } else {
                 $insert_id = $clothes_ret['id'];
             }
             if (empty($insert_id)) {
                 throw new Exception('海报图复制失败');
             }
             foreach ($img_type_arr as $img_value) {
                 $new_img_src = $img_src;
                 $type = 0;
                 if ($img_value == 'model_img') {
                     $new_img_src .= '/model/';
                     $type = 3;
                 } else {
                     if ($img_value == 'product_img') {
                         $new_img_src .= '/product/';
                         $type = 0;
                     } else {
                         if ($img_value == 'detaile_img') {
                             $new_img_src .= '/details/';
                             $type = 4;
                         } else {
                             continue;
                         }
                     }
                 }
                 foreach ($poster_ret['data'] as $value) {
                     $url_arr = explode('/', $value['url']);
                     //查看图片是否存在
                     $img_ret = $clothesimage->select_all_image($insert_id, -1, array('url' => $new_img_src . $url_arr[2]));
                     if (count($img_ret) > 0) {
                         //图片存在 修改
                         $clothesimage->upload_imageinfo($insert_id, $img_ret[0]->id, array('isshow' => 0));
                     } else {
                         //图片不存在 添加
                         //修改图片路径
                         $Bucket_ret = $Bucket->copy($brand_qiniu_account[0]['space'], $value['url'], $brand_qiniu_account[0]['space'], $new_img_src . $url_arr[2]);
                         if (empty($Bucket_ret)) {
                             //没有返回值 表示图片复制成功 将数据添加到衣服图片库里
                             $clothesimage->create_clothes_img($url_arr[2], $type, $new_img_src . $url_arr[2], $insert_id, $brand_qiniu_account[0]['space']);
                         } else {
                             throw new Exception('图片复制失败');
                         }
                     }
                 }
             }
         }
         $ret['status'] = 1;
     } catch (Exception $e) {
         $ret['msg'] = $e->getMessage();
     }
     echo json_encode($ret);
 }