php文件上传代码

这是一款完整的php文件上传实例代码,支持上传的类型可以创建类时自定义,可支持的上传文件类型,代码如下:

  1. <form name="form1" enctype="multipart/form-data" method="post" action="">
  2. <label for="filefield"></label>
  3. <input type="file" name="filefield" >
  4. <input type="submit" name="button" value="上传文件">
  5. </form>
  6. <?php
  7. /*
  8. * $name; 上传文件名
  9. * $size: 上传文件大小
  10. * $path; 文件原路径
  11. * $newpath: 设置新路径
  12. * $not: 禁止上传的文件类型数组
  13. * $notsize: 限制文件大小的值
  14. * $move: 上传文件源
  15. *
  16. */
  17. class fileupload {
  18. public $name;
  19. public $size;
  20. public $path;
  21. public $newpath;
  22. public $not = array();
  23. public $notsize;
  24. public $move;
  25. public $allfile = array();
  26. function __construct($name,$size,$path,$newpath,$not,$notsize) {
  27. $this ->name = $name;
  28. $this ->size = $size/1048576;
  29. $this ->path = $path;
  30. $this ->newpath = $newpath;
  31. $this ->not = explode(',',$not);
  32. $this ->notsize = $notsize;
  33. $this ->upload();
  34. }
  35. /*
  36. * 上传程序
  37. * 首先判断目录是否存在
  38. * 判断文件类型及大小
  39. */
  40. function upload(){
  41. if(!file_exists($this->newpath)){
  42. echo "<script>alert('该目录不存在!')</script>";
  43. return;
  44. }else{
  45. $arr = explode('.',$this->name);
  46. if(in_array($arr[1],$this->not)){
  47. echo "<script>alert('该类型文件禁止上传!')</script>";
  48. return;
  49. }else if($this->name == ''){
  50. echo "<script>alert('请选择上传的文件!')</script>";
  51. return;
  52. }else if($this->size>$this->notsize){
  53. echo "<script>alert('上传文件超过规定大小!')</script>";
  54. return;
  55. }else if(file_exists("$this->newpath"."$this->name")){
  56. echo "<script>alert('该文件已经存在!')</script>";
  57. return;
  58. }
  59. else{
  60. $this->move = move_uploaded_file($this->path,$this->newpath.$this->name);
  61. $this->move();
  62. }
  63. }
  64. }
  65. /*
  66. * 判断文件上传是否成功
  67. */
  68. function move(){
  69. if($this->move){
  70. echo "<script>alert('文件上传成功!')</script>";
  71. return;
  72. }else{
  73. echo "<script>alert('上传失败!')</script>";
  74. return;
  75. }
  76. }
  77. }
  78. $fu = new fileupload($array[name],$array[size],$array[tmp_name],'./www.phpfensi.com/','exe,rar',5);