/** * 上传图片到PCS * @param 图片路径 $file * @param 图片名称【用户id+时间串】 $fileName */ public function upLoadPic($tmp_name, $fileName) { $rel = false; //引入pcs操作类 require_once '../../../util/pcs/libs/BaiduPCS.class.php'; require_once '../../../util/StackConst.php'; //请根据实际情况更新$access_token与$appName参数 $access_token = StackConst::access_token(); //应用目录名"/apps/stack/shares/picture/" $targetPath = StackConst::pcs_pic_url(); $pcs = new BaiduPCS($access_token); //新文件名默认给空 $newFileName = ""; $result = $pcs->upload(file_get_contents($tmp_name), $targetPath, $fileName, $newFileName); $flist = json_decode($result); if (isset($flist)) { $rel = $flist > 0 ? true : false; } //成功返回true/失败返回false return $rel; }
function upload_baidupcs($zip_file_path) { $file_blocks = array(); //分片上传文件成功后返回的md5值数组集合 $file_block_size = 2 * 1024 * 1024; // 2M $rmote_dir = str_replace('//', '/', REMOTE_ROOT . '/backup/'); $file_name = basename($zip_file_path); $BaiduPCS = new BaiduPCS(BAIDU_TOKEN); // 开始分片上传 $handle = @fopen($zip_file_path, 'rb'); while (!@feof($handle)) { $file_block_content = fread($handle, $file_block_size); $temp = $BaiduPCS->upload($file_block_content, $remote_dir, $file_name, false, true); if (!is_array($temp)) { $temp = json_decode($temp, true); } if (isset($temp['md5'])) { array_push($file_blocks, $temp['md5']); } } fclose($handle); //@unlink($zip_file_path); if (count($file_blocks) > 1) { $BaiduPCS->createSuperFile($remote_dir, $file_name, $file_blocks, ''); } }
<?php require_once '../libs/BaiduPCS.class.php'; //请根据实际情况更新$access_token与$appName参数 $access_token = '3.839af46f54db6ed60797847d2febbca0.2592000.1359262544.754976761-248414'; //应用目录名 $appName = '测试应用'; //应用根目录 $root_dir = '/apps' . '/' . $appName . '/'; //上传文件的目标保存路径,此处表示保存到应用根目录下 $targetPath = $root_dir; //要上传的本地文件路径 $file = dirname(__FILE__) . '/' . 'yun.jpg'; //文件名称 $fileName = basename($file); //新文件名,为空表示使用原有文件名 $newFileName = ''; $pcs = new BaiduPCS($access_token); if (!file_exists($file)) { exit('文件不存在,请检查路径是否正确'); } else { $fileSize = filesize($file); $handle = fopen($file, 'rb'); $fileContent = fread($handle, $fileSize); $result = $pcs->upload($fileContent, $targetPath, $fileName, $newFileName); fclose($handle); echo $result; }
$pcs = new BaiduPCS($access_token); if (!file_exists($file)) { exit('文件不存在,请检查路径是否正确'); } else { $fileSize = filesize($file); $handle = fopen($file, 'rb'); //分片上传文件成功后返回的md5值数组集合 $filesBlock = array(); //设置分片上传文件块大小为20K $blockSize = 20480; if ($fileSize < $blockSize) { exit('请选择体积大于20480个字节的文件'); } $isCreateSuperFile = TRUE; while (!feof($handle)) { $temp = $pcs->upload(fread($handle, $blockSize), $targetPath, $fileName, $newFileName, $isCreateSuperFile); if (!is_array($temp)) { $temp = json_decode($temp, true); } array_push($filesBlock, $temp); } fclose($handle); if (count($filesBlock) > 1) { $params = array(); foreach ($filesBlock as $value) { array_push($params, $value['md5']); } $result = $pcs->createSuperFile($targetPath, $fileName, $params, $newFileName); echo $result; } }