我們現(xiàn)在只要搜索文件上傳類(lèi)有大把,但是真正好用的上傳類(lèi)不多,下面我介紹這個(gè)文件上傳類(lèi)是我自己使用了很久,非常不錯(cuò)的一個(gè)代碼,大家可參考參考.
php文件上傳類(lèi)程序代碼如下:
- <?php
- /**
- * 文件上傳類(lèi)
- */
- class uploadFile {
- public $max_size = '1000000';//設(shè)置上傳文件大小
- public $file_name = 'date';//重命名方式代表以時(shí)間命名,其他則使用給予的名稱(chēng)
- public $allow_types;//允許上傳的文件擴(kuò)展名,不同文件類(lèi)型用“|”隔開(kāi)
- public $errmsg = '';//錯(cuò)誤信息
- public $uploaded = '';//上傳后的文件名(包括文件路徑)
- public $save_path;//上傳文件保存路徑
- private $files;//提交的等待上傳文件
- private $file_type = array();//文件類(lèi)型
- private $ext = '';//上傳文件擴(kuò)展名
- /**
- * 構(gòu)造函數(shù),初始化類(lèi)
- * @access public
- * @param string $file_name 上傳后的文件名
- * @param string $save_path 上傳的目標(biāo)文件夾
- */
- public function __construct($save_path = './upload/',$file_name = 'date',$allow_types = '') {
- $this->file_name = $file_name;//重命名方式代表以時(shí)間命名,其他則使用給予的名稱(chēng)
- $this->save_path = (preg_match('//$/',$save_path)) ? $save_path : $save_path . '/';
- $this->allow_types = $allow_types == '' ? 'jpg|gif|png|zip|rar' : $allow_types;
- }
- /**
- * 上傳文件
- * @access public
- * @param $files 等待上傳的文件(表單傳來(lái)的$_FILES[])
- * @return boolean 返回布爾值
- */
- public function upload_file($files) {
- $name = $files['name'];
- $type = $files['type'];
- $size = $files['size'];
- $tmp_name = $files['tmp_name'];
- $error = $files['error'];
- switch ($error) {
- case 0 : $this->errmsg = '';
- break;
- case 1 : $this->errmsg = '超過(guò)了php.ini中文件大小';
- break;
- case 2 : $this->errmsg = '超過(guò)了MAX_FILE_SIZE 選項(xiàng)指定的文件大小';
- break;
- case 3 : $this->errmsg = '文件只有部分被上傳';
- break;
- case 4 : $this->errmsg = '沒(méi)有文件被上傳';
- break;
- case 5 : $this->errmsg = '上傳文件大小為0';
- break;
- default : $this->errmsg = '上傳文件失敗!';
- break;
- }
- if($error == 0 && is_uploaded_file($tmp_name)) {
- //檢測(cè)文件類(lèi)型
- if($this->check_file_type($name) == FALSE){
- return FALSE;
- }//開(kāi)源代碼Vevb.com
- //檢測(cè)文件大小
- if($size > $this->max_size){
- $this->errmsg = '上傳文件<font color=red>'.$name.'</font>太大,最大支持<font color=red>'.ceil($this->max_size/1024).'</font>kb的文件';
- return FALSE;
- }
- $this->set_save_path();//設(shè)置文件存放路徑
- $new_name = $this->file_name != 'date' ? $this->file_name.'.'.$this->ext : date('YmdHis').'.'.$this->ext;//設(shè)置新文件名
- $this->uploaded = $this->save_path.$new_name;//上傳后的文件名
- //移動(dòng)文件
- if(move_uploaded_file($tmp_name,$this->uploaded)){
- $this->errmsg = '文件<font color=red>'.$this->uploaded.'</font>上傳成功!';
- return TRUE;
- }else{
- $this->errmsg = '文件<font color=red>'.$this->uploaded.'</font>上傳失敗!';
- return FALSE;
- }
- }
- }
- /**
- * 檢查上傳文件類(lèi)型
- * @access public
- * @param string $filename 等待檢查的文件名
- * @return 如果檢查通過(guò)返回TRUE 未通過(guò)則返回FALSE和錯(cuò)誤消息
- */
- public function check_file_type($filename){
- $ext = $this->get_file_type($filename);
- $this->ext = $ext;
- $allow_types = explode('|',$this->allow_types);//分割允許上傳的文件擴(kuò)展名為數(shù)組
- //echo $ext;
- //檢查上傳文件擴(kuò)展名是否在請(qǐng)?jiān)试S上傳的文件擴(kuò)展名中
- if(in_array($ext,$allow_types)){
- return TRUE;
- }else{
- $this->errmsg = '上傳文件<font color=red>'.$filename.'</font>類(lèi)型錯(cuò)誤,只支持上傳<font color=red>'.str_replace('|',',',$this->allow_types).'</font>等文件類(lèi)型!';
- return FALSE;
- }
- }
- /**
- * 取得文件類(lèi)型
- * @access public
- * @param string $filename 要取得文件類(lèi)型的目標(biāo)文件名
- * @return string 文件類(lèi)型
- */
- public function get_file_type($filename){
- $info = pathinfo($filename);
- $ext = $info['extension'];
- return $ext;
- }
- /**
- * 設(shè)置文件上傳后的保存路徑
- */
- public function set_save_path(){
- $this->save_path = (preg_match('//$/',$this->save_path)) ? $this->save_path : $this->save_path . '/';
- if(!is_dir($this->save_path)){
- //如果目錄不存在,創(chuàng)建目錄
- $this->set_dir();
- }
- }
- /**
- * 創(chuàng)建目錄
- * @access public
- * @param string $dir 要?jiǎng)?chuàng)建目錄的路徑
- * @return boolean 失敗時(shí)返回錯(cuò)誤消息和FALSE
- */
- public function set_dir($dir = null){
- //檢查路徑是否存在
- if(!$dir){
- $dir = $this->save_path;
- }
- if(is_dir($dir)){
- $this->errmsg = '需要?jiǎng)?chuàng)建的文件夾已經(jīng)存在!';
- }
- $dir = explode('/', $dir);
- foreach($dir as $v){
- if($v){
- $d .= $v . '/';
- if(!is_dir($d)){
- $state = mkdir($d, 0777);
- if(!$state)
- $this->errmsg = '在創(chuàng)建目錄<font color=red>' . $d . '時(shí)出錯(cuò)!';
- }
- }
- }
- return true;
- }
- }
- /*************************************************
- * 圖片處理類(lèi)
- *
- * 可以對(duì)圖片進(jìn)行生成縮略圖,打水印等操作
- * 本類(lèi)默認(rèn)編碼為UTF8 如果要在GBK下使用請(qǐng)將img_mark方法中打中文字符串水印iconv注釋去掉
- *
- * 由于UTF8漢字和英文字母大小(像素)不好確定,在中英文混合出現(xiàn)太多時(shí)可能會(huì)出現(xiàn)字符串偏左
- * 或偏右,請(qǐng)根據(jù)項(xiàng)目環(huán)境對(duì)get_mark_xy方法中的$strc_w = strlen($this->mark_str)*7+5進(jìn)
- * 行調(diào)整
- * 需要GD庫(kù)支持,為更好使用本類(lèi)推薦使用GD庫(kù)2.0+
- *
- * @author kickflip@php100 QQ263340607
- *************************************************/
- class uploadImg extends uploadFile {
- public $mark_str = 'kickflip@php100'; //水印字符串
- public $str_r = 0; //字符串顏色R
- public $str_g = 0; //字符串顏色G
- public $str_b = 0; //字符串顏色B
- public $mark_ttf = './upload/SIMSUN.TTC'; //水印文字字體文件(包含路徑)
- public $mark_logo = './upload/logo.png'; //水印圖片
- public $resize_h;//生成縮略圖高
- public $resize_w;//生成縮略圖寬
- public $source_img;//源圖片文件
- public $dst_path = './upload/';//縮略圖文件存放目錄,不填則為源圖片存放目錄
- /**
- * 生成縮略圖 生成后的圖
- * @access public
- * @param integer $w 縮小后圖片的寬(px)
- * @param integer $h 縮小后圖片的高(px)
- * @param string $source_img 源圖片(路徑+文件名)
- */
- public function img_resized($w,$h,$source_img = NULL){
- $source_img = $source_img == NULL ? $this->uploaded : $source_img;//取得源文件的地址,如果為空則默認(rèn)為上次上傳的圖片
- if(!is_file($source_img)) { //檢查源圖片是否存在
- $this->errmsg = '文件'.$source_img.'不存在';
- return FALSE;
- }
- $this->source_img = $source_img;
- $img_info = getimagesize($source_img);
- $source = $this->img_create($source_img); //創(chuàng)建源圖片
- $this->resize_w = $w;
- $this->resize_h = $h;
- $thumb = imagecreatetruecolor($w,$h);
- imagecopyresized($thumb,$source,0,0,0,0,$w,$h,$img_info[0],$img_info[1]);//生成縮略圖片
- $dst_path = $this->dst_path == '' ? $this->save_path : $this->dst_path; //取得目標(biāo)文件夾路徑
- $dst_path = (preg_match('//$/',$dst_path)) ? $dst_path : $dst_path . '/';//將目標(biāo)文件夾后加上/
- if(!is_dir($dst_path)) $this->set_dir($dst_path); //如果不存在目標(biāo)文件夾則創(chuàng)建
- $dst_name = $this->set_newname($source_img);
- $this->img_output($thumb,$dst_name);//輸出圖片
- imagedestroy($source);
- imagedestroy($thumb);
- }
- /**
- *打水印
- *@access public
- *@param string $source_img 源圖片路徑+文件名
- *@param integer $mark_type 水印類(lèi)型(1為英文字符串,2為中文字符串,3為圖片logo,默認(rèn)為英文字符串)
- *@param integer $mark_postion 水印位置(1為左下角,2為右下角,3為左上角,4為右上角,默認(rèn)為右下角);
- *@return 打上水印的圖片
- */
- public function img_mark($source_img = NULL,$mark_type = 1,$mark_postion = 2) {
- $source_img = $source_img == NULL ? $this->uploaded : $source_img;//取得源文件的地址,如果為空則默認(rèn)為上次上傳的圖片
- if(!is_file($source_img)) { //檢查源圖片是否存在
- $this->errmsg = '文件'.$source_img.'不存在';
- return FALSE;
- }
- $this->source_img = $source_img;
- $img_info = getimagesize($source_img);
- $source = $this->img_create($source_img); //創(chuàng)建源圖片
- $mark_xy = $this->get_mark_xy($mark_postion);//取得水印位置
- $mark_color = imagecolorallocate($source,$this->str_r,$this->str_g,$this->str_b);
- switch($mark_type) {
- case 1 : //加英文字符串水印
- $str = $this->mark_str;
- imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color);
- $this->img_output($source,$source_img);
- break;
- case 2 : //加中文字符串水印
- if(!is_file($this->mark_ttf)) { //檢查字體文件是否存在
- $this->errmsg = '打水印失敗:字體文件'.$this->mark_ttf.'不存在!';
- return FALSE;
- }
- $str = $this->mark_str;
- //$str = iconv('gbk','utf-8',$str);//轉(zhuǎn)換字符編碼 如果使用GBK編碼請(qǐng)去掉此行注釋
- imagettftext($source,12,0,$mark_xy[2],$mark_xy[3],$mark_color,$this->mark_ttf,$str);
- $this->img_output($source,$source_img);
- break;
- case 3 : //加圖片水印
- if(is_file($this->mark_logo)){ //如果存在水印logo的圖片則取得logo圖片的基本信息,不存在則退出
- $logo_info = getimagesize($this->mark_logo);
- }else{
- $this->errmsg = '打水印失敗:logo文件'.$this->mark_logo.'不存在!';
- return FALSE;
- }
- $logo_info = getimagesize($this->mark_logo);
- if($logo_info[0]>$img_info[0] || $logo_info[1]>$img_info[1]) { //如果源圖片小于logo大小則退出
- $this->errmsg = '打水印失敗:源圖片'.$this->source_img.'比'.$this->mark_logo.'小!';
- return FALSE;
- }
- $logo = $this->img_create($this->mark_logo);
- imagecopy ( $source, $logo, $mark_xy[4], $mark_xy[5], 0, 0, $logo_info[0], $logo_info[1]);
- $this->img_output($source,$source_img);
- break;
- default: //其它則為文字圖片
- $str = $this->mark_str;
- imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color);
- $this->img_output($source,$source_img);
- break;
- }
- imagedestroy($source);
- }
- /**
- * 取得水印位置
- * @access private
- * @param integer $mark_postion 水印的位置(1為左下角,2為右下角,3為左上角,4為右上角,其它為右下角)
- * @return array $mark_xy 水印位置的坐標(biāo)(索引0為英文字符串水印坐標(biāo)X,索引1為英文字符串水印坐標(biāo)Y,
- * 索引2為中文字符串水印坐標(biāo)X,索引3為中文字符串水印坐標(biāo)Y,索引4為水印圖片坐標(biāo)X,索引5為水印圖片坐標(biāo)Y)
- */
- private function get_mark_xy($mark_postion){
- $img_info = getimagesize($this->source_img);
- $stre_w = strlen($this->mark_str)*9+5 ; //水印英文字符串的長(zhǎng)度(px)(5號(hào)字的英文字符大小約為9px 為了美觀再加5px)
- //(12號(hào)字的中文字符大小為12px,在utf8里一個(gè)漢字長(zhǎng)度為3個(gè)字節(jié)一個(gè)字節(jié)4px 而一個(gè)英文字符長(zhǎng)度一個(gè)字節(jié)大小大約為9px
- // 為了在中英文混合的情況下顯示完全 設(shè)它的長(zhǎng)度為字節(jié)數(shù)*7px)
- $strc_w = strlen($this->mark_str)*7+5 ; //水印中文字符串的長(zhǎng)度(px)
- if(is_file($this->mark_logo)){ //如果存在水印logo的圖片則取得logo圖片的基本信息
- $logo_info = getimagesize($this->mark_logo);
- }
- //由于imagestring函數(shù)和imagettftext函數(shù)中對(duì)于字符串開(kāi)始位置不同所以英文和中文字符串的Y位置也有所不同
- //imagestring函數(shù)是從文字的左上角為參照 imagettftext函數(shù)是從文字左下角為參照
- switch($mark_postion){
- case 1: //位置左下角
- $mark_xy[0] = 5; //水印英文字符串坐標(biāo)X
- $mark_xy[1] = $img_info[1]-20;//水印英文字符串坐標(biāo)Y
- $mark_xy[2] = 5; //水印中文字符串坐標(biāo)X
- $mark_xy[3] = $img_info[1]-5;//水印中文字符串坐標(biāo)Y
- $mark_xy[4] = 5;//水印圖片坐標(biāo)X
- $mark_xy[5] = $img_info[1]-$logo_info[1]-5;//水印圖片坐標(biāo)Y
- break;
- case 2: //位置右下角
- $mark_xy[0] = $img_info[0]-$stre_w; //水印英文字符串坐標(biāo)X
- $mark_xy[1] = $img_info[1]-20;//水印英文字符串坐標(biāo)Y
- $mark_xy[2] = $img_info[0]-$strc_w; //水印中文字符串坐標(biāo)X
- $mark_xy[3] = $img_info[1]-5;//水印中文字符串坐標(biāo)Y
- $mark_xy[4] = $img_info[0]-$logo_info[0]-5;//水印圖片坐標(biāo)X
- $mark_xy[5] = $img_info[1]-$logo_info[1]-5;//水印圖片坐標(biāo)Y
- break;
- case 3: //位置左上角
- $mark_xy[0] = 5; //水印英文字符串坐標(biāo)X
- $mark_xy[1] = 5;//水印英文字符串坐標(biāo)Y
- $mark_xy[2] = 5; //水印中文字符串坐標(biāo)X
- $mark_xy[3] = 15;//水印中文字符串坐標(biāo)Y
- $mark_xy[4] = 5;//水印圖片坐標(biāo)X
- $mark_xy[5] = 5;//水印圖片坐標(biāo)Y
- break;
- case 4: //位置右上角
- $mark_xy[0] = $img_info[0]-$stre_w; //水印英文字符串坐標(biāo)X
- $mark_xy[1] = 5;//水印英文字符串坐標(biāo)Y
- $mark_xy[2] = $img_info[0]-$strc_w; //水印中文字符串坐標(biāo)X
- $mark_xy[3] = 15;//水印中文字符串坐標(biāo)Y
- $mark_xy[4] = $img_info[0]-$logo_info[0]-5;//水印圖片坐標(biāo)X
- $mark_xy[5] = 5;//水印圖片坐標(biāo)Y
- break;
- default : //其它默認(rèn)為右下角
- $mark_xy[0] = $img_info[0]-$stre_w; //水印英文字符串坐標(biāo)X
- $mark_xy[1] = $img_info[1]-5;//水印英文字符串坐標(biāo)Y
- $mark_xy[2] = $img_info[0]-$strc_w; //水印中文字符串坐標(biāo)X
- $mark_xy[3] = $img_info[1]-15;//水印中文字符串坐標(biāo)Y
- $mark_xy[4] = $img_info[0]-$logo_info[0]-5;//水印圖片坐標(biāo)X
- $mark_xy[5] = $img_info[1]-$logo_info[1]-5;//水印圖片坐標(biāo)Y
- break;
- }
- return $mark_xy;
- }
- /**
- * 創(chuàng)建源圖片
- * @access private
- * @param string $source_img 源圖片(路徑+文件名)
- * @return img 從目標(biāo)文件新建的圖像
- */
- private function img_create($source_img) {
- $info = getimagesize($source_img);
- switch ($info[2]){
- case 1:
- if(!function_exists('imagecreatefromgif')){
- $source = @imagecreatefromjpeg($source_img);
- }else{
- $source = @imagecreatefromgif($source_img);
- }
- break;
- case 2:
- $source = @imagecreatefromjpeg($source_img);
- break;
- case 3:
- $source = @imagecreatefrompng($source_img);
- break;
- case 6:
- $source = @imagecreatefromwbmp($source_img);
- break;
- default:
- $source = FALSE;
- break;
- }
- return $source;
- }
- /**
- * 重命名圖片
- * @access private
- * @param string $source_img 源圖片路徑+文件名
- * @return string $dst_name 重命名后的圖片名(路徑+文件名)
- */
- private function set_newname($sourse_img) {
- $info = pathinfo($sourse_img);
- $new_name = $this->resize_w.'_'.$this->resize_h.'_'.$info['basename'];//將文件名修改為:寬_高_(dá)文件名
- if($this->dst_path == ''){ //如果存放縮略圖路徑為空則默認(rèn)為源文件同文件夾
- $dst_name = str_replace($info['basename'],$new_name,$sourse_img);
- }else{
- $dst_name = $this->dst_path.$new_name;
- }
- return $dst_name;
- }
- /**
- * 輸出圖片
- * @access private
- * @param $im 處理后的圖片
- * @param $dst_name 輸出后的的圖片名(路徑+文件名)
- * @return 輸出圖片
- */
- public function img_output($im,$dst_name) {
- $info = getimagesize($this->source_img);
- switch ($info[2]){
- case 1:
- if(!function_exists('imagegif')){
- imagejpeg($im,$dst_name);
- }else{
- imagegif($im, $dst_name);
- }
- break;
- case 2:
- imagejpeg($im,$dst_name);
- break;
- case 3:
- imagepng($im,$dst_name);
- break;
- case 6:
- imagewbmp($im,$dst_name);
- break;
- }
- }
- }
- ?>
使用方法,代碼如下:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>上傳文件</title></head><body leftmargin="0" topmargin="0"><table cellpadding="2" cellspacing="1" border="0" height="100%" align="left"><form action='wend.php?action=upload' method='post' enctype='multipart/form-data'><tr ><td valign='middle'><input type='file' name='uploadfile'><input name='submit' type='submit' value='上傳'></td></tr></form></table</body></html>
php處理文件,調(diào)用上面的類(lèi)文件,然后再如下操作,代碼如下:
- $action = addslashes($_GET['action']);
- if( $action =="udd")
- {
- $state=uploadfile('uploadfile');
- //echo $state['err'];exit;
- if($state['err']){
- die('<script>alert("上傳出錯(cuò):'.$state['err'].'");history.go(-1);</script>');
- }
- else
- {
- echo '文件己上傳!<a href="upp.php">刪除重傳</a>';
- }
- }
- else{
- echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
- echo '<html xmlns="http://www.w3.org/1999/xhtml">';
- echo '<head>';
- echo '<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />';
- echo '<title>上傳文件</title>';
- //echo "<meta http-equiv="content-type" content="text/html; charset=gb2312">";
- echo '</head>';
- echo "<body leftmargin="0" topmargin="0">";
- echo "<table cellpadding="2" cellspacing="1" border="0" height="100%" align="left">";
- echo "<form action='abc.php?action=udd' method='post' enctype='multipart/form-data'>";
- echo "<tr ><td valign='middle'>";
- echo "<input type='file' name='uploadfile'>";
- echo "<input name='submit' type='submit' value='上傳'>";
- echo "</td></tr>";
- echo "</form>";
- echo "</table";
- echo "</body>";
- echo '</html>';
- }
新聞熱點(diǎn)
疑難解答