Skip to content

Commit b8e0fb1

Browse files
committed
完善上传方法文档
1 parent 80b5748 commit b8e0fb1

File tree

2 files changed

+73
-17
lines changed

2 files changed

+73
-17
lines changed

README.md

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ require_once('upyun.class.php');
1010
$upyun = new UpYun('bucketname', 'username', 'password');
1111
````
1212

13-
参数 `bucketname` 为空间名称,`useranme``password` 为授权操作员的账号密码,不是又拍云的登陆账号密码
13+
参数 `bucketname` 为空间名称,`useranme``password` 为授权操作员的账号密码。
1414

15-
根据国内的网络情况,又拍云存储API目前提供了电信、网通、铁通三个接入点,在初始化的时候可以添加可选的第四个参数来指定API接入点。
15+
根据国内的网络情况,又拍云存储API目前提供了电信、联通网通、移动铁通三个接入点,在初始化的时候可以添加可选的第四个参数来指定API接入点。
1616

1717
````
1818
$upyun = new UpYun('bucketname', 'username', 'password', UpYun::$ED_TELECOM);
@@ -27,6 +27,43 @@ $upyun = new UpYun('bucketname', 'username', 'password', UpYun::$ED_TELECOM);
2727

2828
默认参数为自动选择API接入点。但是我们推荐根据服务器网络状况,手动设置合理的接入点已获取最佳的访问速度。
2929

30+
### 上传文件
31+
32+
````
33+
// 直接传递文件内容的形式上传
34+
$upyun->writeFile('/temp/text_demo.txt', 'Hello World', True);
35+
36+
// 文档流方式上传,可降低内存占用
37+
$fh = fopen('demo.png', 'r');
38+
$upyun->writeFile('/temp/upload_demo.png', $fh, True);
39+
fclose($fh);
40+
````
41+
第三个参数为可选。True 表示自动创建相应目录,默认值为False。
42+
43+
本方法还有一个数组类型的可选参数,用来设置文件类型、缩略图处理等参数。
44+
45+
````
46+
$opts = array(
47+
UpYun::$X_GMKERL_THUMBNAIL => 'square' // 缩略图版本,仅适用于图片空间
48+
);
49+
50+
$fh = fopen('demo.png', 'r');
51+
$upyun->writeFile('/temp/upload_demo.png', $fh, True, $opts);
52+
fclose($fh);
53+
````
54+
该参数可以设置的值还包括:
55+
56+
* UpYun::$CONTENT_TYPE
57+
* UpYun::$CONTENT_MD5
58+
* UpYun::$CONTENT_SECRET
59+
* UpYun::$X_GMKERL_THUMBNAIL
60+
* UpYun::$X_GMKERL_TYPE
61+
* UpYun::$X_GMKERL_VALUE
62+
* UpYun::$X_GMKERL_QUALITY
63+
* UpYun::$X_GMKERL_UNSHARP
64+
65+
参数的具体使用方法,请参考 [标准API上传文件](http://wiki.upyun.com/index.php?title=%E6%A0%87%E5%87%86API%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6)
66+
3067
### 创建目录
3168
````
3269
$upyun->mkDir('/demo/');
@@ -35,10 +72,10 @@ $upyun->mkDir('/demo/');
3572

3673
### 删除目录或者文件
3774
````
38-
$upyun->delete('/demo/');
39-
$upyun->delete('/demo/demo.png');
75+
$upyun->delete('/demo/'); // 删除目录
76+
$upyun->delete('/demo/demo.png'); // 删除文件
4077
````
41-
删除成功返回True,否则抛出异常
78+
删除成功返回True,否则抛出异常。注意删除目录时,`必须保证目录为空` ,否则也会抛出异常。
4279

4380
## 异常处理
4481

upyun.class.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function __construct($message, Exception $previous = null) {
3737
}/*}}}*/
3838

3939
class UpYun {
40+
public static $VERSION = '1.1.1';
4041

4142
/*{{{*/
4243
public static $ED_AUTO = 'v0.api.upyun.com';
@@ -46,12 +47,12 @@ class UpYun {
4647

4748
public static $CONTENT_TYPE = 'Content-Type';
4849
public static $CONTENT_MD5 = 'Content-MD5';
49-
public static $CONTENT_SECRET = 'Content­Secret';
50+
public static $CONTENT_SECRET = 'Content-Secret';
5051

5152
// 缩略图
52-
public static $X_GMKERL_THUMBNAIL = 'x­gmkerl­thumbnail';
53-
public static $X_GMKERL_TYPE = 'x­gmkerl-type';
54-
public static $X_GMKERL_VALUE = 'x­gmkerl-value';
53+
public static $X_GMKERL_THUMBNAIL = 'x-gmkerl-thumbnail';
54+
public static $X_GMKERL_TYPE = 'x-gmkerl-type';
55+
public static $X_GMKERL_VALUE = 'x-gmkerl-value';
5556
public static $X_GMKERL_QUALITY = 'x­gmkerl-quality';
5657
public static $X_GMKERL_UNSHARP = 'x­gmkerl-unsharp';
5758
/*}}}*/
@@ -88,6 +89,13 @@ public function __construct($bucketname, $username, $password, $endpoint = NULL)
8889
$this->endpoint = is_null($endpoint) ? self::$ED_AUTO : $endpoint;
8990
}/*}}}*/
9091

92+
/**
93+
* 获取当前SDK版本号
94+
*/
95+
public function version() {
96+
return sels::$VERSION;
97+
}
98+
9199
/**
92100
* 创建目录
93101
* @param $path 路径
@@ -108,23 +116,34 @@ public function mkDir($path, $auto_mkdir = false) {/*{{{*/
108116
* @return boolean
109117
*/
110118
public function delete($path) {/*{{{*/
111-
$this->_do_request('DELETE', $path);
119+
return $this->_do_request('DELETE', $path);
112120
}/*}}}*/
113121

114122

115123
/**
116124
* 上传文件
117-
* @param $path 存储路径
118-
* @param $file 需要上传的文件,可以是文件流或者文件内容
119-
* @param $opts 可选参数
125+
* @param string $path 存储路径
126+
* @param boolean $auto_mkdir 自动创建目录
127+
* @param mixed $file 需要上传的文件,可以是文件流或者文件内容
128+
* @param array $opts 可选参数
120129
*/
121-
public function writeFile($path, $file, $opts = NULL) {/*{{{*/
130+
public function writeFile($path, $file, $auto_mkdir = False, $opts = NULL) {/*{{{*/
131+
if (is_null($opts)) $opts = array();
122132
if (!is_null($this->_content_md5) || !is_null($this->_file_secret)) {
123-
if (is_null($opts)) $opts = array();
124-
if (!is_null($this->_content_md5)) array_push($opts, self::$CONTENT_MD5 . " {$this->_content_md5}");
125-
if (!is_null($this->_file_secret)) array_push($opts, self::$CONTENT_SECRET . " {$this->_file_secret}");
133+
//if (!is_null($this->_content_md5)) array_push($opts, self::$CONTENT_MD5 . ": {$this->_content_md5}");
134+
//if (!is_null($this->_file_secret)) array_push($opts, self::$CONTENT_SECRET . ": {$this->_file_secret}");
135+
if (!is_null($this->_content_md5)) $opts[self::$CONTENT_MD5] = $this->_content_md5;
136+
if (!is_null($this->_file_secret)) $opts[self::$CONTENT_SECRET] = $this->_file_secret;
126137
}
127138

139+
// 如果设置了缩略版本或者缩略图类型,则添加默认压缩质量和锐化参数
140+
//if (isset($opts[self::$X_GMKERL_THUMBNAIL]) || isset($opts[self::$X_GMKERL_TYPE])) {
141+
// if (!isset($opts[self::$X_GMKERL_QUALITY])) $opts[self::$X_GMKERL_QUALITY] = 95;
142+
// if (!isset($opts[self::$X_GMKERL_UNSHARP])) $opts[self::$X_GMKERL_UNSHARP] = 'true';
143+
//}
144+
145+
if ($auto_mkdir === True) $opts['Mkdir'] = 'true';
146+
128147
return $this->_do_request('PUT', $path, $opts, $file);
129148
}/*}}}*/
130149

0 commit comments

Comments
 (0)