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. 原生JS面向对象思想封装轮播图组件

    原生JS面向对象思想封装轮播图组件 在前端页面开发过程中,页面中的轮播图特效很常见,因此我就想封装一个自己的原生JS的轮播图组件.有了这个需求就开始着手准备了,代码当然是以简洁为目标,轮播图的各个功能 ...

  2. css3学习笔记之多列

    CSS3 创建多列 column-count 属性指定了需要分割的列数. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <!D ...

  3. 前端性能优化工具--DOM Monster

    当我们开发web应用的时候,性能是一个永远不能回避的问题.其实对于DOM的性能调试也是一个不可或缺的过程.使用DOM monster你只需要添加到你的”书签中“,在任何需要调试的页面点击这个书签,它就 ...

  4. Windows内存原理与内存管理

    WIndows为每个进程分配了4GB的虚拟地址空间,让每个进程都认为自己拥有4GB的内存空间,4GB怎么来的? 32位 CPU可以取地址的空间为2的32次方,就是4GB(正如16位CPU有20根寻址线 ...

  5. mysql 报Row size too large 65535 原因与解决方法

    报错信息:Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535 ...

  6. Skyline中使用AxTE3DWindowEx打开新的一个球体

    在winform窗体中拖入AxTE3DWindowEx控件. using system; using system.Collections.Generic; using System.Drawing; ...

  7. 关于Socket编写简单聊天工具的总结(原创)

    这段时间再看socket编程,虽然现在是刚刚接触,但是还是忍不住想写一篇总结,来激励自己努力学习,写的不好的地方,还请大家指教啊! 下面针对一个简单的发送消息和文件的程序说说吧.   首先是服务器需要 ...

  8. Transact-SQL 存储过程(c#调用执行)

    1. Microsoft SQL Server Management Studio 中创建 存储过程 1.1 借助模板资源管理器中的Stored Procedure模板进行修改创建 1.2 直接新建查 ...

  9. 在linux下配置Nginx+Java+PHP的环境

    Apache对Java的支持很灵活,它们的结合度也很高,例如Apache+Tomcat和Apache+resin等都可以实现对Java应用 的支持.Apache一般采用一个内置模块来和Java应用服务 ...

  10. ISO-9126 软件质量模型

    摘要 在软件开发过程中,软件的质量是一个重要的因素,而软件体系结构在整个过程中显得尤为重要.软件的质量需求是在开发初期的非功能性需求,对软件的体系结构影响很大.但是并不意味着一味的追求质量,必须在效率 ...