/** * 上传函数 * @param $object * @param $file * @param $opt * @return bool */ function _file_upload($object, $file, $opt = array()) { //设置超时时间 //set_time_limit(120); //如果文件不存在,直接返回FALSE if (!@file_exists($file)) { return FALSE; } //获取WP配置信息 $cos_options = get_option('cos_options', TRUE); $cos_bucket = esc_attr($cos_options['bucket']); if (@file_exists($file)) { try { //实例化存储对象 $qcloud_cos = new Cosapi(); $dirname = dirname($object); _create_folder($cos_bucket, $dirname); $data = $qcloud_cos->upload($file, $cos_bucket, $object); return TRUE; } catch (Exception $ex) { return FALSE; } } else { return FALSE; } }
use Qcloud_cos\Auth; use Qcloud_cos\Cosapi; use Qcloud_cos\CosDb; $bucketName = 'test'; $srcPath = './test.log'; $dstPath = '/sdk/test.log'; $dstFolder = '/sdk/'; Cosapi::setTimeout(180); //创建文件夹 $createFolderRet = Cosapi::createFolder($bucketName, $dstFolder); var_dump($createFolderRet); //上传文件 $bizAttr = ""; $insertOnly = 0; $sliceSize = 3 * 1024 * 1024; $uploadRet = Cosapi::upload($bucketName, $srcPath, $dstPath, $bizAttr, $sliceSize, $insertOnly); var_dump($uploadRet); //目录列表 $listnum = 20; $pattern = "eListBoth"; $order = 0; $listRet = Cosapi::listFolder($bucketName, $dstFolder, $listnum, $pattern, $order); var_dump($listRet); //更新目录信息 $bizAttr = ""; $updateRet = Cosapi::updateFolder($bucketName, $dstFolder, $bizAttr); var_dump($updateRet); //更新文件信息 $bizAttr = ""; $authority = "eWPrivateRPublic"; $customer_headers_array = array('Cache-Control' => "no", 'Content-Type' => "application/pdf", 'Content-Language' => "ch");
exit("0"); } } $files = time(); //传到COS $base64_body = substr(strstr($base64_image_content, ','), 1); //生成本地文件 $new_file = "./tmp/" . $files . ".png"; $data = base64_decode($base64_body); file_put_contents($new_file, $data); $cosfolder = Cosapi::statFolder($bucketName, $faceFolder); if ($cosfolder["code"] != "0") { Cosapi::createFolder($bucketName, $faceFolder); } //要传入腾讯云文件 $dstPath = $faceFolder . $files . ".png"; //查询文件是否存在 $result = Cosapi::stat($bucketName, $dstPath); if ($result["code"] == "0") { Cosapi::del($bucketName, $dstPath); $types = Cosapi::upload($new_file, $bucketName, $dstPath); } else { $types = Cosapi::upload($new_file, $bucketName, $dstPath); } /*清空本地的存储文件及目录 unlink($save_path); */ $file_url = $types["data"]["access_url"]; session_start(); $_SESSION['face'] = $file_url; echo "1";
//移动文件 $file_path = $save_path . $new_file_name; if (move_uploaded_file($tmp_name, $file_path) === false) { alert("上传文件失败。"); } //本地文件 $srcPath = $file_path; //要传入腾讯云文件 $dstPath = "/attached/" . $dir_name . "/" . $ymd . "/" . $new_file_name; //查询文件是否存在 $result = Cosapi::stat($bucketName, $dstPath); if ($result["code"] == "0") { Cosapi::del($bucketName, $dstPath); $types = Cosapi::upload($srcPath, $bucketName, $dstPath); } else { $types = Cosapi::upload($srcPath, $bucketName, $dstPath); } /*清空本地的存储文件及目录 unlink($file_path); rmdir("./tmp/".$dir_name."/".$ymd); rmdir("./tmp/".$dir_name); */ //完整的URL路径 $file_url = $types["data"]["access_url"]; header('Content-type: text/html; charset=UTF-8'); $json = new Services_JSONs(); echo $json->encode(array('error' => 0, 'url' => $file_url)); exit; } function alert($msg) {