多文件上传类代码
真正支持单文件和多文件上传类代码,修正了$_FILES[$field]['name']中的$field不能用变量只能和表单中的文件名name="userfile"一致的缺点$_FILES['userfile']['name'],这里 中的文件名可以随意取。
//index.htm
1、单文件上传
2、多文件上传
--------------------------------------------------------------------------------------------------------------------------------
//upload.php
class File_upload{
public $upload_path='./upload/';//上传文件的路径
public $allow_type=array();//允许上传的文件类型
public $max_size='20480';//允许的最大文件大小
public $overwrite=false;//是否设置成覆盖模式
public $renamed=false;//是否直接使用上传文件的名称,还是系统自动命名
/**
* 私有变量
*/
private $upload_file=array();//保存上传成功文件的信息
private $upload_file_num=0;//上传成功文件的数目
private $succ_upload_file=array();//成功保存的文件信息
/**
* 构造器
*
* @param string $upload_path
* @param string $allow_type
* @param string $max_size
*/
public function __construct($upload_path='./upload/',$allow_type='jpg|bmp|png|gif|jpeg',$max_size='204800')
{
$this->set_upload_path($upload_path);
$this->set_allow_type($allow_type);
$this->max_size=$max_size;
$this->get_upload_files();
}
/**
* 设置上传路径,并判定
*
* @param string $path
*/
public function set_upload_path($path)
{
if(file_exists($path)){
if(is_writeable($path)){
$this->upload_path=$path;
}else{
if(@chmod($path,'0666'))
$this->upload_path=$path;
}
}else{
if(@mkdir($path,'0666')){
$this->upload_path=$path;
}
}
}
//设置上传文件类型
public function set_allow_type($types){
$this->allow_type=explode("|",$types);
}
//上传文件
public function get_upload_files()
{
foreach ($_FILES AS $key=>$field)
{
$this->get_upload_files_detial($key);
}
}
//上传文件数据存放到数组中
public function get_upload_files_detial($field){
if(is_array($_FILES["$field"]['name']))
{
for($i=0;$i
if(0==$_FILES[$field]['error'][$i])
{
$this->upload_file[$this->upload_file_num]['name']=$_FILES[$field]['name'][$i];
$this->upload_file[$this->upload_file_num]['type']=$_FILES[$field]['type'][$i];
$this->upload_file[$this->upload_file_num]['size']=$_FILES[$field]['size'][$i];
$this->upload_file[$this->upload_file_num]['tmp_name']=$_FILES[$field]['tmp_name'][$i];
$this->upload_file[$this->upload_file_num]['error']=$_FILES[$field]['error'][$i];
$this->upload_file_num++;
}
}
}
else {
if(0==$_FILES["$field"]['error'])
{
$this->upload_file[$this->upload_file_num]['name']=$_FILES["$field"]['name'];
$this->upload_file[$this->upload_file_num]['type']=$_FILES["$field"]['type'];
$this->upload_file[$this->upload_file_num]['size']=$_FILES["$field"]['size'];
$this->upload_file[$this->upload_file_num]['tmp_name']=$_FILES["$field"]['tmp_name'];
$this->upload_file[$this->upload_file_num]['error']=$_FILES["$field"]['error'];
$this->upload_file_num++;
}
}
}
/**
* 检查上传文件是构满足指定条件
*
*/
public function check($i)
{
if(!empty($this->upload_file[$i]['name'])){
//检查文件大小
if($this->upload_file[$i]['size']>$this->max_size*1024)$this->upload_file[$i]['error']=2;
//设置默认服务端文件名
$this->upload_file[$i]['filename']=$this->upload_path.$this->upload_file[$i]['name'];
//获取文件路径信息
$file_info=pathinfo($this->upload_file[$i]['name']);
//获取文件扩展名
$file_ext=$file_info['extension'];
//检查文件类型
if(!in_array($file_ext,$this->allow_type))$this->upload_file[$i]['error']=5;
//需要重命名的
if($this->renamed){
list($usec, $sec) = explode(" ",microtime());
$this->upload_file[$i]['filename']=$sec.substr($usec,2).'.'.$file_ext;
unset($usec);
unset($sec);
}
//检查文件是否存在
if(file_exists($this->upload_file[$i]['filename'])){
if($this->overwrite){
@unlink($this->upload_file[$i]['filename']);
}else{
$j=0;
do{
$j++;
$temp_file=str_replace('.'.$file_ext,'('.$j.').'.$file_ext,$this->upload_file[$i]['filename']);
}while (file_exists($temp_file));
$this->upload_file[$i]['filename']=$temp_file;
unset($temp_file);
unset($j);
}
}
//检查完毕
} else $this->upload_file[$i]['error']=6;
}
/**
* 上传文件
*
* @return true
*/
public function upload()
{
$upload_msg='';
for($i=0;$iupload_file_num;$i++)
{
if(!empty($this->upload_file[$i]['name']))
{
//检查文件
$this->check($i);
if (0==$this->upload_file[$i]['error'])
{
//上传文件
if(!@move_uploaded_file($this->upload_file[$i]['tmp_name'],$this->upload_file[$i]['filename']))
{
$upload_msg.='上传文件'.$this->upload_file[$i]['name'].' 出错:'.$this->error($this->upload_file[$i]['error']).'!
';
}else
{
$this->succ_upload_file[]=$this->upload_file[$i]['filename'];
$upload_msg.='上传文件'.$this->upload_file[$i]['name'].' 成功了
';
}
}else $upload_msg.='上传文件'.$this->upload_file[$i]['name'].' 出错:'.$this->error($this->upload_file[$i]['error']).'!
';
}
}
echo $upload_msg;
}
//错误信息
public function error($error)
{
switch ($error) {
case 1:
return '文件大小超过php.ini 中 upload_max_filesize 选项限制的值';
break;
case 2:
return '文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
break;
case 3:
return '文件只有部分被上传';
break;
case 4:
return '没有文件被上传';
break;
case 5:
return '这个文件不允许被上传';
break;
case 6:
return '文件名为空';
break;
default:
return '出错';
break;
}
}
//获取成功的数据信息为数组(备用)
public function get_succ_file(){
return $this->succ_upload_file;
}
}
$upload=new File_upload('./upload/','jpg|bmp|png|gif|jpeg');
$upload->upload();
$t=$upload->get_succ_file();
print_r($t);
?>

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在電腦中刪除或解壓縮資料夾,時有時會彈出提示對話框“錯誤0x80004005:未指定錯誤”,如果遇到這中情況應該怎麼解決呢?提示錯誤碼0x80004005的原因其實很多,但大部分因為病毒導致,我們可以重新註冊dll來解決問題,下面,小編給大夥講解0x80004005錯誤代碼處理經驗。有使用者在使用電腦時出現錯誤代碼0X80004005的提示,0x80004005錯誤主要是由於電腦沒有正確註冊某些動態連結庫文件,或電腦與Internet之間存在不允許的HTTPS連接防火牆所引起。那麼如何

夸克網盤和百度網盤都是現在最常用的儲存文件的網盤軟體,如果想要將夸克網盤內的文件保存到百度網盤,要怎麼操作呢?本期小編整理了夸克網盤電腦端的檔案轉移到百度網盤的教學步驟,一起來看看是怎麼操作吧。 夸克網盤的檔案怎麼存到百度網盤?要將夸克網盤的文件轉移到百度網盤,首先需在夸克網盤下載所需文件,然後在百度網盤用戶端中選擇目標資料夾並開啟。接著,將夸克網盤中下載的檔案拖放到百度網盤用戶端開啟的資料夾中,或使用上傳功能將檔案新增至百度網盤。確保上傳完成後在百度網盤中查看檔案是否已成功轉移。這樣就

最近有很多網友問小編,hiberfil.sys是什麼文件? hiberfil.sys佔用了大量的C碟空間可以刪除嗎?小編可以告訴大家hiberfil.sys檔是可以刪除的。下面就來看看詳細的內容。 hiberfil.sys是Windows系統中的隱藏文件,也是系統休眠文件。通常儲存在C盤根目錄下,其大小與系統安裝記憶體大小相當。這個檔案在電腦休眠時被使用,其中包含了當前系統的記憶體數據,以便在恢復時快速恢復到先前的狀態。由於其大小與記憶體容量相等,因此它可能會佔用較大的硬碟空間。 hiber

如果您需要遠端編程任何設備,這篇文章會為您帶來幫助。我們將分享編程任何設備的頂級GE通用遠端代碼。通用電氣的遙控器是什麼? GEUniversalRemote是一款遙控器,可用於控制多個設備,如智慧電視、LG、Vizio、索尼、藍光、DVD、DVR、Roku、AppleTV、串流媒體播放器等。 GEUniversal遙控器有各種型號,具有不同的功能和功能。 GEUniversalRemote最多可以控制四台設備。頂級通用遙控器代碼,可在任何裝置上編程GE遙控器配備一組代碼,使其能夠與不同設備配合。您可

1.打開酷狗音樂,點選個人頭像。 2、點選右上角設定的圖示。 3.點選【上傳音樂作品】。 4.點選【上傳作品】。 5.選擇歌曲,然後點選【下一步】。 6.最後點選【上傳】即可。

身為一名程式設計師,對於能夠簡化程式設計體驗的工具,我感到非常興奮。借助人工智慧工具的幫助,我們可以產生演示程式碼,並根據需求進行必要的修改。在VisualStudioCode中新引入的Copilot工具讓我們能夠創建具有自然語言聊天互動的AI生成程式碼。透過解釋功能,我們可以更好地理解現有程式碼的含義。如何使用Copilot產生程式碼?要開始,我們首先需要取得最新的PowerPlatformTools擴充。要實現這一點,你需要進入擴充頁面,搜尋“PowerPlatformTool”,然後點擊Install按鈕

MySQL中.ibd檔案的作用詳解及相關注意事項MySQL是一種流行的關聯式資料庫管理系統,資料庫中的資料儲存在不同的檔案中。其中,.ibd檔案是InnoDB儲存引擎中的資料文件,用於儲存表格中的資料和索引。本文將對MySQL中.ibd檔案的作用進行詳細解析,並提供相關程式碼範例以幫助讀者更好地理解。一、.ibd檔的作用:儲存資料:.ibd檔是InnoDB存

在Linux系統中,可以使用下列指令來查看日誌檔案的內容:tail指令:tail指令用來顯示日誌檔案的末尾內容。它是查看最新日誌資訊的常用命令。 tail[選項][檔案名稱]常用的選項包括:-n:指定要顯示的行數,預設為10行。 -f:即時監視文件內容,並在文件更新時自動顯示新的內容。範例:tail-n20logfile.txt#顯示logfile.txt檔案的最後20行內容tail-flogfile.txt#即時監視logfile.txt檔案的更新內容head指令:head指令用於顯示記錄檔的開頭
