1. <?php
  2. header("Content-Type:text/html; charset=utf-8");
  3. if($_POST['submit']){
  4. $upfiles = new Upload();
  5. $upfiles->upload_file();
  6. }
  7. class Upload{
  8. /*作者:mckee 来自:www.phpddt.com*/
  9. public $upload_name; //上传文件名
  10. public $upload_tmp_name; //上传临时文件名
  11. public $upload_final_name; //上传文件的最终文件名
  12. public $upload_target_dir; //文件被上传到的目标目录
  13. public $upload_target_path; //文件被上传到的最终路径
  14. public $upload_filetype ; //上传文件类型
  15. public $allow_uploadedfile_type; //允许的上传文件类型
  16. public $upload_file_size; //上传文件的大小
  17. public $allow_uploaded_maxsize=10000000; //允许上传文件的最大值
  18. //构造函数
  19. public function __construct()
  20. {
  21. $this->upload_name = $_FILES["file"]["name"]; //取得上传文件名
  22. $this->upload_filetype = $_FILES["file"]["type"];
  23. $this->upload_tmp_name = $_FILES["file"]["tmp_name"];
  24. $this->allow_uploadedfile_type = array('jpeg','jpg','png','gif','bmp','doc','zip','rar','txt','wps');
  25. $this->upload_file_size = $_FILES["file"]["size"];
  26. $this->upload_target_dir="./upload";
  27. }
  28. //文件上传
  29. public function upload_file()
  30. {
  31. $upload_filetype = $this->getFileExt($this->upload_name);
  32. if(in_array($upload_filetype,$this->allow_uploadedfile_type))
  33. {
  34. if($this->upload_file_size < $this->allow_uploaded_maxsize)
  35. {
  36. if(!is_dir($this->upload_target_dir))
  37. {
  38. mkdir($this->upload_target_dir);
  39. chmod($this->upload_target_dir,0777);
  40. }
  41. $this->upload_final_name = date("YmdHis").rand(0,100).'.'.$upload_filetype;
  42. $this->upload_target_path = $this->upload_target_dir."/".$this->upload_final_name;
  43. if(!move_uploaded_file($this->upload_tmp_name,$this->upload_target_path))
  44. echo "<font color=red>文件上传失败!</font>";
  45. }
  46. else
  47. {
  48. echo("<font color=red>文件太大,上传失败!</font>");
  49. }
  50. }
  51. else
  52. {
  53. echo("不支持此文件类型,请重新选择");
  54. }
  55. }
  56. /**
  57. *获取文件扩展名
  58. *@param String $filename 要获取文件名的文件
  59. */
  60. public function getFileExt($filename){
  61. $info = pathinfo($filename);
  62. return $info["extension"];
  63. }
  64. }
  65. ?>
  66. <form enctype="multipart/form-data" method="POST" action="">
  67. <input type="file" name="file"><input type="submit" name="submit" value="上传">
  68. </form>

php简单文件上传类的更多相关文章

  1. ASP.NET 文件上传类 简单好用

    调用: UploadFile uf = new UploadFile(); /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认: ...

  2. PHP 文件上传类

    FileUpload.;                $];                $_newname = date(,). :                             To ...

  3. php 文件上传类 实例分享

    最近在研究php上传的内容,找到一个不错的php上传类,分享下. <?php /** * 文件上传类 * class: uploadFile * edit: www.jbxue.com */ c ...

  4. [上传下载] C#FileUp文件上传类 (转载)

    点击下载 FileUp.zip 主要功能如下 .把上传的文件转换为字节数组 .流转化为字节数组 .上传文件根据FileUpload控件上传 .把Byte流上传到指定目录并保存为文件 看下面代码吧 // ...

  5. ThinkPHP文件上传类

    TP框架自带文件上传类使用: 类文件在ThinkPHP/Library/Think/默认在目录下 public function upload(){ $upload = new \Think\Uplo ...

  6. C#文件上传类,文件流,字节数组等

    using System;using System.IO;using System.Web;using System.Web.UI.WebControls; namespace DotNet.Util ...

  7. Php文件上传类class.upload.php

    简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮助你快速的给自己的网站集成上传文件功能.不仅如此,此分类还有一些列的处理功能,可以对上传的文件或者本地的文件进行处 ...

  8. 自定义MVC框架之工具类-文件上传类

    截止目前已经改造了3个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 该文件上传类功能如下: 1,允许定制上传的文件类型,文件mime信息,文 ...

  9. php 文件上传类,功能相当齐全,留作开发中备用吧。

    收藏一个经典好用的php 文件上传类,功能相当齐全,留作开发中备用吧. 好东西,大家都喜欢,才是真的好,哈哈!!! <?php  /**   * 文件上传类   */  class upload ...

随机推荐

  1. java新手笔记34 连接数据库

    1.JdbcUtil package com.yfs.javase.jdbc; import java.sql.Connection; import java.sql.DriverManager; i ...

  2. CAD输出的局部平面坐标数据配准转换到WGS84坐标系

             局部平面坐标                                             平移纠正到常用平面坐标系下的坐标            转换后的地理坐标 采用两 ...

  3. 使用ckplayer搭建rtmp视频直播应用

    视频直播才有的是RTMP协议进行视频实时流传输,在这里我们用到的软件都是 adobe 公司的一个是:Flash Media Server4 另一个是flash media live encoder 这 ...

  4. 巧用Systemtap注入延迟模拟IO设备抖动

    原创文章,转载请注明: 转载自系统技术非业余研究 本文链接地址: 巧用Systemtap注入延迟模拟IO设备抖动 当我们的IO密集型的应用怀疑设备的IO抖动,比如说一段时间的wait时间过长导致性能或 ...

  5. Linux 下添加普通用户,登陆并删除

    adduser 命令.LINUX创建用户的命令useradd -g test -d /home/test1 -s /etc/bash -m test1注解:-g 所属组 -d 家目录 -s 所用的SH ...

  6. 执行插入语句时直接返回插入信息的自增id,判断是否为空

    insert into userinfo(UserName,UserPass,RegTime,email)values('a','b',GETDATE(),'123@qq.com');select @ ...

  7. asp.net 后台 修改 javascript 变量

    Util.JSSetChange(Page, GetType()); public static void JSSetChange(Page page, Type cstype) { // Defin ...

  8. 几种placeholder替换项目参数的方法比较

    引言:(引自:http://openwebx.org/docs/autoconfig.html) 在一个应用中,我们总是会遇到一些参数,例如: 数据库服务器IP地址.端口.用户名: 用来保存上传资料的 ...

  9. phpcms v9二级栏目生成到根目录后三级栏目无法访问的解决办法

    这个栏目问题折腾了我一天多,可怜我这个美工又不会程序!!! 我的问题是: 我的栏目设置如下: 一级栏目[生成到根目录(是)]> 二级栏目[生成到根目录(是)] > 三级栏目[生成到根目录( ...

  10. Spark Streaming揭秘 Day17 资源动态分配

    Spark Streaming揭秘 Day17 资源动态分配 今天,让我们研究一下一个在Spark中非常重要的特性:资源动态分配. 为什么要动态分配?于Spark不断运行,对资源也有不小的消耗,在默认 ...