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. log4j定义某个类的日志级别

    项目引入了定时任务后,当我把已有的定时任务删除后,控制台一直会打出类似于 [org.springframework.scheduling.quartz.LocalDataSourceJobStore] ...

  2. poj 2431 Expedition

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12980   Accepted: 3705 Descr ...

  3. Base64编码原理与应用

    本文内容转自网络,如需详细内容,请参考相关网址. http://my.oschina.net/goal/blog/201032 代码参考:http://blog.csdn.net/prsniper/a ...

  4. flex&bison 1

    .   {ECHO;}-----单独的flex使用中有效 .   { yyerror();}--------flex和bison交叉使用,即使不调用yyerror函数,也会报错的 error: syn ...

  5. DCL_数据库控制语言

    DCL(Data Control Language)              -------是数据库控制语言.是用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等) ...

  6. ubuntu grub配置

    一.Grub 2包含如下几部分内容:1./boot/grub/grub.cfg 文件2./etc/grub.d/ 文件夹3./etc/default/grub 文件 二.配置和意义: 1.修改grub ...

  7. mysql 运行sql脚本文件

    #只运行,不导出 mysql> source /home/user/to_run.sql; #导出 $ mysql -h host -u user -ppassword database < ...

  8. httpc服务器错误类型大全

    HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败HTTP 401.2 - 未授权:服务器配置问题导致登录失败HTTP 401.3 - ACL 禁止访问资源HTTP 401.4 ...

  9. Object-C - 类的定义

    http://www.cnblogs.com/zhangweia/archive/2011/11/01/2231549.html 1. 文件分为.h:定义接口,及其属性,方法说明. .m :是实现类. ...

  10. Jquery实现图片左右自动滚动

    图片左右滚动的效果想必大家都有见到过吧,其实很简单.在本文将为大家介绍下使用Jquery是如何实现图片左右自动滚动的. 代码如下:<!DOCTYPE HTML>  <html> ...