提醒:
Form部分要注意,第一记得添加enctype=”multipart/form-data”,另外input的name=在上传多文件时要定义为数组,即:uploadinput[],如果只上传一个文件可以写为name=”uploadinput”。
Html 部分:
//__________________________________________________________________
<form enctype=”multipart/form-data” action=”upload_datei.php” method=”POST”>
Send this file: <br>
<input type=”file”><br>
<input type=”file”><br>
<input type=”file”><br>
<input type=”submit” value=”Send File”>
</form>
PHP 接受和处理文件信息部分
//__________________________________________________________________
<?php
include_once “includes/classes/class_upload.php”;
// 所允许上传的文件类型
$fileFormat = array(’gif’,'jpg’,'jpge’,'zip’,'rar’,'pdf’,'7z’,'html’,'doc’,'gz’,'tar’);
$savepath = “upload/”; // 文件上传目录
// 文件大小限制,0 表示无限制,但受php.ini中upload_max_filesize设置影响
$maxsize = 0;
$overwrite = 0; // 覆盖 0 不允许 1 允许
$f = new clsUpload( $savepath, $fileFormat, $maxsize, $overwrite);
//下面的uploadinput是Form中上传文件的input的名字
if (!$f->run(’uploadinput’)){
echo $f->errmsg(); //这里只能传递最后一个出错的信息,详细的信息在$f->getInfo()中可以得到。
}
print_r($f->returnArray);
/*输出错误和得到的结果数组如下
The uploaded file is Unallowable!
Array(
[0] => Array(
[name] => a.doc
[saveName] => 20051122014533_895.doc
[size] => 41
[type] => application/msword
)
[1] => Array(
[name] => aa.htm
[saveName] => 20051122014533_377.htm
[size] => 0
[type] => text/html
[error] => The uploaded file is Unallowable!
)
[2] => Array(
[name] => aaa.php
[saveName] => 20051122014533_840.php
[size] => 0
[type] => text/html
[error] => The uploaded file is Unallowable!
)
)
*/
?>
类文件
//__________________________________________________________________
<?php /*
○︿
︶
√ ﹀ . 。o O . ~~
TITLE : CLASS clsUpload
FILE : class_upload.php
DESCRIPTION : To provide upload utility,
AUTHOR : Peng Zhang zpadmin AT gmail DOT com
http://blog.neten.de
BASED ON : whxbb(whxbb AT 21cn DOT com)
kingerq AT msn DOT com
WRITED : 2005 NOV 20
MODIFIED : 2005 NOV 22
REVISION : V1.0.0
*/
class clsUpload{
var $saveName; // 保存名
var $savePath; // 保存路径
var $fileFormat = array(’gif’,'jpg’,'jpge’,'zip’,'rar’,'pdf’,'7z’,'html’,'doc’,'gz’,'tar’);// 文件格式限定
var $overwrite = 0;// 覆盖模式
var $maxSize = 0;// 文件最大字节
var $ext;// 文件扩展名
var $errno;// 错误代号
var $returnArray= array();// 所有文件的返回信息
var $returninfo= array();// 每个文件返回信息
//构造函数
// @param savePath 文件保存路径
// @param fileFormat 文件格式限制数组
// @param maxSize 文件最大尺寸
// @param overwriet 是否覆盖 1 允许覆盖 0 禁止覆盖
function clsUpload($savePath, $fileFormat=”,$maxSize = 0, $overwrite = 0) {
$this->setSavepath($savePath);
$this->setFileformat($fileFormat);
$this->setMaxsize($maxSize);
$this->setOverwrite($overwrite);
$this->errno = 0;
}
// 上传
function run($fileInput,$changeName = 1){
if(isset($_FILES[$fileInput])){
$fileArr = $_FILES[$fileInput];
if(is_array($fileArr['name'])){//上传同文件域名称多个文件
for($i = 0; $i < count($fileArr['name']); $i++){
$ar['tmp_name'] = $fileArr['tmp_name'][$i];
$ar['name'] = $fileArr['name'][$i];
$ar['type'] = $fileArr['type'][$i];
$ar['size'] = $fileArr['size'][$i];
$ar['error'] = $fileArr['error'][$i];
$this->getExt($ar['name']);//取得扩展名,赋给$this->ext,下次循环会更新
$this->setSavename($changeName == 1 ? ” : $ar['name']);//设置保存文件名
if($this->copyfile($ar)){
$this->returnArray[] = $this->returninfo;
}else{
$this->returninfo['error'] = $this->errmsg();
$this->returnArray[] = $this->returninfo;
}
}
return $this->errno ? false : true;
}else{//上传单个文件
$this->getExt($fileArr['name']);//取得扩展名
$this->setSavename($changeName == 1 ? ” : $fileArr['name']);//设置保存文件名
if($this->copyfile($fileArr)){
$this->returnArray[] = $this->returninfo;
}else{
$this->returninfo['error'] = $this->errmsg();
$this->returnArray[] = $this->returninfo;
}
return $this->errno ? false : true;
}
return false;
}else{
$this->errno = 10;
return false;
}
}
// 单个文件上传
function copyfile($fileArray){
$this->returninfo = array();
// 返回信息
$this->returninfo['name'] = $fileArray['name'];
$this->returninfo['saveName'] = $this->saveName;
$this->returninfo['size'] = number_format( ($fileArray['size'])/1024 , 0, ‘.’, ‘ ‘);//以 B 为单位
$this->returninfo['type'] = $fileArray['type'];
// 检查文件格式
if (!$this->validateFormat()){
$this->errno = 11;
return false;
}
// 检查目录是否可写
if(!@is_writable($this->savePath)){
$this->errno = 12;
return false;
}
// 如果不允许覆盖,检查文件是否已经存在
if($this->overwrite == 0 && @file_exists($this->savePath.$fileArray['name'])){
$this->errno = 13;
return false;
}
// 如果有大小限制,检查文件是否超过限制
if ($this->maxSize != 0 ){
if ($fileArray["size"] > $this->maxSize){
$this->errno = 14;
return false;
}
}
// 文件上传
if(!@copy($fileArray["tmp_name"], $this->savePath.$this->saveName)){
$this->errno = $fileArray["error"];
return false;
}
// 删除临时文件
if(!@$this->del($fileArray["tmp_name"])){
return false;
}
return true;
}
// 文件格式检查
function validateFormat(){
if(!is_array($this->fileFormat) || in_array(strtolower($this->ext), $this->fileFormat) )
return true;
else
return false;
}
//获取文件扩展名
function getExt($fileName){
$ext = explode(”.”, $fileName);
$ext = $ext[count($ext) - 1];
$this->ext = $ext;
}
//设置上传文件的最大字节限制
// @param $maxSize 文件大小(bytes) 0:表示无限制
function setMaxsize($maxSize){
$this->maxSize = $maxSize;
}
//设置文件格式限定
// @param $fileFormat 文件格式数组
function setFileformat($fileFormat){
if(is_array($fileFormat)){$this->fileFormat = $fileFormat ;}
}
//设置覆盖模式
// @param overwrite 覆盖模式 1:允许覆盖 0:禁止覆盖
function setOverwrite($overwrite){
$this->overwrite = $overwrite;
}
//设置保存路径
// @param $savePath 文件保存路径:以 “/” 结尾,若没有 “/”,则补上
function setSavepath($savePath){
$this->savePath = substr( str_replace(”",”/”, $savePath) , -1) == “/” ? $savePath : $savePath.”/”;
}
//设置文件保存名
// @saveName 保存名,如果为空,则系统自动生成一个随机的文件名
function setSavename($saveName){
if ($saveName == ”){ // 如果未设置文件名,则生成一个随机文件名
$name = date(’YmdHis’).”_”.rand(100,999).’.’.$this->ext;
} else {
$name = $saveName;
}
$this->saveName = $name;
}
//删除文件
// @param $fileName 所要删除的文件名
function del($fileName){
if(!@unlink($fileName)){
$this->errno = 15;
return false;
}
return true;
}
// 返回上传文件的信息
function getInfo(){
return $this->returnArray;
}
// 得到错误信息
function errmsg(){
$uploadClassError = array(
0 =>’There is no error, the file uploaded with success. ‘,
1 =>’The uploaded file exceeds the upload_max_filesize directive in php.ini.’,
2 =>’The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. ‘,
3 =>’The uploaded file was only partially uploaded. ‘,
4 =>’No file was uploaded. ‘,
6 =>’Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3. ‘,
7 =>’Failed to write file to disk. Introduced in PHP 5.1.0. ‘,
10 =>’Input name is not unavailable!’,
11 =>’The uploaded file is Unallowable!’,
12 =>’Directory unwritable!’,
13 =>’File exist already!’,
14 =>’File is too big!’,
15 =>’Delete file unsuccessfully!’,
);
if ($this->errno == 0)
return false;
else
return $uploadClassError[$this->errno];
}
}
?>