PHP封裝的多文件上傳類實(shí)例與用法詳解
本文實(shí)例講述了PHP封裝的多文件上傳類實(shí)例與用法。分享給大家供大家參考,具體如下:
<?php
/**//*
* @(#)UploadFile.php
*
* 可同時(shí)處理用戶多個(gè)上傳文件。效驗(yàn)文件有效性后存儲至指定目錄。
* 可返回上傳文件的相關(guān)有用信息供其它程序使用。(如文件名、類型、大小、保存路徑)
* 使用方法請見本類底部(UploadFile類使用注釋)信息。
*
*/
class UploadFile {
var $user_post_file = array(); //用戶上傳的文件
var $save_file_path; //存放用戶上傳文件的路徑
var $max_file_size; //文件最大尺寸
var $last_error; //記錄最后一次出錯(cuò)信息
//默認(rèn)允許用戶上傳的文件類型
var $allow_type = array('gif', 'jpg', 'png', 'zip', 'rar', 'txt', 'doc', 'pdf');
var $final_file_path; //最終保存的文件名
var $save_info = array(); //返回一組有用信息,用于提示用戶。
/**//**
* 構(gòu)造函數(shù),用與初始化相關(guān)信息,用戶待上傳文件、存儲路徑等
*
* @param Array $file 用戶上傳的文件
* @param String $path 存儲用戶上傳文件的路徑
* @param Integer $size 允許用戶上傳文件的大小(字節(jié))
* @param Array $type 此數(shù)組中存放允計(jì)用戶上傳的文件類型
*/
function UploadFile($file, $path, $size = 2097152, $type = '') {
$this->user_post_file = $file;
$this->save_file_path = $path;
$this->max_file_size = $size; //如果用戶不填寫文件大小,則默認(rèn)為2M.
if ($type != '')
$this->allow_type = $type;
}
/**//**
* 存儲用戶上傳文件,檢驗(yàn)合法性通過后,存儲至指定位置。
* @access public
* @return int 值為0時(shí)上傳失敗,非0表示上傳成功的個(gè)數(shù)。
*/
function upload() {
for ($i = 0; $i < count($this->user_post_file['name']); $i++) {
//如果當(dāng)前文件上傳功能,則執(zhí)行下一步。
if ($this->user_post_file['error'][$i] == 0) {
//取當(dāng)前文件名、臨時(shí)文件名、大小、擴(kuò)展名,后面將用到。
$name = $this->user_post_file['name'][$i];
$tmpname = $this->user_post_file['tmp_name'][$i];
$size = $this->user_post_file['size'][$i];
$mime_type = $this->user_post_file['type'][$i];
$type = $this->getFileExt($this->user_post_file['name'][$i]);
//檢測當(dāng)前上傳文件大小是否合法。
if (!$this->checkSize($size)) {
$this->last_error = "The file size is too big. File name is: ".$name;
$this->halt($this->last_error);
continue;
}
//檢測當(dāng)前上傳文件擴(kuò)展名是否合法。
if (!$this->checkType($type)) {
$this->last_error = "Unallowable file type: .".$type." File name is: ".$name;
$this->halt($this->last_error);
continue;
}
//檢測當(dāng)前上傳文件是否非法提交。
if(!is_uploaded_file($tmpname)) {
$this->last_error = "Invalid post file method. File name is: ".$name;
$this->halt($this->last_error);
continue;
}
//移動文件后,重命名文件用。
$basename = $this->getBaseName($name, ".".$type);
//移動后的文件名
$saveas = $basename."-".time().".".$type;
//組合新文件名再存到指定目錄下,格式:存儲路徑 + 文件名 + 時(shí)間 + 擴(kuò)展名
$this->final_file_path = $this->save_file_path."/".$saveas;
if(!move_uploaded_file($tmpname, $this->final_file_path)) {
$this->last_error = $this->user_post_file['error'][$i];
$this->halt($this->last_error);
continue;
}
//存儲當(dāng)前文件的有關(guān)信息,以便其它程序調(diào)用。
$this->save_info[] = array("name" => $name, "type" => $type,
"mime_type" => $mime_type,
"size" => $size, "saveas" => $saveas,
"path" => $this->final_file_path);
}
}
return count($this->save_info); //返回上傳成功的文件數(shù)目
}
/**//**
* 返回一些有用的信息,以便用于其它地方。
* @access public
* @return Array 返回最終保存的路徑
*/
function getSaveInfo() {
return $this->save_info;
}
/**//**
* 檢測用戶提交文件大小是否合法
* @param Integer $size 用戶上傳文件的大小
* @access private
* @return boolean 如果為true說明大小合法,反之不合法
*/
function checkSize($size) {
if ($size > $this->max_file_size) {
return false;
}
else {
return true;
}
}
/**//**
* 檢測用戶提交文件類型是否合法
* @access private
* @return boolean 如果為true說明類型合法,反之不合法
*/
function checkType($extension) {
foreach ($this->allow_type as $type) {
if (strcasecmp($extension , $type) == 0)
return true;
}
return false;
}
/**//**
* 顯示出錯(cuò)信息
* @param $msg 要顯示的出錯(cuò)信息
* @access private
*/
function halt($msg) {
printf("<b><UploadFile Error:></b> %s <br>\n", $msg);
}
/**//**
* 取文件擴(kuò)展名
* @param String $filename 給定要取擴(kuò)展名的文件
* @access private
* @return String 返回給定文件擴(kuò)展名
*/
function getFileExt($filename) {
$stuff = pathinfo($filename);
return $stuff['extension'];
}
/**//**
* 取給定文件文件名,不包括擴(kuò)展名。
* eg: getBaseName("j:/hexuzhong.jpg"); //返回 hexuzhong
*
* @param String $filename 給定要取文件名的文件
* @access private
* @return String 返回文件名
*/
function getBaseName($filename, $type) {
$basename = basename($filename, $type);
return $basename;
}
}
/**//******************** UploadFile類使用注釋
//注意,上傳組件name屬性不管是一個(gè)還是多個(gè)都要使用數(shù)組形式,如:
<input type="file" name="user_upload_file[]">
<input type="file" name="user_upload_file[]">
//如果用戶點(diǎn)擊了上傳按鈕。
if ($_POST['action'] == "上傳") {
//設(shè)置允許用戶上傳的文件類型。
$type = array('gif', 'jpg', 'png', 'zip', 'rar');
//實(shí)例化上傳類,第一個(gè)參數(shù)為用戶上傳的文件組、第二個(gè)參數(shù)為存儲路徑、
//第三個(gè)參數(shù)為文件最大大小。如果不填則默認(rèn)為2M
//第四個(gè)參數(shù)為充許用戶上傳的類型數(shù)組。如果不填則默認(rèn)為gif, jpg, png, zip, rar, txt, doc, pdf
$upload = new UploadFile($_FILES['user_upload_file'], 'j:/tmp', 100000, $type);
//上傳用戶文件,返回int值,為上傳成功的文件個(gè)數(shù)。
$num = $upload->upload();
if ($num != 0) {
echo "上傳成功<br>";
//取得文件的有關(guān)信息,文件名、類型、大小、路徑。用print_r()打印出來。
print_r($upload->getSaveInfo());
//格式為: Array
// (
// [0] => Array(
// [name] => example.txt
// [type] => txt
// [size] => 526
// [path] => j:/tmp/example-1108898806.txt
// )
// )
echo $num."個(gè)文件上傳成功";
}
else {
echo "上傳失敗<br>";
}
}
*/
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php連接Access數(shù)據(jù)庫錯(cuò)誤及解決方法
前二天把一個(gè)asp+access的網(wǎng)站改成php+access的,在連連數(shù)據(jù)庫時(shí)可真讓我狠狠的郁悶了一把,通過百度了大量的相關(guān)文章終于解決了2013-06-06
PHP laravel實(shí)現(xiàn)導(dǎo)出PDF功能
有時(shí)候我們會需要使用PHP導(dǎo)出pdf。這篇文章主要是記錄一下laravel實(shí)現(xiàn)導(dǎo)出PDF的兩種方式。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-10-10
從一個(gè)不錯(cuò)的留言本弄的mysql數(shù)據(jù)庫操作類
本文通過實(shí)例代碼給大家介紹了mysql數(shù)據(jù)庫操作類的相關(guān)知識,感興趣的朋友跟隨腳本之家小編一起看看吧2007-09-09
深入file_get_contents與curl函數(shù)的詳解
本篇文章是對file_get_contents與curl函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP解析url并得到url參數(shù)方法總結(jié)
在本篇文章里我們給大家總結(jié)了關(guān)于PHP解析url并得到url參數(shù)的方法內(nèi)容,需要的朋友們參考下。2018-10-10

