UpYun::writeFile PHP Method

writeFile() public method

上传文件
public writeFile ( string $path, mixed $file, boolean $auto_mkdir = False, array $opts = NULL )
$path string 存储路径
$file mixed 需要上传的文件,可以是文件流或者文件内容
$auto_mkdir boolean 自动创建目录
$opts array 可选参数
    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;
    }

Usage Example

Example #1
0
 function upload($source, $target)
 {
     try {
         $file = new UpyunMultiPartFile($source);
     } catch (Exception $e) {
         return 0;
     }
     if ($file->getSize() > 1024 * 1024 && $this->upyun_config['form_api_key']) {
         $sign = new UpyunMultipartSignature($this->upyun_config['form_api_key']);
         $upload = new UpyunMultipartUpload($sign);
         $upload->setBucketName($this->upyun_config['bucket_name']);
         $upload->setBlockSize($upload->getBlockSizeAdaptive($file));
         try {
             $result = $upload->upload($file, array('path' => $this->upyun_dir . ltrim($target, '/')));
             return $result;
         } catch (Exception $e) {
             return 0;
         }
     } else {
         $fh = fopen($source, 'rb');
         if (!$fh) {
             return 0;
         }
         $upyun = new UpYun($this->upyun_config['bucket_name'], $this->upyun_config['operator_name'], $this->upyun_config['operator_pwd']);
         $rsp = $upyun->writeFile($this->upyun_dir . ltrim($target, '/'), $fh, true);
         return $rsp;
     }
 }
All Usage Examples Of UpYun::writeFile