/** * 当个上传文件并返回上传的URL地址 * @param $filename string 要保存的文件名称 注意如果存在目录的话 * @param $filepath string 文件在服务器(本地的绝对或者相对路径,这里的服务器实际上是本地)上的路径 * @return bool or url */ public function upload2AliyunSdkSingle($filename, $filepath) { $dir = C('ALIYUN_DIR'); //保存在阿里云的基本路径 $endpoint = C('ALIYUN_endpoint'); //阿里云的节点信息 请自行查阅本字段意义 $midStr = 'upload/' . date('y') . '/' . date('m') . '/' . date('d') . '/'; //这里是为了保存的路径暂时以当前时间为准 try { $ossClient = new OssClient(C('ALIYUN_KeyId'), C('ALIYUN_KeySecret'), $endpoint); //这里要放入你从阿里云获得的key和secret这里我做成了配置项读取 } catch (OssException $e) { print $e->getErrorMessage(); } $ossClient->setTimeout(3600); // 设置请求超时时间 $ossClient->setConnectTimeout(10); //设置连接超时时间 try { $ossClient->uploadFile(C('ALIYUN_bucket'), $dir . $midStr . $filename, $filepath); } catch (OssException $e) { return false; } $ret = str_replace('http://', 'http://' . C('ALIYUN_bucket') . '.', $endpoint . '/' . $dir . $midStr . $filename); //返回拼接的http url return $ret; }
/** * Write using a local file path. * * @param string $path * @param string $localFilePath * @param Config $config Config object * @return array|false false on failure file meta data on success */ public function putFile($path, $localFilePath, Config $config) { $object = $this->applyPathPrefix($path); $options = $this->getOptionsFromConfig($config); $options[OssClient::OSS_CHECK_MD5] = true; if (!isset($options[OssClient::OSS_CONTENT_TYPE])) { $options[OssClient::OSS_CONTENT_TYPE] = Util::guessMimeType($path, ''); } try { $this->client->uploadFile($this->bucket, $object, $localFilePath, $options); } catch (OssException $e) { return false; } $type = 'file'; $result = compact('type', 'path'); $result['mimetype'] = $options[OssClient::OSS_CONTENT_TYPE]; return $result; }
} } WriteLog("进入服务"); if ($_SERVER['REQUEST_METHOD'] == 'POST') { WriteLog("\n上传开始时间:" . date('y-m-d h:i:s', time()) . "\n"); $insertvideo = new CommentVedio(); $backinfo = new Backinform(); $file = $_FILES["upfile"]; $filename = $file["tmp_name"]; $pinfo = pathinfo($file["name"]); $ftype = $pinfo['extension']; $Video = create_guid() . "." . $ftype; $object = "Ionic/Video/" . $Video; $bucket = "w376161501"; try { $ossClient->uploadFile($bucket, $object, $filename); $backinfo->kindbig = "OK"; } catch (OssException $e) { WriteLog("上传原图失败信息" . $e->getMessage()); $backinfo->kindbig = "NO"; } $resourceid = create_guid(); $mediaid = "DB5483ADCADC3ADC1E5CD2"; $linkid = test_input($_POST["id"]); $linktype = test_input($_POST["eventtype"]); WriteLog("连接ID:" . $linkid . "--连接类型:" . $linktype . "\n"); $resourcetype = "视频"; if ($insertvideo->Insert_Vedio($resourceid, $mediaid, $linktype, $linkid, $resourcetype, $object) == "OK") { $backinfo->kind = "OK"; $json = json_encode($backinfo); echo "{$json}";
$save_url .= $ymd . "/"; if (!file_exists($save_path)) { mkdir($save_path); } //新文件名 $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext; //移动文件 $file_path = $save_path . $new_file_name; if (move_uploaded_file($tmp_name, $file_path) === false) { alert("上传文件失败。"); } $object = "attached/" . $dir_name . "/" . $ymd . "/" . $new_file_name; $content = $file_path; $ossClient = new OssClient(OSS_ACCESS_ID, OSS_ACCESS_KEY, OSS_ENDPOINT, false); try { $ossClient->uploadFile($bucket, $object, $content); } catch (OssException $e) { print $e->getMessage(); } /*清空本地的存储文件及目录 unlink($file_path); rmdir("./tmp/".$dir_name."/".$ymd); rmdir("./tmp/".$dir_name); */ //完整的URL路径 $file_url = $urls . $save_url . $new_file_name; header('Content-type: text/html; charset=UTF-8'); $json = new Services_JSONs(); echo $json->encode(array('error' => 0, 'url' => $file_url)); exit; }
/** * 上传指定的本地文件内容 * * @param OssClient $ossClient OssClient实例 * @param string $bucket 存储空间名称 * @return null */ function uploadFile($ossClient, $bucket) { $object = "oss-php-sdk-test/upload-test-object-name.txt"; $filePath = __FILE__; $options = array(); try { $ossClient->uploadFile($bucket, $object, $filePath, $options); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print __FUNCTION__ . ": OK" . "\n"; }
<?php require_once './autoload.php'; use OSS\OssClient; use OSS\Core\OssException; $time = $_GET["time"]; $images = $_GET["images"]; $imagesarr = explode(",", $images); $imagescount = count($imagesarr); if ($imagescount >= 1) { $accesskeyid = "zqFGz1zqtNXd7e49"; $accesskeysecret = "QyMtSHSYO9nPXm8O26Ls5jtueoEBFv"; $endpoint = "oss-cn-qingdao.aliyuncs.com"; $bucketname = "jzsnet"; try { $ossClient = new OssClient($accesskeyid, $accesskeysecret, $endpoint); for ($i = 0; $i < $imagescount; $i++) { $realimage = $imagesarr[$i]; $realimagepath = __DIR__ . "/ueditor/php/upload/image/" . $time . "/" . $realimage; $ossClient->uploadFile($bucketname, $realimage, $realimagepath); } } catch (OssException $e) { print $e->getMessage(); } }