不用加载官方那一大坨sdk了,简单一点
看过网上的一些例子都过于复杂,包含了官方的phpsdk那一大坨东西,理论上我只需要简单的上传文件而已,不需要那么多东西,于是自己写了一下。
以下代码保存到/yourpath/ThinkPHP/Library/Think/Upload/Driver/Oss.class.php即可<?php<br />
<br />
// ----------------------------------------------------------------------<br />
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]<br />
// ----------------------------------------------------------------------<br />
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.<br />
// ----------------------------------------------------------------------<br />
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )<br />
// ----------------------------------------------------------------------<br />
// | Author: 废墟 <r.anerg@gmail.com> <http://anerg.com><br>
// ----------------------------------------------------------------------<br>
<br>
namespace ThinkUploadDriver;<br>
<br>
class Oss {<br>
<br>
const OSS_HOST = 'oss.aliyuncs.com';<br>
<br>
/**<br>
* 上传文件根目录<br>
* @var string<br>
*/<br>
private $rootPath;<br>
<br>
/**<br>
* 上传错误信息<br>
* @var string<br>
*/<br>
private $error = '';<br>
private $config = array(<br>
'access_id' => '', //阿里云Access Key ID<br>
'access_key' => '', //阿里云Access Key Secret<br>
'bucket' => '', //空间名称<br>
'timeout' => 90, //超时时间<br>
);<br>
<br>
/**<br>
* 构造函数,用于设置上传根路径<br>
* @param array $config FTP配置<br>
*/<br>
public function __construct($config) {<br>
/* 默认FTP配置 */<br>
$this->config = array_merge($this->config, $config);<br>
}<br>
<br>
/**<br>
* 检测上传根目录(阿里云上传时支持自动创建目录,直接返回)<br>
* @param string $rootpath 根目录<br>
* @return boolean true-检测通过,false-检测失败<br>
*/<br>
public function checkRootPath($rootpath) {<br>
/* 设置根目录 */<br>
$this->rootPath = trim($rootpath, './') . '/';<br>
return true;<br>
}<br>
<br>
/**<br>
* 检测上传目录(阿里云上传时支持自动创建目录,直接返回)<br>
* @param string $savepath 上传目录<br>
* @return boolean 检测结果,true-通过,false-失败<br>
*/<br>
public function checkSavePath($savepath) {<br>
return true;<br>
}<br>
<br>
/**<br>
* 创建文件夹 (阿里云上传时支持自动创建目录,直接返回)<br> * @param string $savepath 目录名称<br>
* @return boolean true-创建成功,false-创建失败<br>
*/<br>
公共函数 mkdir($savepath) {<br>
返回 true;<br>
}<br>
<br>
/**<br>
* 保存指定文件<br>
* @param array $file 保存的文件信息<br>
* @param boolean $replace 同名文件是否覆盖<br>
* @return boolean 保存状态,true-成功,false-失败<br>
*/<br>
公共函数保存($文件,$替换= true){<br>
$_headers = array('内容类型:''.$file['type']);<br>
$resource=fopen($file['tmp_name'],'r');<br>
<br>
$path=$this->rootPath. $file['savepath'] . $file['保存名称'];<br>
<br>
$uri = "/{$this->config['bucket']}{$path}";<br>
$ch = curl_init('http://' . self::OSS_HOST . $uri);<br>
<br>
$date = gmdate('D, d M Y H:i:s GMT');<br>
<br>
array_push($_headers,“日期:{$date}”);<br>
<br>
$sign_string =“PUTnn”。 $文件['类型']。 “n”。 $日期。 “n”。 $uri;<br>
$sign = $this->hex_to_base64(hash_hmac('sha1', $sign_string, $this->config['access_key']));<br>
<br>
array_push($_headers,'授权:OSS''.$this->config['access_id'].':'.$sign);<br>
<br>
fseek($resource, 0, SEEK_END);<br>
$length=ftell($resource);<br>
fseek($resource, 0);<br>
<br>
array_push($_headers,“内容长度:{$length}”);<br>
<br>
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");<br>
curl_setopt($ch, CURLOPT_POST, 1);<br>
curl_setopt($ch, CURLOPT_INFILE, $resource);<br>
curl_setopt($ch, CURLOPT_INFILESIZE, $length);<br>
curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers);<br>
curl_setopt($ch, CURLOPT_TIMEOUT, $this->config['timeout']);<br>
curl_setopt($ch, CURLOPT_HEADER, 1);<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);<br>
$response = curl_exec($ch);<br>
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);<br>
fclose($资源);<br>
如果($status==200){<br>
返回 true;<br>
} 其他 {<br>
$this->错误=$response;<br> return false;<br>
}<br>
}<br>
<br>
/**<br>
* 获取最后一次上传错误信息<br>
* @return string 错误信息<br>
*/<br>
public function getError() {<br>
return $this->error;<br>
}<br>
<br>
<br>
private function hex_to_base64($str) {<br>
$result = '';<br>
<br>
for ($i = 0; $i < strlen($str); $i = 2) {<br />
$result .= chr(hexdec(substr($str, $i, 2)));<br />
}<br />
<br />
return base64_encode($result);<br />
}<br />
<br />
}
使用方法也比较简单,如果只有一个bucket则可以把配置写入config.php里,内容如下'FILE_UPLOAD_TYPE' => 'Oss',<code class="prettyprint linenums lang-php">'FILE_UPLOAD_TYPE' => 'Oss',<br>
'UPLOAD_TYPE_CONFIG' => array(<br>
'access_id' => 'access_id', //阿里云Access Key ID<br>
'access_key' => 'access_key', //阿里云Access Key Secret<br>
'bucket' => 'bucket' //阿里云的bucket<br>
),
'UPLOAD_TYPE_CONFIG' => array($config = array(<br>
'maxSize' => 0,<br>
'exts' => array('jpg', 'jpeg', 'png'),<br>
'saveName' => array('uniq_photo_name', '__FILE__'),<br>
'rootPath' => '/',<br>
);<br>
$oss_config = array(<br>
'access_id' => 'access_id', //阿里云Access Key ID<br>
'access_key' => 'access_key', //阿里云Access Key Secret<br>
'bucket' => 'bucket'<br>
);<br>
$upload = new ThinkUpload($config, 'Oss', $oss_config);
'access_id' => 'access_id', //阿里云Access Key ID
'access_key' => 'access_key', //阿里云Access Key Secret
'bucket' => 'bucket' //阿里云的bucket ),如果你需要将不同的文件存入不同的bucket则需要在上传方法中采用动态配置$config = array(
'maxSize' => 0,
'exts' => array('jpg', 'jpeg', 'png'),
'saveName' => array('uniq_photo_name', '__FILE__'),
'rootPath' => '/',
);
$oss_config = array(
'access_id' => 'access_id', //阿里云Access Key ID
'access_key' => 'access_key', //阿里云Access Key Secret
'bucket' => 'bucket'
);
$upload = new ThinkUpload($config, 'Oss', $oss_config); AD:真正免费,域名 虚机 企业邮箱=0元