| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6360 人关注过本帖
标题:[原创]Php文件上传类,多多指教
取消只看楼主 加入收藏
rainic
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2367
专家分:0
注 册:2005-8-9
收藏
 问题点数:0 回复次数:3 
[原创]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
rainic
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2367
专家分:0
注 册:2005-8-9
收藏
得分:0 
嗯嗯...

2007-04-14 18:22
rainic
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2367
专家分:0
注 册:2005-8-9
收藏
得分:0 
我回来了

2007-12-21 01:37
快速回复:[原创]Php文件上传类,多多指教
数据加载中...
 
   



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

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