Php: powerful image upload class _ PHP Tutorial

Source: Internet
Author: User
Php supports uploading images .? Phpclassupphoto {public $ previewsize0.125; ratio of preview images public $ preview0; whether to generate preview; 1; 0 public $ datetime; random number public $ ph_name; Upload

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

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.