thinkphp如何上傳圖片呢?下面就為大家詳細(xì)介紹下!
1、封裝上傳類方法
?php//調(diào)用上傳html' target='_blank'>public function uploadUser(){ $banner=$this- uploadYmdImg( ymd_banner if(!empty($banner)){ $data[ ymd_banner ]=$banner; * 封裝圖片上傳 * */ public function uploadYmdImg($fileName,$route= banner ){ if(!empty($_FILES[$fileName][ tmp_name ])){ $upload = new /Think/Upload();// 實(shí)例化上傳類 $upload- maxSize = 3145728 ;// 設(shè)置附件上傳大小 $upload- exts = array( jpg , gif , png , jpeg // 設(shè)置附件上傳類型 $upload- rootPath = ./Upload/ .$route. / // 設(shè)置附件上傳根目錄 // 上傳單個(gè)文件 $info = $upload- uploadOne($_FILES[$fileName]); if(!$info) {// 上傳錯(cuò)誤提示錯(cuò)誤信息 $this- error($upload- getError()); }else{// 上傳成功 獲取上傳文件信息 return C( TMPL_PARSE_STRING.__YIMUDI__ ). /Upload/ .$route. / .$info[ savepath ].$info[ savename }else{ return NULL;}
2、Thinkphp內(nèi)部定義的上傳類
?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]// +----------------------------------------------------------------------// | Copyright (c) 2006-2016 http://thinkVeVb.com All rights reserved.// +----------------------------------------------------------------------namespace Think;class Upload { * 默認(rèn)上傳配置 * @var array private $config = array( mimes = array(), //允許上傳的文件MiMe類型 maxSize = 0, //上傳的文件大小限制 (0-不做限制) exts = array(), //允許上傳的文件后綴 autoSub = true, //自動(dòng)子目錄保存文件 subName = array( date , Y-m-d ), //子目錄創(chuàng)建方式,[0]-函數(shù)名,[1]-參數(shù),多個(gè)參數(shù)使用數(shù)組 rootPath = ./Uploads/ , //保存根路徑 savePath = , //保存路徑 saveName = array( uniqid , ), //上傳文件命名規(guī)則,[0]-函數(shù)名,[1]-參數(shù),多個(gè)參數(shù)使用數(shù)組 saveExt = , //文件保存后綴,空則使用原后綴 replace = false, //存在同名是否覆蓋 hash = true, //是否生成hash編碼 callback = false, //檢測文件是否存在回調(diào),如果存在返回文件信息數(shù)組 driver = , // 文件上傳驅(qū)動(dòng) driverConfig = array(), // 上傳驅(qū)動(dòng)配置 * 上傳錯(cuò)誤信息 * @var string private $error = //上傳錯(cuò)誤信息 * 上傳驅(qū)動(dòng)實(shí)例 * @var Object private $uploader; * 構(gòu)造方法,用于構(gòu)造上傳實(shí)例 * @param array $config 配置 * @param string $driver 要使用的上傳驅(qū)動(dòng) LOCAL-本地上傳驅(qū)動(dòng),F(xiàn)TP-FTP上傳驅(qū)動(dòng) public function __construct($config = array(), $driver = , $driverConfig = null){ /* 獲取配置 */ $this- config = array_merge($this- config, $config); /* 設(shè)置上傳驅(qū)動(dòng) */ $this- setDriver($driver, $driverConfig); /* 調(diào)整配置,把字符串配置參數(shù)轉(zhuǎn)換為數(shù)組 */ if(!empty($this- config[ mimes ])){ if(is_string($this- mimes)) { $this- config[ mimes ] = explode( , , $this- mimes); $this- config[ mimes ] = array_map( strtolower , $this- mimes); if(!empty($this- config[ exts ])){ if (is_string($this- exts)){ $this- config[ exts ] = explode( , , $this- exts); $this- config[ exts ] = array_map( strtolower , $this- exts); * 使用 $this- name 獲取配置 * @param string $name 配置名稱 * @return multitype 配置值 public function __get($name) { return $this- config[$name]; public function __set($name,$value){ if(isset($this- config[$name])) { $this- config[$name] = $value; if($name == driverConfig ){ //改變驅(qū)動(dòng)配置后重置上傳驅(qū)動(dòng) //注意:必須選改變驅(qū)動(dòng)然后再改變驅(qū)動(dòng)配置 $this- setDriver(); public function __isset($name){ return isset($this- config[$name]); * 獲取最后一次上傳錯(cuò)誤信息 * @return string 錯(cuò)誤信息 public function getError(){ return $this- error; * 上傳單個(gè)文件 * @param array $file 文件數(shù)組 * @return array 上傳成功后的文件信息 public function uploadOne($file){ $info = $this- upload(array($file)); return $info ? $info[0] : $info; * 上傳文件 * @param 文件信息數(shù)組 $files ,通常是 $_FILES數(shù)組 public function upload($files= ) { if( === $files){ $files = $_FILES; if(empty($files)){ $this- error = 沒有上傳的文件! return false; /* 檢測上傳根目錄 */ if(!$this- uploader- checkRootPath($this- rootPath)){ $this- error = $this- uploader- getError(); return false; /* 檢查上傳目錄 */ if(!$this- uploader- checkSavePath($this- savePath)){ $this- error = $this- uploader- getError(); return false; /* 逐個(gè)檢測并上傳文件 */ $info = array(); if(function_exists( finfo_open )){ $finfo = finfo_open ( FILEINFO_MIME_TYPE ); // 對(duì)上傳文件數(shù)組信息處理 $files = $this- dealFiles($files); foreach ($files as $key = $file) { $file[ name ] = strip_tags($file[ name if(!isset($file[ key ])) $file[ key ] = $key; /* 通過擴(kuò)展獲取文件類型,可解決FLASH上傳$FILES數(shù)組返回文件類型錯(cuò)誤的問題 */ if(isset($finfo)){ $file[ type ] = finfo_file ( $finfo , $file[ tmp_name ] ); /* 獲取上傳文件后綴,允許上傳無后綴文件 */ $file[ ext ] = pathinfo($file[ name ], PATHINFO_EXTENSION); /* 文件上傳檢測 */ if (!$this- check($file)){ continue; /* 獲取文件hash */ if($this- hash){ $file[ md5 ] = md5_file($file[ tmp_name $file[ sha1 ] = sha1_file($file[ tmp_name /* 調(diào)用回調(diào)函數(shù)檢測文件是否存在 */ $data = call_user_func($this- callback, $file); if( $this- callback $data ){ if ( file_exists( . .$data[ path ]) ) { $info[$key] = $data; continue; }elseif($this- removeTrash){ call_user_func($this- removeTrash,$data);//刪除垃圾據(jù) /* 生成保存文件名 */ $savename = $this- getSaveName($file); if(false == $savename){ continue; } else { $file[ savename ] = $savename; /* 檢測并創(chuàng)建子目錄 */ $subpath = $this- getSubPath($file[ name if(false === $subpath){ continue; } else { $file[ savepath ] = $this- savePath . $subpath; /* 對(duì)圖像文件進(jìn)行嚴(yán)格檢測 */ $ext = strtolower($file[ ext if(in_array($ext, array( gif , jpg , jpeg , bmp , png , swf ))) { $imginfo = getimagesize($file[ tmp_name if(empty($imginfo) || ($ext == gif empty($imginfo[ bits ]))){ $this- error = 非法圖像文件! continue; /* 保存文件 并記錄保存成功的文件 */ if ($this- uploader- save($file,$this- replace)) { unset($file[ error ], $file[ tmp_name $info[$key] = $file; } else { $this- error = $this- uploader- getError(); if(isset($finfo)){ finfo_close($finfo); return empty($info) ? false : $info; * 轉(zhuǎn)換上傳文件數(shù)組變量為正確的方式 * @access private * @param array $files 上傳的文件變量 * @return array private function dealFiles($files) { $fileArray = array(); $n = 0; foreach ($files as $key= $file){ if(is_array($file[ name ])) { $keys = array_keys($file); $count = count($file[ name for ($i=0; $i $count; $i++) { $fileArray[$n][ key ] = $key; foreach ($keys as $_key){ $fileArray[$n][$_key] = $file[$_key][$i]; $n++; }else{ $fileArray = $files; break; return $fileArray; * 設(shè)置上傳驅(qū)動(dòng) * @param string $driver 驅(qū)動(dòng)名稱 * @param array $config 驅(qū)動(dòng)配置 private function setDriver($driver = null, $config = null){ $driver = $driver ? : ($this- driver ? : C( FILE_UPLOAD_TYPE $config = $config ? : ($this- driverConfig ? : C( UPLOAD_TYPE_CONFIG $class = strpos($driver, // )? $driver : Think//Upload//Driver// .ucfirst(strtolower($driver)); $this- uploader = new $class($config); if(!$this- uploader){ E( 不存在上傳驅(qū)動(dòng):{$name} * 檢查上傳的文件 * @param array $file 文件信息 private function check($file) { /* 文件上傳失敗,捕獲錯(cuò)誤代碼 */ if ($file[ error ]) { $this- error($file[ error return false; /* 無效上傳 */ if (empty($file[ name ])){ $this- error = 未知上傳錯(cuò)誤! /* 檢查是否合法上傳 */ if (!is_uploaded_file($file[ tmp_name ])) { $this- error = 非法上傳文件! return false; /* 檢查文件大小 */ if (!$this- checkSize($file[ size ])) { $this- error = 上傳文件大小不符! return false; /* 檢查文件Mime類型 */ //TODO:FLASH上傳的文件獲取到的mime類型都為application/octet-stream if (!$this- checkMime($file[ type ])) { $this- error = 上傳文件MIME類型不允許! return false; /* 檢查文件后綴 */ if (!$this- checkExt($file[ ext ])) { $this- error = 上傳文件后綴不允許 return false; /* 通過檢測 */ return true;
case 1: $this- error = 上傳的文件超過了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值! break; case 2: $this- error = 上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值! break; case 3: $this- error = 文件只有部分被上傳! break; case 4: $this- error = 沒有文件被上傳! break; case 6: $this- error = 找不到臨時(shí)文件夾! break; case 7: $this- error = 文件寫入失敗! break; default: $this- error = 未知上傳錯(cuò)誤! * 檢查文件大小是否合法 * @param integer $size 數(shù)據(jù) private function checkSize($size) { return !($size $this- maxSize) || (0 == $this- maxSize); * 檢查上傳的文件MIME類型是否合法 * @param string $mime 數(shù)據(jù) private function checkMime($mime) { return empty($this- config[ mimes ]) ? true : in_array(strtolower($mime), $this- mimes); * 檢查上傳的文件后綴是否合法 * @param string $ext 后綴 private function checkExt($ext) { return empty($this- config[ exts ]) ? true : in_array(strtolower($ext), $this- exts); * 根據(jù)上傳文件命名規(guī)則取得保存文件名 * @param string $file 文件信息 private function getSaveName($file) { $rule = $this- saveName; if (empty($rule)) { //保持文件名不變 /* 解決pathinfo中文文件名BUG */ $filename = substr(pathinfo( _{$file[ name ]} , PATHINFO_FILENAME), 1); $savename = $filename; } else { $savename = $this- getName($rule, $file[ name if(empty($savename)){ $this- error = 文件命名規(guī)則錯(cuò)誤! return false; /* 文件保存后綴,支持強(qiáng)制更改文件后綴 */ $ext = empty($this- config[ saveExt ]) ? $file[ ext ] : $this- saveExt; return $savename . . . $ext; * 獲取子目錄的名稱 * @param array $file 上傳的文件信息 private function getSubPath($filename) { $subpath = $rule = $this- subName; if ($this- autoSub !empty($rule)) { $subpath = $this- getName($rule, $filename) . / if(!empty($subpath) !$this- uploader- mkdir($this- savePath . $subpath)){ $this- error = $this- uploader- getError(); return false; return $subpath; * 根據(jù)指定的規(guī)則獲取文件或目錄名稱 * @param array $rule 規(guī)則 * @param string $filename 原文件名 * @return string 文件或目錄名稱 private function getName($rule, $filename){ $name = if(is_array($rule)){ //數(shù)組規(guī)則 $func = $rule[0]; $param = (array)$rule[1]; foreach ($param as $value) { $value = str_replace( __FILE__ , $filename, $value); $name = call_user_func_array($func, $param); } elseif (is_string($rule)){ //字符串規(guī)則 if(function_exists($rule)){ $name = call_user_func($rule); } else { $name = $rule; return $name;}
相關(guān)推薦:
php實(shí)現(xiàn)生成混合驗(yàn)證碼與圖像驗(yàn)證碼并測試(代碼)
tp5實(shí)現(xiàn)批量上傳圖片的方法代碼
以上就是Thinkphp上傳類實(shí)現(xiàn)上傳圖片的代碼的詳細(xì)內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
|
新聞熱點(diǎn)
疑難解答
圖片精選