代码如下:
function kindedtiropic(){ if ($this->upload_type=='upyun'){ $upyun_pic = new upyun(unyun_bucket, unyun_username, unyun_password, $api_access[0]); try{ $api_access = array(upyun::ed_auto, upyun::ed_telecom, upyun::ed_cnc, upyun::ed_ctt); //$php_path = dirname(__file__) . '/'; //$php_url = dirname($_server['php_self']) . '/'; //文件保存目录路径 //$save_path = $php_path . '../attached/'; //文件保存目录url //$save_url = $php_url . '../attached/'; //$domain_file = $_config['file']['domain']; $domain_pic = 'http://'.unyun_domain; //$dir_file = $_config['file']['dir']; $dir_pic = '/'.$this->token.'/'; $save_path = ''; $save_url = ''; //定义允许上传的文件扩展名 $ext_arr = array( 'image' => explode(',',c('up_exts')), 'flash' => array('swf', 'flv'), 'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'), 'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'), ); //最大文件大小 $max_size = intval(c('up_size'))*1000; //$save_path = realpath($save_path) . '/'; //php上传失败 if (!empty($_files['imgfile']['error'])) { switch($_files['imgfile']['error']){ case '1': $error = '超过php.ini允许的大小。'; break; case '2': $error = '超过表单允许的大小。'; break; case '3': $error = '图片只有部分被上传。'; break; case '4': $error = '请选择图片。'; break; case '6': $error = '找不到临时目录。'; break; case '7': $error = '写文件到硬盘出错。'; break; case '8': $error = 'file upload stopped by extension。'; break; case '999': default: $error = '未知错误。'; } $this->alert($error); } //有上传文件时 if (empty($_files) === false) { //原文件名 $file_name = $_files['imgfile']['name']; //服务器上临时文件名 $tmp_name = $_files['imgfile']['tmp_name']; //文件大小 $file_size = $_files['imgfile']['size']; //检查文件名 if (!$file_name) { $this->alert(请选择文件。); } //检查目录 //if (@is_dir($save_path) === false) { // alert(上传目录不存在。); //} //检查目录写权限 //if (@is_writable($save_path) === false) { // alert(上传目录没有写权限。); //} //检查是否已上传 if (@is_uploaded_file($tmp_name) === false) { $this->alert(上传失败。); } //检查文件大小 if ($file_size > $max_size) { $this->alert(上传文件大小超过限制。); } //检查目录名 $dir_name = empty($_get['dir']) ? 'image' : trim($_get['dir']); if (empty($ext_arr[$dir_name])) { $this->alert(目录名不正确。); } //获得文件扩展名 $temp_arr = explode(., $file_name); $file_ext = array_pop($temp_arr); $file_ext = trim($file_ext); $file_ext = strtolower($file_ext); //检查扩展名 if (in_array($file_ext, $ext_arr[$dir_name]) === false) { $this->alert(上传文件扩展名是不允许的扩展名。\n只允许 . implode(,, $ext_arr[$dir_name]) . 格式。); } //创建文件夹 if ($dir_name !== '') { $save_path .= $dir_name . /; $save_url .= $dir_name . /; //if (!file_exists($save_path)) { // mkdir($save_path); //} } $ymd = date(ymd); $save_path .= $ymd . /; $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; $fh = fopen($tmp_name, 'r'); $upyun_pic->writefile($dir_pic . $file_path, $fh, true); $save_url = $domain_pic . $dir_pic . $save_url; fclose($fh); //if (move_uploaded_file($tmp_name, $file_path) === false) { // alert(上传文件失败。); //} //@chmod($file_path, 0644); $file_url = $save_url . $new_file_name; header('content-type: text/html; charset=utf-8'); echo json_encode(array('error' => 0, 'url' => $file_url)); exit; }else{ $this->alert('您就先别试这里了,我们服务器禁止写入文件了,o(∩_∩)o'); } }catch(exception $e) { $this->alert($e->getcode().':'.$e->getmessage()); } }elseif ($this->upload_type=='local'){ $return=$this->localupload(); if ($return['error']){ $this->alert($return['msg']); }else { header('content-type: text/html; charset=utf-8'); echo json_encode(array('error' => 0, 'url' => $return['msg'])); exit; } } }下面所class upyun:class upyun { const version = '2.0';/*{{{*/ const ed_auto = 'v0.api.upyun.com'; const ed_telecom = 'v1.api.upyun.com'; const ed_cnc = 'v2.api.upyun.com'; const ed_ctt = 'v3.api.upyun.com'; const content_type = 'content-type'; const content_md5 = 'content-md5'; const content_secret = 'content-secret'; // 缩略图 const x_gmkerl_thumbnail = 'x-gmkerl-thumbnail'; const x_gmkerl_type = 'x-gmkerl-type'; const x_gmkerl_value = 'x-gmkerl-value'; const x_gmkerl_quality = 'xgmkerl-quality'; const x_gmkerl_unsharp = 'xgmkerl-unsharp';/*}}}*/ private $_bucket_name; private $_username; private $_password; private $_timeout = 30; /** * @deprecated */ private $_content_md5 = null; /** * @deprecated */ private $_file_secret = null; /** * @deprecated */ private $_file_infos= null; protected $endpoint; /** * 初始化 upyun 存储接口 * @param $bucketname 空间名称 * @param $username 操作员名称 * @param $password 密码 * * @return object */ public function __construct($bucketname, $username, $password, $endpoint = null, $timeout = 30) {/*{{{*/ $this->_bucketname = $bucketname; $this->_username = $username; $this->_password = md5($password); $this->_timeout = $timeout; $this->endpoint = is_null($endpoint) ? self::ed_auto : $endpoint; }/*}}}*/ /** * 获取当前sdk版本号 */ public function version() { return self::version; } /** * 创建目录 * @param $path 路径 * @param $auto_mkdir 是否自动创建父级目录,最多10层次 * * @return void */ public function makedir($path, $auto_mkdir = false) {/*{{{*/ $headers = array('folder' => 'true'); if ($auto_mkdir) $headers['mkdir'] = 'true'; return $this->_do_request('put', $path, $headers); }/*}}}*/ /** * 删除目录和文件 * @param string $path 路径 * * @return boolean */ public function delete($path) {/*{{{*/ return $this->_do_request('delete', $path); }/*}}}*/ /** * 上传文件 * @param string $path 存储路径 * @param mixed $file 需要上传的文件,可以是文件流或者文件内容 * @param boolean $auto_mkdir 自动创建目录 * @param array $opts 可选参数 */ public function writefile($path, $file, $auto_mkdir = false, $opts = null) {/*{{{*/ if (is_null($opts)) $opts = array(); if (!is_null($this->_content_md5) || !is_null($this->_file_secret)) { //if (!is_null($this->_content_md5)) array_push($opts, self::content_md5 . : {$this->_content_md5}); //if (!is_null($this->_file_secret)) array_push($opts, self::content_secret . : {$this->_file_secret}); if (!is_null($this->_content_md5)) $opts[self::content_md5] = $this->_content_md5; if (!is_null($this->_file_secret)) $opts[self::content_secret] = $this->_file_secret; } // 如果设置了缩略版本或者缩略图类型,则添加默认压缩质量和锐化参数 //if (isset($opts[self::x_gmkerl_thumbnail]) || isset($opts[self::x_gmkerl_type])) { // if (!isset($opts[self::x_gmkerl_quality])) $opts[self::x_gmkerl_quality] = 95; // if (!isset($opts[self::x_gmkerl_unsharp])) $opts[self::x_gmkerl_unsharp] = 'true'; //} if ($auto_mkdir === true) $opts['mkdir'] = 'true'; $this->_file_infos = $this->_do_request('put', $path, $opts, $file); return $this->_file_infos; }/*}}}*/ /** * 下载文件 * @param string $path 文件路径 * @param mixed $file_handle * * @return mixed */ public function readfile($path, $file_handle = null) {/*{{{*/ return $this->_do_request('get', $path, null, null, $file_handle); }/*}}}*/ /** * 获取目录文件列表 * * @param string $path 查询路径 * * @return mixed */ public function getlist($path = '/') {/*{{{*/ $rsp = $this->_do_request('get', $path); $list = array(); if ($rsp) { $rsp = explode(\n, $rsp); foreach($rsp as $item) { @list($name, $type, $size, $time) = explode(\t, trim($item)); if (!empty($time)) { $type = $type == 'n' ? 'file' : 'folder'; } $item = array( 'name' => $name, 'type' => $type, 'size' => intval($size), 'time' => intval($time), ); array_push($list, $item); } } return $list; }/*}}}*/ /** * @deprecated * @param string $path 目录路径 * @return mixed */ public function getfolderusage($path = '/') {/*{{{*/ $rsp = $this->_do_request('get', '/?usage'); return floatval($rsp); }/*}}}*/ /** * 获取文件、目录信息 * * @param string $path 路径 * * @return mixed */ public function getfileinfo($path) {/*{{{*/ $rsp = $this->_do_request('head', $path); return $rsp; }/*}}}*/ /** * 连接签名方法 * @param $method 请求方式 {get, post, put, delete} * return 签名字符串 */ private function sign($method, $uri, $date, $length){/*{{{*/ //$uri = urlencode($uri); $sign = {$method}&{$uri}&{$date}&{$length}&{$this->_password}; return 'upyun '.$this->_username.':'.md5($sign); }/*}}}*/ /** * http request 封装 * @param string $method http request方法,包括put、post、get、options、delete * @param string $path 除bucketname之外的请求路径,包括get参数 * @param array $headers 请求需要的特殊http headers * @param array $body 需要post发送的数据 * * @return mixed */ protected function _do_request($method, $path, $headers = null, $body= null, $file_handle= null) {/*{{{*/ $uri = /{$this->_bucketname}{$path}; $ch = curl_init(http://{$this->endpoint}{$uri}); $_headers = array('expect:'); if (!is_null($headers) && is_array($headers)){ foreach($headers as $k => $v) { array_push($_headers, {$k}: {$v}); } } $length = 0; $date = gmdate('d, d m y h:i:s \g\m\t'); if (!is_null($body)) { if(is_resource($body)){ fseek($body, 0, seek_end); $length = ftell($body); fseek($body, 0); array_push($_headers, content-length: {$length}); curl_setopt($ch, curlopt_infile, $body); curl_setopt($ch, curlopt_infilesize, $length); } else { $length = @strlen($body); array_push($_headers, content-length: {$length}); curl_setopt($ch, curlopt_postfields, $body); } } else { array_push($_headers, content-length: {$length}); } array_push($_headers, authorization: {$this->sign($method, $uri, $date, $length)}); array_push($_headers, date: {$date}); curl_setopt($ch, curlopt_httpheader, $_headers); curl_setopt($ch, curlopt_timeout, $this->_timeout); curl_setopt($ch, curlopt_header, 1); curl_setopt($ch, curlopt_returntransfer, 1); //curl_setopt($ch, curlopt_followlocation, 1); curl_setopt($ch, curlopt_followlocation, 0); curl_setopt($ch, curlopt_customrequest, $method); if ($method == 'put' || $method == 'post') { curl_setopt($ch, curlopt_post, 1); } else { curl_setopt($ch, curlopt_post, 0); } if ($method == 'get' && is_resource($file_handle)) { curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_file, $file_handle); } if ($method == 'head') { curl_setopt($ch, curlopt_nobody, true); } $response = curl_exec($ch); $http_code = curl_getinfo($ch, curlinfo_http_code); if ($http_code == 0) throw new upyunexception('connection failed', $http_code); curl_close($ch); $header_string = ''; $body = ''; if ($method == 'get' && is_resource($file_handle)) { $header_string = ''; $body = $response; } else { list($header_string, $body) = explode(\r\n\r\n, $response, 2); } //var_dump($http_code); if ($http_code == 200) { if ($method == 'get' && is_null($file_handle)) { return $body; } else { $data = $this->_getheadersdata($header_string); return count($data) > 0 ? $data : true; } //elseif ($method == 'head') { // //return $this->_get_headers_data(substr($response, 0 , $header_size)); // return $this->_getheadersdata($header_string); //} //return true; } else { $message = $this->_geterrormessage($header_string); if (is_null($message) && $method == 'get' && is_resource($file_handle)) { $message = 'file not found'; } switch($http_code) { case 401: throw new upyunauthorizationexception($message); break; case 403: throw new upyunforbiddenexception($message); break; case 404: throw new upyunnotfoundexception($message); break; case 406: throw new upyunnotacceptableexception($message); break; case 503: throw new upyunserviceunavailable($message); break; default: throw new upyunexception($message, $http_code); } } }/*}}}*/ /** * 处理http headers中返回的自定义数据 * * @param string $text header字符串 * * @return array */ private function _getheadersdata($text) {/*{{{*/ $headers = explode(\r\n, $text); $items = array(); foreach($headers as $header) { $header = trim($header); if(strpos($header, 'x-upyun') !== false){ list($k, $v) = explode(':', $header); $items[trim($k)] = in_array(substr($k,8,5), array('width','heigh','frame')) ? intval($v) : trim($v); } } return $items; }/*}}}*/ /** * 获取返回的错误信息 * * @param string $header_string * * @return mixed */ private function _geterrormessage($header_string) { list($status, $stash) = explode(\r\n, $header_string, 2); list($v, $code, $message) = explode( , $status, 3); return $message; } /** * 删除目录 * @deprecated * @param $path 路径 * * @return void */ public function rmdir($path) {/*{{{*/ $this->_do_request('delete', $path); }/*}}}*/ /** * 删除文件 * * @deprecated * @param string $path 要删除的文件路径 * * @return boolean */ public function deletefile($path) {/*{{{*/ $rsp = $this->_do_request('delete', $path); }/*}}}*/ /** * 获取目录文件列表 * @deprecated * * @param string $path 要获取列表的目录 * * @return array */ public function readdir($path) {/*{{{*/ return $this->getlist($path); }/*}}}*/ /** * 获取空间使用情况 * * @deprecated 推荐直接使用 getfolderusage('/')来获取 * @return mixed */ public function getbucketusage() {/*{{{*/ return $this->getfolderusage('/'); }/*}}}*/ /** * 获取文件信息 * * #deprecated * @param $file 文件路径(包含文件名) * return array('type'=> file | folder, 'size'=> file size, 'date'=> unix time) 或 null */ //public function getfileinfo($file){/*{{{*/ // $result = $this->head($file); // if(is_null($r))return null; // return array('type'=> $this->tmp_infos['x-upyun-file-type'], 'size'=> @intval($this->tmp_infos['x-upyun-file-size']), 'date'=> @intval($this->tmp_infos['x-upyun-file-date'])); //}/*}}}*/ /** * 切换 api 接口的域名 * * @deprecated * @param $domain {默然 v0.api.upyun.com 自动识别, v1.api.upyun.com 电信, v2.api.upyun.com 联通, v3.api.upyun.com 移动} * return null; */ public function setapidomain($domain){/*{{{*/ $this->endpoint = $domain; }/*}}}*/ /** * 设置待上传文件的 content-md5 值(如又拍云服务端收到的文件md5值与用户设置的不一致,将回报 406 not acceptable 错误) * * @deprecated * @param $str (文件 md5 校验码) * return null; */ public function setcontentmd5($str){/*{{{*/ $this->_content_md5 = $str; }/*}}}*/ /** * 设置待上传文件的 访问密钥(注意:仅支持图片空!,设置密钥后,无法根据原文件url直接访问,需带 url 后面加上 (缩略图间隔标志符+密钥) 进行访问) * 如缩略图间隔标志符为 ! ,密钥为 bac,上传文件路径为 /folder/test.jpg ,那么该图片的对外访问地址为: http://空间域名/folder/test.jpg!bac * * @deprecated * @param $str (文件 md5 校验码) * return null; */ public function setfilesecret($str){/*{{{*/ $this->_file_secret = $str; }/*}}}*/ /** * @deprecated * 获取上传文件后的信息(仅图片空间有返回数据) * @param $key 信息字段名(x-upyun-width、x-upyun-height、x-upyun-frames、x-upyun-file-type) * return value or null */ public function getwritedfileinfo($key){/*{{{*/ if(!isset($this->_file_infos))return null; return $this->_file_infos[$key]; }/*}}}*/}
回复内容: 是一个textarea 里面的上传图片,用户名,密码什么都没问题
代码如下:
function kindedtiropic(){ if ($this->upload_type=='upyun'){ $upyun_pic = new upyun(unyun_bucket, unyun_username, unyun_password, $api_access[0]); try{ $api_access = array(upyun::ed_auto, upyun::ed_telecom, upyun::ed_cnc, upyun::ed_ctt); //$php_path = dirname(__file__) . '/'; //$php_url = dirname($_server['php_self']) . '/'; //文件保存目录路径 //$save_path = $php_path . '../attached/'; //文件保存目录url //$save_url = $php_url . '../attached/'; //$domain_file = $_config['file']['domain']; $domain_pic = 'http://'.unyun_domain; //$dir_file = $_config['file']['dir']; $dir_pic = '/'.$this->token.'/'; $save_path = ''; $save_url = ''; //定义允许上传的文件扩展名 $ext_arr = array( 'image' => explode(',',c('up_exts')), 'flash' => array('swf', 'flv'), 'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'), 'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'), ); //最大文件大小 $max_size = intval(c('up_size'))*1000; //$save_path = realpath($save_path) . '/'; //php上传失败 if (!empty($_files['imgfile']['error'])) { switch($_files['imgfile']['error']){ case '1': $error = '超过php.ini允许的大小。'; break; case '2': $error = '超过表单允许的大小。'; break; case '3': $error = '图片只有部分被上传。'; break; case '4': $error = '请选择图片。'; break; case '6': $error = '找不到临时目录。'; break; case '7': $error = '写文件到硬盘出错。'; break; case '8': $error = 'file upload stopped by extension。'; break; case '999': default: $error = '未知错误。'; } $this->alert($error); } //有上传文件时 if (empty($_files) === false) { //原文件名 $file_name = $_files['imgfile']['name']; //服务器上临时文件名 $tmp_name = $_files['imgfile']['tmp_name']; //文件大小 $file_size = $_files['imgfile']['size']; //检查文件名 if (!$file_name) { $this->alert(请选择文件。); } //检查目录 //if (@is_dir($save_path) === false) { // alert(上传目录不存在。); //} //检查目录写权限 //if (@is_writable($save_path) === false) { // alert(上传目录没有写权限。); //} //检查是否已上传 if (@is_uploaded_file($tmp_name) === false) { $this->alert(上传失败。); } //检查文件大小 if ($file_size > $max_size) { $this->alert(上传文件大小超过限制。); } //检查目录名 $dir_name = empty($_get['dir']) ? 'image' : trim($_get['dir']); if (empty($ext_arr[$dir_name])) { $this->alert(目录名不正确。); } //获得文件扩展名 $temp_arr = explode(., $file_name); $file_ext = array_pop($temp_arr); $file_ext = trim($file_ext); $file_ext = strtolower($file_ext); //检查扩展名 if (in_array($file_ext, $ext_arr[$dir_name]) === false) { $this->alert(上传文件扩展名是不允许的扩展名。\n只允许 . implode(,, $ext_arr[$dir_name]) . 格式。); } //创建文件夹 if ($dir_name !== '') { $save_path .= $dir_name . /; $save_url .= $dir_name . /; //if (!file_exists($save_path)) { // mkdir($save_path); //} } $ymd = date(ymd); $save_path .= $ymd . /; $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; $fh = fopen($tmp_name, 'r'); $upyun_pic->writefile($dir_pic . $file_path, $fh, true); $save_url = $domain_pic . $dir_pic . $save_url; fclose($fh); //if (move_uploaded_file($tmp_name, $file_path) === false) { // alert(上传文件失败。); //} //@chmod($file_path, 0644); $file_url = $save_url . $new_file_name; header('content-type: text/html; charset=utf-8'); echo json_encode(array('error' => 0, 'url' => $file_url)); exit; }else{ $this->alert('您就先别试这里了,我们服务器禁止写入文件了,o(∩_∩)o'); } }catch(exception $e) { $this->alert($e->getcode().':'.$e->getmessage()); } }elseif ($this->upload_type=='local'){ $return=$this->localupload(); if ($return['error']){ $this->alert($return['msg']); }else { header('content-type: text/html; charset=utf-8'); echo json_encode(array('error' => 0, 'url' => $return['msg'])); exit; } } }下面所class upyun:class upyun { const version = '2.0';/*{{{*/ const ed_auto = 'v0.api.upyun.com'; const ed_telecom = 'v1.api.upyun.com'; const ed_cnc = 'v2.api.upyun.com'; const ed_ctt = 'v3.api.upyun.com'; const content_type = 'content-type'; const content_md5 = 'content-md5'; const content_secret = 'content-secret'; // 缩略图 const x_gmkerl_thumbnail = 'x-gmkerl-thumbnail'; const x_gmkerl_type = 'x-gmkerl-type'; const x_gmkerl_value = 'x-gmkerl-value'; const x_gmkerl_quality = 'xgmkerl-quality'; const x_gmkerl_unsharp = 'xgmkerl-unsharp';/*}}}*/ private $_bucket_name; private $_username; private $_password; private $_timeout = 30; /** * @deprecated */ private $_content_md5 = null; /** * @deprecated */ private $_file_secret = null; /** * @deprecated */ private $_file_infos= null; protected $endpoint; /** * 初始化 upyun 存储接口 * @param $bucketname 空间名称 * @param $username 操作员名称 * @param $password 密码 * * @return object */ public function __construct($bucketname, $username, $password, $endpoint = null, $timeout = 30) {/*{{{*/ $this->_bucketname = $bucketname; $this->_username = $username; $this->_password = md5($password); $this->_timeout = $timeout; $this->endpoint = is_null($endpoint) ? self::ed_auto : $endpoint; }/*}}}*/ /** * 获取当前sdk版本号 */ public function version() { return self::version; } /** * 创建目录 * @param $path 路径 * @param $auto_mkdir 是否自动创建父级目录,最多10层次 * * @return void */ public function makedir($path, $auto_mkdir = false) {/*{{{*/ $headers = array('folder' => 'true'); if ($auto_mkdir) $headers['mkdir'] = 'true'; return $this->_do_request('put', $path, $headers); }/*}}}*/ /** * 删除目录和文件 * @param string $path 路径 * * @return boolean */ public function delete($path) {/*{{{*/ return $this->_do_request('delete', $path); }/*}}}*/ /** * 上传文件 * @param string $path 存储路径 * @param mixed $file 需要上传的文件,可以是文件流或者文件内容 * @param boolean $auto_mkdir 自动创建目录 * @param array $opts 可选参数 */ public function writefile($path, $file, $auto_mkdir = false, $opts = null) {/*{{{*/ if (is_null($opts)) $opts = array(); if (!is_null($this->_content_md5) || !is_null($this->_file_secret)) { //if (!is_null($this->_content_md5)) array_push($opts, self::content_md5 . : {$this->_content_md5}); //if (!is_null($this->_file_secret)) array_push($opts, self::content_secret . : {$this->_file_secret}); if (!is_null($this->_content_md5)) $opts[self::content_md5] = $this->_content_md5; if (!is_null($this->_file_secret)) $opts[self::content_secret] = $this->_file_secret; } // 如果设置了缩略版本或者缩略图类型,则添加默认压缩质量和锐化参数 //if (isset($opts[self::x_gmkerl_thumbnail]) || isset($opts[self::x_gmkerl_type])) { // if (!isset($opts[self::x_gmkerl_quality])) $opts[self::x_gmkerl_quality] = 95; // if (!isset($opts[self::x_gmkerl_unsharp])) $opts[self::x_gmkerl_unsharp] = 'true'; //} if ($auto_mkdir === true) $opts['mkdir'] = 'true'; $this->_file_infos = $this->_do_request('put', $path, $opts, $file); return $this->_file_infos; }/*}}}*/ /** * 下载文件 * @param string $path 文件路径 * @param mixed $file_handle * * @return mixed */ public function readfile($path, $file_handle = null) {/*{{{*/ return $this->_do_request('get', $path, null, null, $file_handle); }/*}}}*/ /** * 获取目录文件列表 * * @param string $path 查询路径 * * @return mixed */ public function getlist($path = '/') {/*{{{*/ $rsp = $this->_do_request('get', $path); $list = array(); if ($rsp) { $rsp = explode(\n, $rsp); foreach($rsp as $item) { @list($name, $type, $size, $time) = explode(\t, trim($item)); if (!empty($time)) { $type = $type == 'n' ? 'file' : 'folder'; } $item = array( 'name' => $name, 'type' => $type, 'size' => intval($size), 'time' => intval($time), ); array_push($list, $item); } } return $list; }/*}}}*/ /** * @deprecated * @param string $path 目录路径 * @return mixed */ public function getfolderusage($path = '/') {/*{{{*/ $rsp = $this->_do_request('get', '/?usage'); return floatval($rsp); }/*}}}*/ /** * 获取文件、目录信息 * * @param string $path 路径 * * @return mixed */ public function getfileinfo($path) {/*{{{*/ $rsp = $this->_do_request('head', $path); return $rsp; }/*}}}*/ /** * 连接签名方法 * @param $method 请求方式 {get, post, put, delete} * return 签名字符串 */ private function sign($method, $uri, $date, $length){/*{{{*/ //$uri = urlencode($uri); $sign = {$method}&{$uri}&{$date}&{$length}&{$this->_password}; return 'upyun '.$this->_username.':'.md5($sign); }/*}}}*/ /** * http request 封装 * @param string $method http request方法,包括put、post、get、options、delete * @param string $path 除bucketname之外的请求路径,包括get参数 * @param array $headers 请求需要的特殊http headers * @param array $body 需要post发送的数据 * * @return mixed */ protected function _do_request($method, $path, $headers = null, $body= null, $file_handle= null) {/*{{{*/ $uri = /{$this->_bucketname}{$path}; $ch = curl_init(http://{$this->endpoint}{$uri}); $_headers = array('expect:'); if (!is_null($headers) && is_array($headers)){ foreach($headers as $k => $v) { array_push($_headers, {$k}: {$v}); } } $length = 0; $date = gmdate('d, d m y h:i:s \g\m\t'); if (!is_null($body)) { if(is_resource($body)){ fseek($body, 0, seek_end); $length = ftell($body); fseek($body, 0); array_push($_headers, content-length: {$length}); curl_setopt($ch, curlopt_infile, $body); curl_setopt($ch, curlopt_infilesize, $length); } else { $length = @strlen($body); array_push($_headers, content-length: {$length}); curl_setopt($ch, curlopt_postfields, $body); } } else { array_push($_headers, content-length: {$length}); } array_push($_headers, authorization: {$this->sign($method, $uri, $date, $length)}); array_push($_headers, date: {$date}); curl_setopt($ch, curlopt_httpheader, $_headers); curl_setopt($ch, curlopt_timeout, $this->_timeout); curl_setopt($ch, curlopt_header, 1); curl_setopt($ch, curlopt_returntransfer, 1); //curl_setopt($ch, curlopt_followlocation, 1); curl_setopt($ch, curlopt_followlocation, 0); curl_setopt($ch, curlopt_customrequest, $method); if ($method == 'put' || $method == 'post') { curl_setopt($ch, curlopt_post, 1); } else { curl_setopt($ch, curlopt_post, 0); } if ($method == 'get' && is_resource($file_handle)) { curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_file, $file_handle); } if ($method == 'head') { curl_setopt($ch, curlopt_nobody, true); } $response = curl_exec($ch); $http_code = curl_getinfo($ch, curlinfo_http_code); if ($http_code == 0) throw new upyunexception('connection failed', $http_code); curl_close($ch); $header_string = ''; $body = ''; if ($method == 'get' && is_resource($file_handle)) { $header_string = ''; $body = $response; } else { list($header_string, $body) = explode(\r\n\r\n, $response, 2); } //var_dump($http_code); if ($http_code == 200) { if ($method == 'get' && is_null($file_handle)) { return $body; } else { $data = $this->_getheadersdata($header_string); return count($data) > 0 ? $data : true; } //elseif ($method == 'head') { // //return $this->_get_headers_data(substr($response, 0 , $header_size)); // return $this->_getheadersdata($header_string); //} //return true; } else { $message = $this->_geterrormessage($header_string); if (is_null($message) && $method == 'get' && is_resource($file_handle)) { $message = 'file not found'; } switch($http_code) { case 401: throw new upyunauthorizationexception($message); break; case 403: throw new upyunforbiddenexception($message); break; case 404: throw new upyunnotfoundexception($message); break; case 406: throw new upyunnotacceptableexception($message); break; case 503: throw new upyunserviceunavailable($message); break; default: throw new upyunexception($message, $http_code); } } }/*}}}*/ /** * 处理http headers中返回的自定义数据 * * @param string $text header字符串 * * @return array */ private function _getheadersdata($text) {/*{{{*/ $headers = explode(\r\n, $text); $items = array(); foreach($headers as $header) { $header = trim($header); if(strpos($header, 'x-upyun') !== false){ list($k, $v) = explode(':', $header); $items[trim($k)] = in_array(substr($k,8,5), array('width','heigh','frame')) ? intval($v) : trim($v); } } return $items; }/*}}}*/ /** * 获取返回的错误信息 * * @param string $header_string * * @return mixed */ private function _geterrormessage($header_string) { list($status, $stash) = explode(\r\n, $header_string, 2); list($v, $code, $message) = explode( , $status, 3); return $message; } /** * 删除目录 * @deprecated * @param $path 路径 * * @return void */ public function rmdir($path) {/*{{{*/ $this->_do_request('delete', $path); }/*}}}*/ /** * 删除文件 * * @deprecated * @param string $path 要删除的文件路径 * * @return boolean */ public function deletefile($path) {/*{{{*/ $rsp = $this->_do_request('delete', $path); }/*}}}*/ /** * 获取目录文件列表 * @deprecated * * @param string $path 要获取列表的目录 * * @return array */ public function readdir($path) {/*{{{*/ return $this->getlist($path); }/*}}}*/ /** * 获取空间使用情况 * * @deprecated 推荐直接使用 getfolderusage('/')来获取 * @return mixed */ public function getbucketusage() {/*{{{*/ return $this->getfolderusage('/'); }/*}}}*/ /** * 获取文件信息 * * #deprecated * @param $file 文件路径(包含文件名) * return array('type'=> file | folder, 'size'=> file size, 'date'=> unix time) 或 null */ //public function getfileinfo($file){/*{{{*/ // $result = $this->head($file); // if(is_null($r))return null; // return array('type'=> $this->tmp_infos['x-upyun-file-type'], 'size'=> @intval($this->tmp_infos['x-upyun-file-size']), 'date'=> @intval($this->tmp_infos['x-upyun-file-date'])); //}/*}}}*/ /** * 切换 api 接口的域名 * * @deprecated * @param $domain {默然 v0.api.upyun.com 自动识别, v1.api.upyun.com 电信, v2.api.upyun.com 联通, v3.api.upyun.com 移动} * return null; */ public function setapidomain($domain){/*{{{*/ $this->endpoint = $domain; }/*}}}*/ /** * 设置待上传文件的 content-md5 值(如又拍云服务端收到的文件md5值与用户设置的不一致,将回报 406 not acceptable 错误) * * @deprecated * @param $str (文件 md5 校验码) * return null; */ public function setcontentmd5($str){/*{{{*/ $this->_content_md5 = $str; }/*}}}*/ /** * 设置待上传文件的 访问密钥(注意:仅支持图片空!,设置密钥后,无法根据原文件url直接访问,需带 url 后面加上 (缩略图间隔标志符+密钥) 进行访问) * 如缩略图间隔标志符为 ! ,密钥为 bac,上传文件路径为 /folder/test.jpg ,那么该图片的对外访问地址为: http://空间域名/folder/test.jpg!bac * * @deprecated * @param $str (文件 md5 校验码) * return null; */ public function setfilesecret($str){/*{{{*/ $this->_file_secret = $str; }/*}}}*/ /** * @deprecated * 获取上传文件后的信息(仅图片空间有返回数据) * @param $key 信息字段名(x-upyun-width、x-upyun-height、x-upyun-frames、x-upyun-file-type) * return value or null */ public function getwritedfileinfo($key){/*{{{*/ if(!isset($this->_file_infos))return null; return $this->_file_infos[$key]; }/*}}}*/}
要上传的文件,前面缺少/
$file_name = $_files['imgfile']['name'];
改为:
$file_name = '/' . $_files['imgfile']['name'];
试试
