一款简单PHP图片上传类带图片显示代码

这是一款简单PHP图片上传类带图片显示代码,应该可以说是上传文件最简单的上传类了,可以设置要显示图片高度与宽度,文件大小等.

uploadimg.class.php文件,代码如下:

  1. class upload
  2. {
  3. var $upload_name;
  4. var $upload_tmp_address;
  5. var $upload_server_name;
  6. var $upload_filetype ;
  7. var $file_type;
  8. var $file_server_address;
  9. var $image_w=900; //要显示图片的宽
  10. var $image_h=350; //要显示图片的高
  11. var $upload_file_size;
  12. var $upload_must_size= 50000; //允许上传文件的大小,自己设置
  13. function upload_file()
  14. {
  15. $this->upload_name = $_files["file"]["name"]; //取得上传文件名
  16. $this->upload_filetype = $_files["file"]["type"];
  17. $this->upload_server_name = date("y_m_dh_i_s").$this->upload_name;
  18. $this->upload_tmp_address = $_files["file"]["tmp_name"]; //取得临时地址
  19. $this->file_type = array("image/gif","image/pjpeg"); //允许上传文件的类型
  20. $this->upload_file_size = $_files["file"]["size"]; //上传文件的大小
  21. if(in_array($this->upload_filetype,$this->file_type))
  22. { if($this->upload_file_size < $this->upload_must_size)
  23. {
  24. echo("上传成功,谢谢支持");
  25. $this->file_server_address = "d:www.111cn.netupload/".$this->upload_server_name;
  26. move_uploaded_file($this->upload_tmp_address,$this->file_server_address);//从temp目录移出
  27. echo("<img src=$this->file_server_address width=$this->image_w height=$this->image_h/>"); //显示图片
  28. }
  29. else
  30. {
  31. echo("文件容量太大");
  32. }
  33. }
  34. else
  35. {
  36. echo("不支持此文件类型,请重新选择");
  37. //开源代码phpfensi.com
  38. }
  39. }
  40. }

html上传代码,代码如下:

  1. <form name="upload" enctype="multipart/form-data" method="post" action="upload.php">
  2. <input type="hidden" name="max_file_size " />
  3. <input type="file" name="file" />
  4. <input type="submit" name="submit" value="提交" />
  5. </form>

调用方法,upload.php,代码如下:

  1. inlcude('uploadimg.class.php');
  2. $dd = new upload;
  3. $dd->upload_file();