标题:[原创]Php文件上传类,多多指教
只看楼主
rainic
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2367
专家分:0
注 册:2005-8-9
 问题点数:0 回复次数:14 
[原创]Php文件上传类,多多指教

<?
//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编辑过]

搜索更多相关主题的帖子: Rain mp3 var 服务器 
2007-01-20 14:58
rainic
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2367
专家分:0
注 册:2005-8-9
得分:0 
顶一下,希望对大家有用

2007-03-30 01:04
sunjian
Rank: 1
等 级:新手上路
威 望:1
帖 子:41
专家分:0
注 册:2006-10-7
得分:0 
如果加点注释就好了
2007-04-11 15:15
lmhllr
Rank: 8Rank: 8
等 级:贵宾
威 望:44
帖 子:1504
专家分:42
注 册:2005-5-12
得分:0 

顶一下!

如果能加上public provite static等关键字就更好了

加点注释就好了,可以增加可读性~~

2007-04-12 01:25
islet
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:89
帖 子:6548
专家分:0
注 册:2005-1-28
得分:0 
不错 借来用用
2007-04-14 14:31
边城路人
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:9
帖 子:591
专家分:0
注 册:2007-3-8
得分:0 

搂主知道613寝室吧
我是路人


当你开始怀疑自己时,说明你已经失去了判断力
2007-04-14 17:33
rainic
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2367
专家分:0
注 册:2005-8-9
得分:0 
嗯嗯...

2007-04-14 18:22
边城路人
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:9
帖 子:591
专家分:0
注 册:2007-3-8
得分:0 

看不懂啊


当你开始怀疑自己时,说明你已经失去了判断力
2007-04-25 11:28
發呆的小猪
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:362
专家分:0
注 册:2006-10-10
得分:0 
好东西,收下了,谢谢LZ呀,要是能加点注释就更好了,呵呵

营口小猪 http://bbs.
2007-06-05 13:53
xinxue
Rank: 1
等 级:新手上路
帖 子:128
专家分:0
注 册:2006-2-28
得分:0 
回复 1# 的帖子
不错,好东西.
2007-12-05 21:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-117141-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.050713 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved