php文件图片上传类

这款文件上传实用代码,可以方便的上传你指定的文件或图片,同时也可以快速的限制上传图片文件类或大小,实例代码如下:

  1. /*
  2. * created on 2010-6-21
  3. *
  4. * the class for image to upload
  5. *
  6. * made by s71ence
  7. *
  8. * @$user_id
  9. * @$max_file_size
  10. * @$max_image_side
  11. * @$destination_folder
  12. *
  13. * return:
  14. * @$_cookie['img_path']
  15. * @$img_unfind
  16. * @$img_type
  17. * @$mkdir_warry
  18. * @$img_side_too_big
  19. * @$img_exist
  20. * @$img_move
  21. * @$img_upload_sucess
  22. */
  23. 代码如下 复制代码
  24. header('content-type:text/html;charset=utf-8');
  25. class image_upload extends fn_function
  26. {
  27. private $user_id;
  28. private $max_file_size; //allow the image's size
  29. private $max_image_side; //allow the image's side
  30. private $destination_folder; //image's storage path
  31. private $img_preview;
  32. private $img_preview_size;
  33. private $cookie_set; //the cookie's name
  34. function __construct($user_id,$max_file_size,$max_image_side,$destination_folder,$img_preview,$img_preview_size,$cookie_set)
  35. {
  36. $this->user_id=$user_id;
  37. $this->max_file_size=$max_file_size;
  38. $this->max_image_side=$max_image_side;
  39. $this->destination_folder=$destination_folder;
  40. $this->img_preview=$img_preview;
  41. $this->img_preview_size=$img_preview_size;
  42. $this->cookie_set=$cookie_set;
  43. $this->get_date();
  44. $this->get_image_type();
  45. }
  46. private function get_date()
  47. {
  48. $this->date=fn_function::get_server_date();
  49. return $this->date;
  50. }
  51. function get_image_type()
  52. {
  53. $this->up_img_types=array(
  54. 'image/jpg',
  55. 'image/jpeg',
  56. 'image/png',
  57. 'image/pjpeg',
  58. 'image/gif',
  59. 'image/bmp',
  60. 'image/x-png'
  61. );
  62. return $this->up_img_types;
  63. }
  64. function upload_image()
  65. {
  66. if ($_server['request_method'] == 'post')
  67. {
  68. //check the iamge is exist
  69. if (!is_uploaded_file($_files["upfile"]["tmp_name"]))
  70. {
  71. return $img_unfind=fn_function::alert_msg('图片不存在!');
  72. exit;
  73. }
  74. $file = $_files["upfile"];
  75. //check the iamge's size
  76. if($this->max_file_size < $file["size"])
  77. {
  78. return $img_side_too_big=fn_function::alert_msg('图片大小超过系统允许最大值!');
  79. exit;
  80. }
  81. //check the iamge's type
  82. if(!in_array($file["type"], $this->up_img_types))
  83. {
  84. return $img_type=fn_function::alert_msg('图片类型不符!');
  85. exit;
  86. }
  87. if(!file_exists($this->destination_folder))
  88. {
  89. if(!fn_function::mkdirs($this->destination_folder))
  90. {
  91. return $mkdir_warry=fn_function::alert_msg('目录创建失败!');
  92. exit;
  93. }
  94. }
  95. $file_name=$file["tmp_name"];
  96. $image_size = getimagesize($file_name);
  97. $pinfo=pathinfo($file["name"]);
  98. $file_type=$pinfo['extension'];
  99. $destination = $this->destination_folder.time().".".$file_type;
  100. setcookie($this->cookie_set, $destination);
  101. if($image_size[0]>$this->max_image_side || $image_size[1]>$this->max_image_side)
  102. {
  103. return $img_side_too_big=fn_function::alert_msg('图片分辨率超过系统允许最大值!');
  104. exit;
  105. }
  106. $overwrite="";
  107. if (file_exists($destination) && $overwrite != true)
  108. {
  109. return $img_exist=fn_function::alert_msg('同名文件已经存在了!');
  110. exit;
  111. }
  112. if(!move_uploaded_file ($file_name, $destination))
  113. {
  114. return $img_move=fn_function::alert_msg('移动文件出错!');
  115. exit;
  116. }
  117. $img_upload_sucess="<font color=red ;
  118. if($this->img_preview==1)
  119. {
  120. $img_src="<img src="".$destination."" width=".($image_size[0]*$this->img_preview_size)." height=".($image_size[1]*$this->img_preview_size);//开源代码phpfensi.com
  121. $img_upload_sucess.=$img_src;
  122. }
  123. return $img_upload_sucess;
  124. }
  125. }
  126. function __destruct()
  127. {
  128. //clear
  129. }
  130. }