分开清晰点,改起来方便点,根据两位的建议,改了下,上传用同一个函数 upload(),可自行判断是否为多文件上传。
editplus里编辑,演示+类代码共140行,4KB,功能上大家还有什么意见?
1<style>
2body{font:12px verdana;}
3</style>
1<pre>
// 在 4.1.0 以前的 PHP 中,需要用 $HTTP_POST_FILES 代替 $_FILES。
// 在 4.0.3 以前的 PHP 中,需要用 copy() 和 is_uploaded_file() 来代替 move_uploaded_file()。
/**
- Short description. fileupload
- @author gu1dai
- @version 0.1
- @copyright [email protected]
*/
class fileupload
{
var $uploaddir ;
var $filename = 'userfile';
var $maxsize = 2048888;
var $extname = 'html,txt,htm';
function setdir(){
$uploaddir = $this->uploaddir;
if (!is_dir($uploaddir)) {
$this->mkdirp($uploaddir);
}
}//end func setdir
function upload(){
if (is_array($_FILES[$this->filename]['name'])) {
$this->uploadMulti();
echo 'multi upload';
}
else {
$this->uploadSingle();
echo 'single upload';
}
}
function uploadSingle(){
$fname = $_FILES[$this->filename]['name'];
if ($this->checkFileExt($fname)) {
$fsize = $_FILES[$this->filename]['size'];
if ($fsize <$this->maxsize) {
$uploadfile = $this->uploaddir . $this->filename;
$tmpfile = $_FILES[$this->filename]['tmp_name'];
echo "$uploadfile = $tmpfile ".$fsize."\n";
if (move_uploaded_file($tmpfile, $uploadfile)) {
print "$fname File is valid, and was successfully uploaded.\n";
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
}//size end
else {
$fsize /=1024*1024;
echo "$fname size is $fsize MB:the max upload size is 1MB";
}
}//fname
}//end func uploadSingle
/**
- Short description. uploadMulti is used to upload lots of files
/
function uploadMulti ()
{
foreach ($_FILES[$this->filename]['name'] as $key=>$fname) {
if ($this->checkFileExt($fname)) {
$fsize = $_FILES[$this->filename]['size'][$key];
if ($fsize <$this->maxsize) {
$uploadfile = $this->uploaddir . $fname;
$tmpfile = $_FILES[$this->filename]['tmp_name'][$key];
echo "$uploadfile = $tmpfile ".$fsize."\n";
if (move_uploaded_file($tmpfile, $uploadfile)) {
print "$fname File is valid, and was successfully uploaded.\n";
//print_r($_FILES);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
}//size end
else {
$fsize /=10241024;
echo "$fname size is $fsize MB:the max upload size is 1MB";
}
}//fname
}//for
} // end func uploadMulti
function checkFileExt($fname){
$path_parts = pathinfo($fname);
$ext = $path_parts["extension"];
$re = "/$ext/i";
preg_match($re,$this->extname, $matches);
if ($matches[0]) //是文本文件扩展名为:.php,.xml,.css,.js.......由数组$matches定义
{
return true;
}
return false;
}// end func
/**
- mkdirp is used to instead of mkdir ,mkdirp can create deep multiple directory.
*/
function mkdirp($target) {
// If the path already exists && is a directory, all is well.
// If the path is not a directory, we've a problem.
if (file_exists($target)) {
if (!is_dir($target)) return false;
else return true;
}
// Attempting to create the directory may clutter up our display.
if ( @mkdir($target) ) return true;
// If the above failed, attempt to create the parent node, then try again.
if ( $this->mkdirp(dirname($target)) ) return $this->mkdirp($target);
return false;
}//
} // end class
if ($_FILES['userfile']<>'') {
$mu = new fileupload;
$mu->filename = 'userf';
$mu->extname = 'html,htm,txt,zip,rar';
$mu->maxsize = 110241024;
$mu->uploaddir = 'd:/usr/www/uploads/';
$mu->upload();
}
1<form action="?" enctype="multipart/form-data" method="POST">
2<input name="userfile[]" type="file"/>
3<input name="userfile[]" type="file"/>
4<input name="userfile[]" type="file"/>
5s<input name="userf" type="file"/>
6<input type="submit" value="submit"/>
7</form></pre>