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. Apache使用mysql认证用户

    使用MySQL进行认证 第1步:下载MySQL认证模块,并更名为mod_auth_mysql.so文件,并保存在apache的modules目录下 第2步:apache要加载此功能模块 LoadMod ...

  2. UVa11384

    题意:给定正整数n,求把1,2,--,n中所有书都变成0的最少操作次数,每次操作可从序列中选择一个或多个整数,同时减去一个相同的正整数. 分析:如1,2,3,4,5,6, 第一步将4,5,6同时减4, ...

  3. SQL SERVER 遇到Unable to find the requested .Net Framework Data Provider. It may not be installed. (System.Data)

    今天新装的SQLSERVER 2012 EXPRESS 用于客户端程序 安装完成后打开登陆SQLSERVER 一切正常 当查看表定义.视图结构时,弹出一下内容 Unable to find the r ...

  4. jQuery学习教程(1)

    一.什么是jQuery JQuery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safar ...

  5. linux与windows共享剪贴板(clipboard)

    linux与windows共享剪贴板(clipboard)的方法 先说两句废话,其实linux和windows之间不需要共享剪贴板,直接在putty中,按住SHIFT+鼠标选择就可以了. 但是作为一种 ...

  6. Oracle非默认监听的处理会遇到的问题以及处理方法

    第一种情况:只是修改默认端口 1.当前监听状态: C:\Windows\system32>lsnrctl status LSNRCTL for 64-bit Windows: Version 1 ...

  7. 四个基数任意次数组合相加得到一个数N,求所有可能组合

    #include <iostream> #include <vector> usingnamespace std; vector<int> vec; constin ...

  8. 使用rar打包多个文件为exe可执行文件

    需求分析:有些机友在刷recovery的时候不知道如何刷入,于是产生写bat脚本和打包为exe可执行文件,只要机友正确安装好驱动后连接手机双击就可以刷入rec了 解决过程: 需要打包的文件 操作过程截 ...

  9. Python+PyQt 数据库基本操作

    Sqlite: 使用Python的sqlite3: 需要注意下commit方式与qt稍有不同 import sqlite3 class DBManager(): def __init__(self): ...

  10. fragment第二次载入就报错

    1.布局中加入一个<fragment 标签,第一次载入的时候是正常的,第二次加载的时候,就直接crashed,退出 2.查到原因Caused by: java.lang.IllegalArgum ...