php简单文件上传类
2016-05-09 23:14
726 查看
<?php
header("Content-Type:text/html; charset=utf-8");
if($_POST['submit']){
$upfiles = new Upload();
$upfiles->upload_file();
}
class Upload{
/*作者:mckee 来自:www.phpddt.com*/
public $upload_name; //上传文件名
public $upload_tmp_name; //上传临时文件名
public $upload_final_name; //上传文件的最终文件名
public $upload_target_dir; //文件被上传到的目标目录
public $upload_target_path; //文件被上传到的最终路径
public $upload_filetype ; //上传文件类型
public $allow_uploadedfile_type; //允许的上传文件类型
public $upload_file_size; //上传文件的大小
public $allow_uploaded_maxsize=10000000; //允许上传文件的最大值
//构造函数
public function __construct()
{
$this->upload_name = $_FILES["file"]["name"]; //取得上传文件名
$this->upload_filetype = $_FILES["file"]["type"];
$this->upload_tmp_name = $_FILES["file"]["tmp_name"];
$this->allow_uploadedfile_type = array('jpeg','jpg','png','gif','bmp','doc','zip','rar','txt','wps');
$this->upload_file_size = $_FILES["file"]["size"];
$this->upload_target_dir="./upload";
}
//文件上传
public function upload_file()
{
$upload_filetype = $this->getFileExt($this->upload_name);
if(in_array($upload_filetype,$this->allow_uploadedfile_type))
{
if($this->upload_file_size < $this->allow_uploaded_maxsize)
{
if(!is_dir($this->upload_target_dir))
{
mkdir($this->upload_target_dir);
chmod($this->upload_target_dir,0777);
}
$this->upload_final_name = date("YmdHis").rand(0,100).'.'.$upload_filetype;
$this->upload_target_path = $this->upload_target_dir."/".$this->upload_final_name;
if(!move_uploaded_file($this->upload_tmp_name,$this->upload_target_path))
echo "<font color=red>文件上传失败!</font>";
}
else
{
echo("<font color=red>文件太大,上传失败!</font>");
}
}
else
{
echo("不支持此文件类型,请重新选择");
}
}
/**
*获取文件扩展名
*@param String $filename 要获取文件名的文件
*/
public function getFileExt($filename){
$info = pathinfo($filename);
return $info["extension"];
}
}
?>
<form enctype="multipart/form-data" method="POST" action="">
<input type="file" name="file"><input type="submit" name="submit" value="上传">
</form>
header("Content-Type:text/html; charset=utf-8");
if($_POST['submit']){
$upfiles = new Upload();
$upfiles->upload_file();
}
class Upload{
/*作者:mckee 来自:www.phpddt.com*/
public $upload_name; //上传文件名
public $upload_tmp_name; //上传临时文件名
public $upload_final_name; //上传文件的最终文件名
public $upload_target_dir; //文件被上传到的目标目录
public $upload_target_path; //文件被上传到的最终路径
public $upload_filetype ; //上传文件类型
public $allow_uploadedfile_type; //允许的上传文件类型
public $upload_file_size; //上传文件的大小
public $allow_uploaded_maxsize=10000000; //允许上传文件的最大值
//构造函数
public function __construct()
{
$this->upload_name = $_FILES["file"]["name"]; //取得上传文件名
$this->upload_filetype = $_FILES["file"]["type"];
$this->upload_tmp_name = $_FILES["file"]["tmp_name"];
$this->allow_uploadedfile_type = array('jpeg','jpg','png','gif','bmp','doc','zip','rar','txt','wps');
$this->upload_file_size = $_FILES["file"]["size"];
$this->upload_target_dir="./upload";
}
//文件上传
public function upload_file()
{
$upload_filetype = $this->getFileExt($this->upload_name);
if(in_array($upload_filetype,$this->allow_uploadedfile_type))
{
if($this->upload_file_size < $this->allow_uploaded_maxsize)
{
if(!is_dir($this->upload_target_dir))
{
mkdir($this->upload_target_dir);
chmod($this->upload_target_dir,0777);
}
$this->upload_final_name = date("YmdHis").rand(0,100).'.'.$upload_filetype;
$this->upload_target_path = $this->upload_target_dir."/".$this->upload_final_name;
if(!move_uploaded_file($this->upload_tmp_name,$this->upload_target_path))
echo "<font color=red>文件上传失败!</font>";
}
else
{
echo("<font color=red>文件太大,上传失败!</font>");
}
}
else
{
echo("不支持此文件类型,请重新选择");
}
}
/**
*获取文件扩展名
*@param String $filename 要获取文件名的文件
*/
public function getFileExt($filename){
$info = pathinfo($filename);
return $info["extension"];
}
}
?>
<form enctype="multipart/form-data" method="POST" action="">
<input type="file" name="file"><input type="submit" name="submit" value="上传">
</form>
相关文章推荐
- LoadRunner性能测试指标 TPS(Transaction per Second)总结
- Gzip压缩和解压缩 (web优化GZIPOutputStream)
- [乐意黎原创] PHP的增加时间
- PHP 练习:租房子
- PHP 多条件查询(汽车表)
- PHP调试技术
- PHP 封装类来访问数据库
- cPanel下如何修改匿名FTP账号权限
- cPanel下FTP对话管理
- windows下PHP环境的搭建
- OSX 编译安装一个高版本的php
- php global用法
- Discuz的forum_index.php文件的分析
- php 数组
- wp-settings.php
- 【discuzx2】forum_index.php文件的分析
- contentProvider访问系统的通讯录
- php页面跳转的几种方式
- php 字符串
- php 实现接收客户端上传的图片