VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 详解php版阿里云OSS图片上传类

本文实例讲述了php版阿里云OSS图片上传类。分享给大家供大家参考,具体如下:

1.阿里云基本函数

  1. /** 
  2.  
  3.  * 把本地变量的内容到文件 
  4.  
  5.  * 简单上传,上传指定变量的内存值作为object的内容 
  6.  
  7.  */ 
  8.  
  9. public function putObject($imgPath,$object) 
  10.  
  11. { 
  12.  
  13.   $content = file_get_contents($imgPath); // 把当前文件的内容获取到传入文件中 
  14.  
  15.   $options = array(); 
  16.  
  17.   try { 
  18.  
  19.     $this->ossClient->putObject($this->bucket, $object, $content, $options); 
  20.  
  21.   } catch (OssException $e) { 
  22.  
  23.     return $e->getMessage(); 
  24.  
  25.   } 
  26.  
  27.   return TRUE; 
  28.  
  29. } 
  30.  
  31. /** 
  32.  
  33.  * 上传指定的本地文件内容 
  34.  
  35.  */ 
  36.  
  37. public function uploadFile($imgPath,$object) //$_FILES['img']['tmp_name'] 
  38.  
  39. { 
  40.  
  41.   $filePath = $imgPath; 
  42.  
  43.   $options = array(); 
  44.  
  45.   try { 
  46.  
  47.     $this->ossClient->uploadFile($this->bucket, $object, $filePath, $options); 
  48.  
  49.   } catch (OssException $e) { 
  50.  
  51.     return $e->getMessage(); 
  52.  
  53.   } 
  54.  
  55.   return TRUE; 
  56.  
  57. } 
  58.  
  59. // 删除对象 
  60.  
  61. public function deleteObject($object) { 
  62.  
  63.   try { 
  64.  
  65.     $this->ossClient->deleteObject($this->bucket, $object); 
  66.  
  67.   } catch (OssException $e) { 
  68.  
  69.     return $e->getMessage(); 
  70.  
  71.   } 
  72.  
  73.   return TRUE; 
  74.  
  75. } 
  76.  
  77. // 判断对象是否存在 
  78.  
  79. public function doesObjectExist($object) { 
  80.  
  81.   try { 
  82.  
  83.     $result = $this->ossClient->doesObjectExist($this->bucket, $object); 
  84.  
  85.   } catch (OssException $e) { 
  86.  
  87.     return $e->getMessage(); 
  88.  
  89.   } 
  90.  
  91.   return $result; 
  92.  
  93. } 
  94.  
  95. // 批量删除对象 
  96.  
  97. public function deleteObjects($objects) { 
  98.  
  99.   try { 
  100.  
  101.     $this->ossClient->deleteObjects($this->bucket, $objects); 
  102.  
  103.   } catch (OssException $e) { 
  104.  
  105.     return $e->getMessage(); 
  106.  
  107.   } 
  108.  
  109.   return TRUE; 
  110.  
  111. } 
  112.  
  113. /** 
  114.  
  115.  * 获取object的内容 
  116.  
  117.  * 
  118.  
  119.  * @param OssClient $ossClient OssClient实例 
  120.  
  121.  * @param string $bucket 存储空间名称 
  122.  
  123.  * @return null 
  124.  
  125.  */ 
  126.  
  127. public function getObject($object) 
  128.  
  129. { 
  130.  
  131.   $options = array(); 
  132.  
  133.   try { 
  134.  
  135.     $content = $this->ossClient->getObject($this->bucket, $object, $options); 
  136.  
  137.   } catch (OssException $e) { 
  138.  
  139.     return $e->getMessage(); 
  140.  
  141.   } 
  142.  
  143.   // file_get_contents 
  144.  
  145.   return $content; 
  146.  
  147. } 

2.基本配置与辅助函数

  1. public $ossClient,$bucket; 
  2.  
  3. private $configinfo =  array( 
  4.  
  5.   'maxSize'      => -1,  // 上传文件的最大值 
  6.  
  7.   'supportMulti'   => true,  // 是否支持多文件上传 
  8.  
  9.   'allowExts'     => array(),  // 允许上传的文件后缀 留空不作后缀检查 
  10.  
  11.   'allowTypes'    => array(),  // 允许上传的文件类型 留空不做检查 
  12.  
  13.   'thumb'       => false,  // 使用对上传图片进行缩略图处理 
  14.  
  15.   'imageClassPath'  => 'ORG.Util.Image',  // 图库类包路径 
  16.  
  17.   'thumbMaxWidth'   => '',// 缩略图最大宽度 
  18.  
  19.   'thumbMaxHeight'  => '',// 缩略图最大高度 
  20.  
  21.   'thumbPrefix'    => 'thumb_',// 缩略图前缀 
  22.  
  23.   'thumbSuffix'    => '', 
  24.  
  25.   'thumbPath'     => '',// 缩略图保存路径 
  26.  
  27.   'thumbFile'     => '',// 缩略图文件名 
  28.  
  29.   'thumbExt'     => '',// 缩略图扩展名 
  30.  
  31.   'thumbRemoveOrigin' => false,// 是否移除原图 
  32.  
  33.   'zipImages'     => false,// 压缩图片文件上传 
  34.  
  35.   'autoSub'      => false,// 启用子目录保存文件 
  36.  
  37.   'subType'      => 'hash',// 子目录创建方式 可以使用hash date custom 
  38.  
  39.   'subDir'      => '', // 子目录名称 subType为custom方式后有效 
  40.  
  41.   'dateFormat'    => 'Ymd', 
  42.  
  43.   'hashLevel'     => 1, // hash的目录层次 
  44.  
  45.   'savePath'     => '',// 上传文件保存路径 
  46.  
  47.   'autoCheck'     => true, // 是否自动检查附件 
  48.  
  49.   'uploadReplace'   => false,// 存在同名是否覆盖 
  50.  
  51.   'saveRule'     => 'uniqid',// 上传文件命名规则 
  52.  
  53.   'hashType'     => 'md5_file',// 上传文件Hash规则函数名 
  54.  
  55.   ); 
  56.  
  57. // 错误信息 
  58.  
  59. private $error = ''; 
  60.  
  61. // 上传成功的文件信息 
  62.  
  63. private $uploadFileInfo ; 
  64.  
  65. public function __get($name){ 
  66.  
  67.   if(isset($this->configinfo[$name])) { 
  68.  
  69.     return $this->configinfo[$name]; 
  70.  
  71.   } 
  72.  
  73.   return null; 
  74.  
  75. } 
  76.  
  77. public function __set($name,$value){ 
  78.  
  79.   if(isset($this->configinfo[$name])) { 
  80.  
  81.     $this->configinfo[$name]  =  $value; 
  82.  
  83.   } 
  84.  
  85. } 
  86.  
  87. public function __isset($name){ 
  88.  
  89.   return isset($this->configinfo[$name]); 
  90.  
  91. } 
  92.  
  93. /** 
  94.  
  95.  * 架构函数 
  96.  
  97.  * @access public 
  98.  
  99.  * @param array $config 上传参数 
  100.  
  101.  */ 
  102.  
  103. public function __construct($config=array()) { 
  104.  
  105.   if(is_array($config)) { 
  106.  
  107.     $this->config  =  array_merge($this->config,$config); 
  108.  
  109.   } 
  110.  
  111.   $this->bucket = C('OSS_TEST_BUCKET'); 
  112.  
  113.   $this->ossClient = new OssClient(C('OSS_ACCESS_ID'), C('OSS_ACCESS_KEY'), C('OSS_ENDPOINT'), false); 
  114.  
  115. } 

3.主函数

  1. /** 
  2.  
  3.  * 上传所有文件 
  4.  
  5.  * @access public 
  6.  
  7.  * @param string $savePath 上传文件保存路径 
  8.  
  9.  * @return string 
  10.  
  11.  */ 
  12.  
  13. public function upload($savePath ='') { 
  14.  
  15.   //如果不指定保存文件名,则由系统默认 
  16.  
  17.   if(emptyempty($savePath)) { 
  18.  
  19.     $savePath = $this->savePath; 
  20.  
  21.   } 
  22.  
  23.   $fileInfo  = array(); 
  24.  
  25.   $isUpload  = false; 
  26.  
  27.   // 获取上传的文件信息 
  28.  
  29.   // 对$_FILES数组信息处理 
  30.  
  31.   $files  =  $this->dealFiles($_FILES); 
  32.  
  33.   foreach($files as $key => $file) { 
  34.  
  35.     //过滤无效的上传 
  36.  
  37.     if(!emptyempty($file['name'])) { 
  38.  
  39.       //登记上传文件的扩展信息 
  40.  
  41.       if(!isset($file['key']))  $file['key']  =  $key; 
  42.  
  43.       $file['extension'] =  $this->getExt($file['name']); 
  44.  
  45.       $file['savepath']  =  $savePath; 
  46.  
  47.       $file['savename']  =  $this->getSaveName($file); 
  48.  
  49.       // 自动检查附件 
  50.  
  51.       if($this->autoCheck) { 
  52.  
  53.         if(!$this->check($file)) 
  54.  
  55.           return false; 
  56.  
  57.       } 
  58.  
  59.       //保存上传文件 
  60.  
  61.       if(!$this->save($file)) return false; 
  62.  
  63.       if(function_exists($this->hashType)) { 
  64.  
  65.         $fun = $this->hashType; 
  66.  
  67.         $file['hash']  = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk')); 
  68.  
  69.       } 
  70.  
  71.       //上传成功后保存文件信息,供其他地方调用 
  72.  
  73.       unset($file['tmp_name'],$file['error']); 
  74.  
  75.       $fileInfo[] = $file; 
  76.  
  77.       $isUpload  = true; 
  78.  
  79.     } 
  80.  
  81.   } 
  82.  
  83.   if($isUpload) { 
  84.  
  85.     $this->uploadFileInfo = $fileInfo; 
  86.  
  87.     return true; 
  88.  
  89.   }else { 
  90.  
  91.     $this->error = '没有选择上传文件'; 
  92.  
  93.     return false; 
  94.  
  95.   } 
  96.  
  97. } 

4.核心处理函数

  1. /** 
  2.  
  3.  * 上传一个文件 
  4.  
  5.  * @access public 
  6.  
  7.  * @param mixed $name 数据 
  8.  
  9.  * @param string $value 数据表名 
  10.  
  11.  * @return string 
  12.  
  13.  */ 
  14.  
  15. private function save($file) { 
  16.  
  17.   $filename = $file['savepath'].$file['savename']; 
  18.  
  19.   if(!$this->uploadReplace && $this->doesObjectExist($filename)) { 
  20.  
  21.     // 不覆盖同名文件 
  22.  
  23.     $this->error  =  '文件已经存在!'.$filename; 
  24.  
  25.     return false; 
  26.  
  27.   } 
  28.  
  29.   // 如果是图像文件 检测文件格式 
  30.  
  31.   if( in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png','swf'))) { 
  32.  
  33.     $info  = getimagesize($file['tmp_name']); 
  34.  
  35.     if(false === $info || ('gif' == strtolower($file['extension']) && emptyempty($info['bits']))){ 
  36.  
  37.       $this->error = '非法图像文件'; 
  38.  
  39.       return false; 
  40.  
  41.     } 
  42.  
  43.   } 
  44.  
  45.   if(!$this->putObject($file['tmp_name'], $this->autoCharset($filename,'utf-8','gbk'))) { 
  46.  
  47.     $this->error = '文件上传保存错误!'; 
  48.  
  49.     return false; 
  50.  
  51.   } 
  52.  
  53.   if($this->thumb && in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png'))) { 
  54.  
  55.     $image = getimagesize(C('OSS_IMG_URL').'/'.$filename); 
  56.  
  57.     if(false !== $image) { 
  58.  
  59.       //是图像文件生成缩略图 
  60.  
  61.       $thumbWidth   =  explode(',',$this->thumbMaxWidth); 
  62.  
  63.       $thumbHeight  =  explode(',',$this->thumbMaxHeight); 
  64.  
  65.       $thumbPrefix  =  explode(',',$this->thumbPrefix); 
  66.  
  67.       $thumbSuffix  =  explode(',',$this->thumbSuffix); 
  68.  
  69.       $thumbFile   =  explode(',',$this->thumbFile); 
  70.  
  71.       $thumbPath   =  $this->thumbPath?$this->thumbPath:dirname($filename).'/'; 
  72.  
  73.       $thumbExt    =  $this->thumbExt ? $this->thumbExt : $file['extension']; //自定义缩略图扩展名 
  74.  
  75.       // 生成图像缩略图 
  76.  
  77.       import($this->imageClassPath); 
  78.  
  79.       for($i=0,$len=count($thumbWidth); $i<$len; $i++) { 
  80.  
  81.         if(!emptyempty($thumbFile[$i])) { 
  82.  
  83.           $thumbname =  $thumbFile[$i]; 
  84.  
  85.         }else{ 
  86.  
  87.           $prefix   =  isset($thumbPrefix[$i])?$thumbPrefix[$i]:$thumbPrefix[0]; 
  88.  
  89.           $suffix   =  isset($thumbSuffix[$i])?$thumbSuffix[$i]:$thumbSuffix[0]; 
  90.  
  91.           $thumbname =  $prefix.basename($filename,'.'.$file['extension']).$suffix; 
  92.  
  93.         } 
  94.  
  95.         $this->thumb(C('OSS_IMG_URL').'/'.$filename,$thumbPath.$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true); 
  96.  
  97.       } 
  98.  
  99.       if($this->thumbRemoveOrigin) { 
  100.  
  101.         // 生成缩略图之后删除原图 
  102.  
  103.         $this->deleteObject($filename); 
  104.  
  105.       } 
  106.  
  107.     } 
  108.  
  109.   } 
  110.  
  111.   if($this->zipImags) { 
  112.  
  113.     // TODO 对图片压缩包在线解压 
  114.  
  115.   } 
  116.  
  117.   return true; 
  118.  
  119. } 
  120.  
  121. /** 
  122.  
  123.  * 生成缩略图 
  124.  
  125.  * @static 
  126.  
  127.  * @access public 
  128.  
  129.  * @param string $image 原图 
  130.  
  131.  * @param string $type 图像格式 
  132.  
  133.  * @param string $thumbname 缩略图文件名 
  134.  
  135.  * @param string $maxWidth 宽度 
  136.  
  137.  * @param string $maxHeight 高度 
  138.  
  139.  * @param string $position 缩略图保存目录 
  140.  
  141.  * @param boolean $interlace 启用隔行扫描 
  142.  
  143.  * @return void 
  144.  
  145.  */ 
  146.  
  147. public function thumb($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true) { 
  148.  
  149.   // 获取原图信息 
  150.  
  151.   $info = Image::getImageInfo($image); 
  152.  
  153.   if ($info !== false) { 
  154.  
  155.     $srcWidth = $info['width']; 
  156.  
  157.     $srcHeight = $info['height']; 
  158.  
  159.     $type = emptyempty($type) ? $info['type'] : $type; 
  160.  
  161.     $type = strtolower($type); 
  162.  
  163.     $interlace = $interlace ? 1 : 0; 
  164.  
  165.     unset($info); 
  166.  
  167.     $scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例 
  168.  
  169.     if ($scale >= 1) { 
  170.  
  171.       // 超过原图大小不再缩略 
  172.  
  173.       $width = $srcWidth; 
  174.  
  175.       $height = $srcHeight; 
  176.  
  177.     } else { 
  178.  
  179.       // 缩略图尺寸 
  180.  
  181.       $width = (int) ($srcWidth * $scale); 
  182.  
  183.       $height = (int) ($srcHeight * $scale); 
  184.  
  185.     } 
  186.  
  187.     // 载入原图 
  188.  
  189.     $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type); 
  190.  
  191.     if(!function_exists($createFun)) { 
  192.  
  193.       return false; 
  194.  
  195.     } 
  196.  
  197.     $srcImg = $createFun($image); 
  198.  
  199.     //创建缩略图 
  200.  
  201.     if ($type != 'gif' && function_exists('imagecreatetruecolor')) 
  202.  
  203.       $thumbImg = imagecreatetruecolor($width, $height); 
  204.  
  205.     else 
  206.  
  207.       $thumbImg = imagecreate($width, $height); 
  208.  
  209.      //png和gif的透明处理 by luofei614 
  210.  
  211.     if('png'==$type){ 
  212.  
  213.       imagealphablending($thumbImg, false);//取消默认的混色模式(为解决阴影为绿色的问题) 
  214.  
  215.       imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息(为解决阴影为绿色的问题) 
  216.  
  217.     }elseif('gif'==$type){ 
  218.  
  219.       $trnprt_indx = imagecolortransparent($srcImg); 
  220.  
  221.        if ($trnprt_indx >= 0) { 
  222.  
  223.           //its transparent 
  224.  
  225.           $trnprt_color = imagecolorsforindex($srcImg , $trnprt_indx); 
  226.  
  227.           $trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); 
  228.  
  229.           imagefill($thumbImg, 0, 0, $trnprt_indx); 
  230.  
  231.           imagecolortransparent($thumbImg, $trnprt_indx); 
  232.  
  233.      } 
  234.  
  235.     } 
  236.  
  237.     // 复制图片 
  238.  
  239.     if (function_exists("ImageCopyResampled")) 
  240.  
  241.       imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight); 
  242.  
  243.     else 
  244.  
  245.       imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight); 
  246.  
  247.     // 对jpeg图形设置隔行扫描 
  248.  
  249.     if ('jpg' == $type || 'jpeg' == $type) 
  250.  
  251.       imageinterlace($thumbImg, $interlace); 
  252.  
  253.     imagePNG($thumbImg,'Uploads/file.png'); // 中转站 
  254.  
  255.     // 生成图片 
  256.  
  257.     $this->putObject('Uploads/file.png',$thumbname); 
  258.  
  259.     imagedestroy($thumbImg); 
  260.  
  261.     imagedestroy($srcImg); 
  262.  
  263.     return $thumbname; 
  264.  
  265.   } 
  266.  
  267.   return false; 
  268.  
  269. } 

5.辅助函数

  1. /** 
  2.  
  3. * 转换上传文件数组变量为正确的方式 
  4.  
  5. * @access private 
  6.  
  7. * @param array $files 上传的文件变量 
  8.  
  9. * @return array 
  10.  
  11. */ 
  12.  
  13. private function dealFiles($files) { 
  14.  
  15.     $fileArray = array(); 
  16.  
  17.     $n     = 0; 
  18.  
  19.     foreach ($files as $key=>$file){ 
  20.  
  21.       if(is_array($file['name'])) { 
  22.  
  23.         $keys    =  array_keys($file); 
  24.  
  25.         $count   =  count($file['name']); 
  26.  
  27.         for ($i=0; $i<$count; $i++) { 
  28.  
  29.           $fileArray[$n]['key'] = $key; 
  30.  
  31.           foreach ($keys as $_key){ 
  32.  
  33.             $fileArray[$n][$_key] = $file[$_key][$i]; 
  34.  
  35.           } 
  36.  
  37.           $n++; 
  38.  
  39.         } 
  40.  
  41.       }else{ 
  42.  
  43.         $fileArray[$key] = $file; 
  44.  
  45.       } 
  46.  
  47.     } 
  48.  
  49.     return $fileArray; 
  50.  
  51. } 
  52.  
  53. /** 
  54.  
  55. * 检查上传的文件 
  56.  
  57. * @access private 
  58.  
  59. * @param array $file 文件信息 
  60.  
  61. * @return boolean 
  62.  
  63. */ 
  64.  
  65. private function check($file) { 
  66.  
  67.     if($file['error']!== 0) { 
  68.  
  69.       //文件上传失败 
  70.  
  71.       //捕获错误代码 
  72.  
  73.       $this->error($file['error']); 
  74.  
  75.       return false; 
  76.  
  77.     } 
  78.  
  79.     //文件上传成功,进行自定义规则检查 
  80.  
  81.     //检查文件大小 
  82.  
  83.     if(!$this->checkSize($file['size'])) { 
  84.  
  85.       $this->error = '上传文件大小不符!'; 
  86.  
  87.       return false; 
  88.  
  89.     } 
  90.  
  91.     //检查文件Mime类型 
  92.  
  93.     if(!$this->checkType($file['type'])) { 
  94.  
  95.       $this->error = '上传文件MIME类型不允许!'; 
  96.  
  97.       return false; 
  98.  
  99.     } 
  100.  
  101.     //检查文件类型 
  102.  
  103.     if(!$this->checkExt($file['extension'])) { 
  104.  
  105.       $this->error ='上传文件类型不允许'; 
  106.  
  107.       return false; 
  108.  
  109.     } 
  110.  
  111.     //检查是否合法上传 
  112.  
  113.     if(!$this->checkUpload($file['tmp_name'])) { 
  114.  
  115.       $this->error = '非法上传文件!'; 
  116.  
  117.       return false; 
  118.  
  119.     } 
  120.  
  121.     return true; 
  122.  
  123. } 
  124.  
  125. // 自动转换字符集 支持数组转换 
  126.  
  127. private function autoCharset($fContents, $from='gbk', $to='utf-8') { 
  128.  
  129.     $from  = strtoupper($from) == 'UTF8' ? 'utf-8' : $from; 
  130.  
  131.     $to   = strtoupper($to) == 'UTF8' ? 'utf-8' : $to; 
  132.  
  133.     if (strtoupper($from) === strtoupper($to) || emptyempty($fContents) || (is_scalar($fContents) && !is_string($fContents))) { 
  134.  
  135.       //如果编码相同或者非字符串标量则不转换 
  136.  
  137.       return $fContents; 
  138.  
  139.     } 
  140.  
  141.     if (function_exists('mb_convert_encoding')) { 
  142.  
  143.       return mb_convert_encoding($fContents, $to, $from); 
  144.  
  145.     } elseif (function_exists('iconv')) { 
  146.  
  147.       return iconv($from, $to, $fContents); 
  148.  
  149.     } else { 
  150.  
  151.       return $fContents; 
  152.  
  153.     } 
  154.  
  155. } 
  156.  
  157. /** 
  158.  
  159. * 检查上传的文件类型是否合法 
  160.  
  161. * @access private 
  162.  
  163. * @param string $type 数据 
  164.  
  165. * @return boolean 
  166.  
  167. */ 
  168.  
  169. private function checkType($type) { 
  170.  
  171.     if(!emptyempty($this->allowTypes)) 
  172.  
  173.       return in_array(strtolower($type),$this->allowTypes); 
  174.  
  175.     return true; 
  176.  
  177. } 
  178.  
  179. /** 
  180.  
  181. * 检查上传的文件后缀是否合法 
  182.  
  183. * @access private 
  184.  
  185. * @param string $ext 后缀名 
  186.  
  187. * @return boolean 
  188.  
  189. */ 
  190.  
  191. private function checkExt($ext) { 
  192.  
  193.     if(!emptyempty($this->allowExts)) 
  194.  
  195.       return in_array(strtolower($ext),$this->allowExts,true); 
  196.  
  197.     return true; 
  198.  
  199. } 
  200.  
  201. /** 
  202.  
  203. * 检查文件大小是否合法 
  204.  
  205. * @access private 
  206.  
  207. * @param integer $size 数据 
  208.  
  209. * @return boolean 
  210.  
  211. */ 
  212.  
  213. private function checkSize($size) { 
  214.  
  215.     return !($size > $this->maxSize) || (-1 == $this->maxSize); 
  216.  
  217. } 
  218.  
  219. /** 
  220.  
  221. * 检查文件是否非法提交 
  222.  
  223. * @access private 
  224.  
  225. * @param string $filename 文件名 
  226.  
  227. * @return boolean 
  228.  
  229. */ 
  230.  
  231. private function checkUpload($filename) { 
  232.  
  233.     return is_uploaded_file($filename); 
  234.  
  235. } 
  236.  
  237. /** 
  238.  
  239. * 取得上传文件的后缀 
  240.  
  241. * @access private 
  242.  
  243. * @param string $filename 文件名 
  244.  
  245. * @return boolean 
  246.  
  247. */ 
  248.  
  249. private function getExt($filename) { 
  250.  
  251.     $pathinfo = pathinfo($filename); 
  252.  
  253.     return $pathinfo['extension']; 
  254.  
  255. } 
  256.  
  257. /** 
  258.  
  259. * 取得上传文件的信息 
  260.  
  261. * @access public 
  262.  
  263. * @return array 
  264.  
  265. */ 
  266.  
  267. public function getUploadFileInfo() { 
  268.  
  269.     return $this->uploadFileInfo; 
  270.  
  271. } 
  272.  
  273. /** 
  274.  
  275. * 取得最后一次错误信息 
  276.  
  277. * @access public 
  278.  
  279. * @return string 
  280.  
  281. */ 
  282.  
  283. public function getErrorMsg() { 
  284.  
  285.     return $this->error; 
  286.  
  287. } 

总结:与普通上传的区别在于,它是全部通过阿里云的oss接口来处理文件保存的。普通上传是把本地文件移动到服务器上,而它则是把文件移动到阿里云服务器上。

缩略图思路:

a.上传图片至服务器

b.获取图片进行处理

c.上传处理好的图片至服务器

d.根据配置,删除或者不删除服务器的原图(OSS)

  1. imagePNG($thumbImg,'Uploads/file.png'); // 中转站 
  2.  
  3. // 生成图片 
  4.  
  5. $this->putObject('Uploads/file.png',$thumbname); 
  6.  
  7. unlink('Uploads/file.png'); 
  8.  
  9. imagedestroy($thumbImg);
  10.  

出处:http://www.phpfensi.com/php/20220726/21445.html


相关教程