说明:
PHP文件上传类,可设置允许上传的文件扩展名、是否自动创建下级目录。
用法:
<?php
include('upfile.class.php');
if (isset($_GET['up'])){
$up = new upile("upload");
$result = $up->upload('upfile'); // HTML中<input />的name属性值
echo '结果:' . $result; // 如果成功返回地址,否则返回FALSE,错误信息保存在“$up->error”。
if (!$up->error){
echo '成功';
}else{
echo '失败,错误信息:' . $up->error;
}
}
?>
<form method="post" action="?up" enctype="multipart/form-data">
<input type="file" name="upfile"><br><br>
<input type="submit" value="上传">
</form>
下载:
upfile.class.php.tar.gz (为什么此附件的地址不能分享给朋友们下载?)
代码:
<?php
// 说明:上传类
// 日期:14:40 2010/5/26 周三
class upfile{
public $denyType = 'asp,php,aspx,jsp,html,js,vbs'; // 拒绝上传的扩展名(不是文件类型!)
public $uploadFolder = ''; // 存放目录(末尾不要加“/”)
public $error = ''; // 错误信息
public $autoSubfolder = TRUE; // 是否自动创建子目录
public $SubfolderFormat = 'Y-m'; // 子目录格式(使用DATE函数的格式)
public $filePath = ''; // 上传成功后的URL
// 说明:初始化
function __construct($uploadFolder = 'upload'){
$this->uploadFolder = $uploadFolder;
}
// 说明:上传,参数是<input />的name属性值;成功返回相对URL,失败返回FALSE和错误信息(在$this->error里)
// bool/sting upload(string $html_tags_input_attrib_name);
function upload($inputName){ // 上传操作,参数是input标签的name属性。
$this->_mkdir(); // 创建存放目录
if ($this->error){ // 如果有错,直接返回(例如_mkdir)
return FALSE;
}
if(!$_FILES[$inputName]["name"]){
$this->error = '没有上传文件';
return FALSE;
}
if($_FILES[$inputName]["name"]){
$isUpFile = $_FILES[$inputName]['tmp_name'];
if (is_uploaded_file($isUpFile)){
$extName = $this->_checkType($_FILES[$inputName]["name"]);
if (FALSE == $extName){
return FALSE;
}
$microSenond = floor(microtime()*10000);// 取一个毫秒级数字,4位。
$newFileName = $uploadFolder . '/' . date('YmdHis') . $microSenond . '.' . $extName ; // 所上传文件的新名字。
$location = $this->uploadFolder . $newFileName;
$result = @move_uploaded_file($isUpFile, $location);
if ($result){
$this->filePath = $location;
return $location;
}else{
$this->error = '移动临时文件时出错';
return FALSE;
}
}else{
$uploadError = $_FILES[$inputName]['error'];
if (1 == $uploadError){ // 文件大小超过了php.ini中的upload_max_filesize
$this->error = '文件太大,服务器拒绝接收大于' . ini_get('upload_max_filesize') . '的文件';
return FALSE;
}elseif (3 == $uploadError){ // 上传了部分文件
$this->error = '上传中断,请重试';
return FALSE;
}elseif (4 == $uploadError){
$this->error = '没有文件被上传';
return FALSE;
}elseif (6 == $uploadError){
$this->error = '找不到临时文件夹,请联系您的服务器管理员';
return FALSE;
}elseif (7 == $uploadError){
$this->error = '文件写入失败,请联系您的服务器管理员';
return FALSE;
}else{
if (0 != $uploadError){
$this->error = '未知上传错误,请联系您的服务器管理员';
return FALSE;
}
}
}
}
}
// 检测扩展名是否允许(因未知扩展名太多,故不严格检测(只据扩展名,不据文件头))
private function _checkType($file){
$extName = preg_replace('/.*\.(.*[^\.].*)*/iU','\\1', $file);
if (empty($extName)){
$extName = 'unKnown';
}
$denyTypes = strtolower($this->denyType); // 先转成小写
$denyTypes = explode(',', $this->denyType); // 再分解数组
foreach ($denyTypes as $ext){
$ext = strtolower($ext);
if ($extName == $ext){
$this->error = '不允许上传的文件类型:' . $extName;
return FALSE;
}
}
if (!$this->error){
return $extName;
}
}
// 说明:存放目录
private function _mkdir(){ // 创建上传目录
// 基目录是否可写
$baseDir = dirname($this->uploadFolder);
if(!is_writable($baseDir)){
$this->error = '基目录不可写';
return FALSE;
}
// 创建目录
if(!is_dir($this->uploadFolder)){
$mkdirResutlt = @mkdir($this->uploadFolder);
if (!$mkdirResutlt){
$this->error = '存放文件的目录创建失败';
return FALSE;
}
}
// 创建子目录
if($this->autoSubfolder){
$subFolder = date($this->SubfolderFormat);
$newUploadFolder = $this->uploadFolder . '/' . $subFolder;
if(!is_dir($newUploadFolder)){
$mkdirResutlt = @mkdir($newUploadFolder);
if(!$mkdirResutlt){
$this->error = '子目录创建失败';
return FALSE;
}
}
$this->uploadFolder = $newUploadFolder;
}
}
}
?>
相关阅读
<完>