Php powerful Image Upload class

Source: Internet
Author: User

  1. <? Php
  2. Class upphoto {
  3. Public $ previewsize = 0.125; // preview the image proportion
  4. Public $ preview = 0; // whether to generate a preview. The value is 1 and the value is 0.
  5. Public $ datetime; // Random Number
  6. Public $ ph_name; // upload the image file name
  7. Public $ ph_tmp_name; // temporary Image File Name
  8. Public $ ph_path = "../userimg/"; // Upload File Storage path
  9. Public $ ph_type; // image type
  10. Public $ ph_size; // image size
  11. Public $ imgsize; // the size of the uploaded image, used to determine the display ratio.
  12. Public $ al_ph_type = array (image/jpg, image/jpeg, image/png, image/pjpeg, image/gif, image/bmp, image/x-png ); // allows the upload of image types
  13. Public $ al_ph_size = 1000000; // the file size that can be uploaded
  14. Function _ construct (){
  15. $ This-> set_datatime ();
  16. }
  17. Function set_datatime (){
  18. $ This-> datetime = date ("YmdHis ");
  19. }
  20. // Obtain the file type
  21. Function get_ph_type ($ phtype ){
  22. $ This-> ph_type = $ phtype;
  23. }
  24. // Get the file size
  25. Function get_ph_size ($ phsize ){
  26. $ This-> ph_size = $ phsize. "<br> ";
  27. }
  28. // Get the upload temporary file name
  29. Function get_ph_tmpname ($ tmp_name ){
  30. $ This-> ph_tmp_name = $ tmp_name;
  31. $ This-> imgsize = getimagesize ($ tmp_name );
  32. }
  33. // Obtain the original file name
  34. Function get_ph_name ($ phname ){
  35. $ This-> ph_name = $ this-> ph_path. $ this-> datetime. strrchr ($ phname, "."); // the last position where the strrchr obtains the object's vertex.
  36. // $ This-> ph_name = $ this-> datetime. strrchr ($ phname, "."); // obtain the last position where the point of the file appears.
  37. Return $ this-> ph_name;
  38. }
  39. // Determine the directory for storing uploaded files
  40. Function check_path (){
  41. If (! File_exists ($ this-> ph_path )){
  42. Mkdir ($ this-> ph_path );
  43. }
  44. }
  45. // Determine whether the size of the uploaded file exceeds the permitted size
  46. Function check_size (){
  47. If ($ this-> ph_size> $ this-> al_ph_size ){
  48. $ This-> showerror ("uploading images exceeds 2000KB ");
  49. }
  50. }
  51. // Determine the file type
  52. Function check_type (){
  53. If (! In_array ($ this-> ph_type, $ this-> al_ph_type )){
  54. $ This-> showerror ("Incorrect Image Upload type ");
  55. }
  56. }
  57. // Upload an image
  58. Function up_photo (){
  59. If (! Move_uploaded_file ($ this-> ph_tmp_name, $ this-> ph_name )){
  60. $ This-> showerror ("File Upload error ");
  61. }
  62. }
  63. // Preview the image
  64. Function showphoto (){
  65. If ($ this-> preview = 1 ){
  66. If ($ this-& gt; imgsize [0] & gt; 2000 ){
  67. $ This-> imgsize [0] = $ this-> imgsize [0] * $ this-> previewsize;
  68. $ This-> imgsize [1] = $ this-> imgsize [1] * $ this-> previewsize;
  69. }
  70. Echo (" ph_name} "width =" {$ this-> imgsize [0]} "height =" {$ this-> imgsize [1]} "> ");
  71. }
  72. }
  73. // Error message
  74. Function showerror ($ errorstr ){
  75. Echo "<script language = javascript> alert ($ errorstr); location = javascript: history. go (-1); </script> ";
  76. Exit ();
  77. }
  78. Function save (){
  79. $ This-> check_path ();
  80. $ This-> check_size ();
  81. $ This-> check_type ();
  82. $ This-> up_photo ();
  83. $ This-> showphoto ();
  84. }
  85. }
  86. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.