[class]fileupload,已有功能:检查文件扩展名,文件大小,批量上传。适用版本,php4.2以上

分开清晰点,改起来方便点,根据两位的建议,改了下,上传用同一个函数 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-&gt;uploaddir;
if (!is_dir($uploaddir)) {
$this-&gt;mkdirp($uploaddir);
}
}//end func setdir

function upload(){
if (is_array($_FILES[$this-&gt;filename]['name'])) {
$this-&gt;uploadMulti();
echo 'multi upload';
}
else {
$this-&gt;uploadSingle();
echo 'single upload';
}
}

function uploadSingle(){
$fname = $_FILES[$this-&gt;filename]['name'];
if ($this-&gt;checkFileExt($fname)) {
$fsize = $_FILES[$this-&gt;filename]['size'];
if ($fsize &lt;$this-&gt;maxsize) {
$uploadfile = $this-&gt;uploaddir . $this-&gt;filename;
$tmpfile = $_FILES[$this-&gt;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-&gt;filename]['name'] as $key=&gt;$fname) {
    if ($this-&gt;checkFileExt($fname)) {
    $fsize = $_FILES[$this-&gt;filename]['size'][$key];
    if ($fsize &lt;$this-&gt;maxsize) {
    $uploadfile = $this-&gt;uploaddir . $fname;
    $tmpfile = $_FILES[$this-&gt;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 /=1024
    1024;
    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-&gt;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 &amp;&amp; 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-&gt;mkdirp(dirname($target)) ) return $this-&gt;mkdirp($target);

return false;
}//

} // end class

if ($_FILES['userfile']&lt;&gt;'') {
$mu = new fileupload;
$mu-&gt;filename = 'userf';
$mu-&gt;extname = 'html,htm,txt,zip,rar';
$mu-&gt;maxsize = 110241024;
$mu-&gt;uploaddir = 'd:/usr/www/uploads/';
$mu-&gt;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>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus