当前位置: 首页 > 图文教程 > 网络编程 > PHP > 文件上传类

PHP
PHP 执行系统外部命令 system() exec() passthru()
最新的php 文件上传模型,支持多文件上传
php 静态页面中显示动态内容
数据库查询记录php 多行多列显示
谈PHP生成静态页面分析 模板+缓存+写文件
PHP 各种排序算法实现代码
PHP nl2br函数 将换行字符转成 <br>
php 分页原理详解
Discuz 模板语句分析及知识技巧
php win下Socket方式发邮件类
怎样去阅读一份php源代码
建站常用13种PHP开源CMS比较
php xml留言板 xml存储数据的简单例子
PHP 开源AJAX框架14种
PHP 替换模板变量实现步骤
PHP has encountered an Access Violation at 7C94BD02解决方法
php 正则匹配函数体
php 文件夹删除、php清除缓存程序
php download.php实现代码 跳转到下载文件(response.redirect)
PHP类(Class)入门教程

PHP 中的 文件上传类


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 198 ::
收藏到网摘: n/a


使用示例:
upload.php
<?php
include_once "upload.class.php";
if ($Submit != '')
{
$fileArr['file'] = $file;
$fileArr['name'] = $file_name;
$fileArr['size'] = $file_size;
$fileArr['type'] = $file_type;
/** 所允许上传的文件类型 */
$filetypes = array('gif','jpg','jpge','png');
/** 文件上传目录 */
$savepath = "/usr/htdocs/upload/";
/** 没有最大限制 0 无限制*/
$maxsize = 0;
/** 覆盖 0 不允许 1 允许 */
$overwrite = 0;
$upload = new upload($fileArr, $file_name, $savepath, $filetypes, $overwrite, $maxsize);
if (!$upload->run())
{
echo "上传失败".$upload->errmsg();
}
}
?>
<html>
<head>
<title>文件上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" enctype="multipart/form-data" method="post" action="">
<input type="file" name="file">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
上传类
upload.class.php
<?php
//
// +----------------------------------------------------------------------+
// | 文件上传
|// | 本代码仅供学习讨论之用,允许随意修改
|// | Author: whxbb([email protected])
|// +----------------------------------------------------------------------+
//
// $Id: upload.class.php,v 1.0 2001/10/14 14:06:57 whxbb Exp $
//
$UPLOAD_CLASS_ERROR = array( 1 => '不允许上传该格式文件',
2 => '目录不可写',
3 => '文件已存在',
4 => '不知名错误',
5 => '文件太大'
);
/**
* Purpose
* 文件上传
*
* Example
*
$fileArr['file'] = $file;
$fileArr['name'] = $file_name;
$fileArr['size'] = $file_size;
$fileArr['type'] = $file_type;
// 所允许上传的文件类型
$filetypes = array('gif','jpg','jpge','png');
// 文件上传目录
$savepath = "/usr/htdocs/upload/";
// 没有最大限制 0 无限制
$maxsize = 0;
// 覆盖 0 不允许 1 允许
$overwrite = 0;
$upload = new upload($fileArr, $file_name, $savepath, $filetypes, $overwrite, $maxsize);
if (!$upload->run())
{
echo $upload->errmsg();
}
*
* @author whxbb([email protected])
* @version 0.1
*/
class upload
{
var $file;
var $file_name;
var $file_size;
var $file_type;
/** 保存名 */
var $savename;
/** 保存路径 */
var $savepath;
/** 文件格式限定 */
var $fileformat = array();
/** 覆盖模式 */
var $overwrite = 0;
/** 文件最大字节 */
var $maxsize = 0;
/** 文件扩展名 */
var $ext;
/** 错误代号 */
var $errno;
/**
* 构造函数
* @param $fileArr 文件信息数组 'file' 临时文件所在路径及文件名
'name' 上传文件名
'size' 上传文件大小
'type' 上传文件类型
* @param savename 文件保存名
* @param savepath 文件保存路径
* @param fileformat 文件格式限制数组
* @param overwriet 是否覆盖 1 允许覆盖 0 禁止覆盖
* @param maxsize 文件最大尺寸
*/
function upload($fileArr, $savename, $savepath, $fileformat, $overwrite = 0, $maxsize = 0) {
$this->file = $fileArr['file'];
$this->file_name = $fileArr['name'];
$this->file_size = $fileArr['size'];
$this->file_type = $fileArr['type'];
$this->get_ext();
$this->set_savepath($savepath);
$this->set_fileformat($fileformat);
$this->set_overwrite($overwrite);
$this->set_savename($savename);
$this->set_maxsize($maxsize);
}
/** 上传 */
function run()
{
/** 检查文件格式 */
if (!$this->validate_format())
{
$this->errno = 1;
return false;
}
/** 检查目录是否可写 */
if(!@is_writable($this->savepath))
{
$this->errno = 2;
return false;
}
/** 如果不允许覆盖,检查文件是否已经存在 */
if($this->overwrite == 0 && @file_exists($this->savepath.$this->savename))
{
$this->errno = 3;
return false;
}
/** 如果有大小限制,检查文件是否超过限制 */
if ($this->maxsize != 0 )
{
if ($this->file_size > $this->maxsize)
{
$this->errno = 5;
return false;
}
}
/** 文件上传 */
if(!@copy($this->file, $this->savepath.$this->savename))
{
$this->errno = 4;
return false;
}
/** 删除临时文件 */
$this->destory();
return true;
}
/**
* 文件格式检查
* @access protect
*/
function validate_format()
{
if (!is_array($this->fileformat)) // 没有格式限制
return true;
$ext = strtolower($this->ext);
reset($this->fileformat);
while(list($var, $key) = each($this->fileformat))
{
if (strtolower($key) == $ext)
return true;
}
reset($this->fileformat);
return false;
}
/**
* 获取文件扩展名
* access public
*/
function get_ext()
{
$ext = explode(".", $this->file_name);
$ext = $ext[count($ext) - 1];
$this->ext = $ext;
}
/**
* 设置上传文件的最大字节限制
* @param $maxsize 文件大小(bytes) 0:表示无限制
* @access public
*/
function set_maxsize($maxsize)
{
$this->maxsize = $maxsize;
}
/**
* 设置覆盖模式
* @param 覆盖模式 1:允许覆盖 0:禁止覆盖
* @access public
*/
function set_overwrite($overwrite)
{
$this->overwrite = $overwrite;
}
/**
* 设置允许上传的文件格式
* @param $fileformat 允许上传的文件扩展名数组
* @access public
*/
function set_fileformat($fileformat)
{
$this->fileformat = $fileformat;
}
/**
* 设置保存路径
* @param $savepath 文件保存路径:以 "/" 结尾
* @access public
*/
function set_savepath($savepath)
{
$this->savepath = $savepath;
}
/**
* 设置文件保存名
* @savename 保存名,如果为空,则系统自动生成一个随机的文件名
* @access public
*/
function set_savename($savename)
{
if ($savename == '') // 如果未设置文件名,则生成一个随机文件名
{
srand ((double) microtime() * 1000000);
$rnd = rand(100,999);
$name = date('Ymdhis') + $rnd;
$name = $name.".".$this->ext;
} else {
$name = $savename;
}
$this->savename = $name;
}
/**
* 删除文件
* @param $file 所要删除的文件名
* @access public
*/
function del($file)
{
if(!@unlink($file))
{
$this->errno = 3;
return false;
}
return true;
}
/**
* 删除临时文件
* @access proctect
*/
function destory()
{
$this->del($this->file);
}
/**
* 得到错误信息
* @access public
* @return error msg string or false
*/
function errmsg()
{
global $UPLOAD_CLASS_ERROR;
if ($this->errno == 0)
return false;
else
return $UPLOAD_CLASS_ERROR[$this->errno];
}
}
?>