<?
//Create By Rain, Jan 20 2007
//Update By Rain, Feb 2 2007
class Upload
{
var $fileMaxSize = 0; //当不大于0时,表示上传文件大小只受服务器配置的上传文件大小限制
var $acceptType = ""; //for example: $acceptType = "image|audio|video"
var $refuseType = ""; //for example: $refuseType = "application|text"
var $acceptExt = ""; //for example: $acceptExt = "jpg|gif|png|mp3|wma"
var $refuseExt = ""; //for example: $refuseExt = "php|php3|asp|aspx|jsp|exe|com"
var $error = 0; //0:无错误 1:上传失败 2:文件已存在(不覆盖模式上传) 3:非法的文件类型 4:非法的文件扩展名 5:文件大小超过限制
function Upload() {
}
function Save($overwrite = 1) {
return $this->SaveTo(realpath("./"), $overwrite);
}
function SaveTo($uploadDir, $overwrite = 1)
{
$i = 0;
foreach ($_FILES as $file) {
if ($this->checkFileSize($file) && $this->checkFileType($file) && $this->checkFileExt($file)) {
if ($overwrite || !file_exists($uploadDir . $file['name']))
if (move_uploaded_file($file['tmp_name'], $uploadDir . "/" . $file['name']))
$isOK[$i] = $file['name'];
else {
$isOK[$i] = false;
$this->error = 1;
}
else {
$isOK[$i] = false;
$this->error = 2;
}
}
else
$isOK[$i] = false;
$i++;
}
return $isOK;
}
function SaveAsTimeStrTo($uploadDir, $overwrite = 1) {
$i = 0;
foreach ($_FILES as $file) {
if ($this->checkFileSize($file) && $this->checkFileType($file) && $this->checkFileExt($file)) {
if ($overwrite || !file_exists($uploadDir . $file['name'])) {
if (!$i) {
$fileName = date("ymdHis");
}
$fullfileName = $fileName . "_" . $i . "." . $this->getFileExt($file['name']);
if (move_uploaded_file($file['tmp_name'], $uploadDir . $fullfileName))
$isOK[$i] = $uploadDir . $fullfileName;
else {
$isOK[$i] = false;
$this->error = 1;
}
$i++;
}
else {
$isOK[$i] = false;
$this->error = 2;
}
}
else
$isOK[$i] = false;
}
return $isOK;
}
function fileSave($formFile, $overwrite = 1) {
return $this->fileSaveTo($formFile, realpath("./"), $overwrite);
}
function fileSaveTo($formFile, $uploadDir, $overwrite = 1) {
return $this->fileSaveAs($formFile, $uploadDir, $_FILES[$formFile]['name'], $overwrite);
}
function fileSaveAs($formFile, $uploadDir, $fileName, $overwrite = 1) {
//$isOK = true;
$file = $_FILES[$formFile];
if ($this->checkFileSize($file) && $this->checkFileType($file) && $this->checkFileExt($file)) {
if ($overwrite || !file_exists($uploadDir . $fileName)) {
if (move_uploaded_file($file['tmp_name'], $uploadDir . "/" . $fileName))
$isOK = true;
else {
$isOK = false;
$this->error = 1;
}
}
else {
$isOK = false;
$this->error = 2;
}
}
else
$isOK = false;
return $isOK;
}
function setRefuseType($refuseType) {
$this->refuseType = $this->my_preg_replace($refuseType);
}
function setAcceptType($acceptType) {
$this->acceptType = $this->my_preg_replace($acceptType);
}
function checkFileType($file) {
if ($this->acceptType != "") {
if (!preg_match($this->acceptType, $this->getFileType($file['type']))) {
$this->error = 3;
return false;
}
}
if ($this->refuseType != "") {
if (preg_match($this->refuseType, $this->getFileType($file['type']))) {
$this->error = 3;
return false;
}
}
return true;
}
function getFileType($fileType) {
$i = strrpos($fileType, "/");
if ($i > 0)
return substr($fileType, 0, $i);
else
return "";
}
function setRefuseExt($refuseExt) {
$this->refuseExt = $this->my_preg_replace($refuseExt);
}
function setAcceptExt($acceptExt) {
$this->acceptExt = $this->my_preg_replace($acceptExt);
}
function checkFileExt($file) {
if ($this->acceptExt != "") {
if (!preg_match($this->acceptExt, $this->getFileExt($file['name']))) {
$this->error = 4;
return false;
}
}
if ($this->refuseExt != "") {
if (preg_match($this->refuseExt, $this->getFileExt($file['name']))) {
$this->error = 4;
return false;
}
}
return true;
}
function getFileExt($fileName) {
$i = strrpos($fileName, ".");
if ($i > 0)
return substr($fileName, $i + 1);
else
return "";
}
function setFileMaxSize($fileMaxSize) {
$this->fileMaxSize = $fileMaxSize;
}
function checkFileSize($file) {
if ($file['size'] > 0) {
if ($this->fileMaxSize > 0) {
if ($file['size'] > $this->fileMaxSize) {
$this->error = 5;
return false;
}
}
return true;
}
return false;
}
function getError() {
return $this->error;
}
function printError() {
switch ($this->error) {
case 1:
$strErr = "上传失败"; break;
case 2:
$strErr = "文件已存在"; break;
case 3:
$strErr = "非法的文件类型"; break;
case 4:
$strErr = "非法的文件扩展名"; break;
case 5:
$strErr = "文件大小超过限制"; break;
}
echo "错误", $this->error, ",$strErr.";
}
function getFileSize($formFile) {
return $_FILES[$formFile]['size'];
}
function getAllFileSize($formFile) {
$i = 0;
foreach ($_FILES as $file) {
$AllFileSize[$i] = $file['size'];
$i++;
}
return $AllFileSize;
}
function getFileName($formFile) {
return $_FILES[$formFile]['name'];
}
function getFileNames() {
$i = 0;
foreach ($_FILES as $file) {
$fileNames[$i] = $file['name'];
$i++;
}
return $fileNames;
}
function my_preg_replace($str){ //替换成正则表达式
if (trim($str)) {
$str = preg_replace("/\b/", "\b", $str);
$str = "/" . $str . "/i";
}
return $str;
}
function delFile($filename) {
unlink($filename);
}
function moveFile($filename) {
$filecopy = str_replace("data", "data_expired", $filename);
$dirs = substr($filecopy, 0, strrpos($filecopy, "/") + 1);
$this->mkDir($dirs);
if (copy($filename, $filecopy)) {
unlink($filename);
return true;
}
return false;
}
function moveFiles($filepaths) {
$arrayFile = split(",", $filepaths);
foreach ($arrayFile as $file) {
$this->moveFile($file);
}
}
function delDir($dir) {
$directory = dir($dir);
while($entry = $directory->read()) {
if ($entry!="." && $entry!="..") {
$entry = $dir.'/'.$entry;
if(is_file($entry)) {
unlink($entry);
}
else if (is_dir($entry)) {
$this->delDir($entry);
}
}
}
$directory->close();
rmdir($dir);
}
function mkDir($dirs) {
$len = strlen($dirs);
for ($i = 0; $i < $len; $i = $j + 1) {
$j = strpos($dirs, "/", $i);
$dir = substr($dirs, 0, $j);
if ($dir)
@mkdir($dir);
}
}
}
?>
[此贴子已经被作者于2007-2-25 23:09:45编辑过]