Home > Backend Development > PHP Tutorial > Write a powerful class for uploading images in PHP_PHP Tutorial

Write a powerful class for uploading images in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 17:40:30
Original
872 people have browsed it

  1. class upphoto{
  2. public $previewsize=0.125 ; //Preview image ratio
  3. public $preview=0 ; //Whether to generate a preview, 1 if yes, 0 if not
  4. public $datetime; //Random number
  5. public $ph_name; //Upload 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; //Upload image size, used to determine display ratio
  11. public $al_ph_type=array(image/jpg,image /jpeg,image/png,image/pjpeg,image/gif,image/bmp,image/x-png); //Allow image type to upload
  12. public $al_ph_size=1000000; //Allow file size to upload
  13. function __construct(){
  14. $this->set_datatime();
  15. }
  16. function set_datatime(){
  17. $this->datetime=date("YmdHis");
  18. }
  19. //Get file type
  20. function get_ph_type($phtype){
  21. $this->ph_type=$phtype;
  22. }
  23. //Get 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. //Get the original text File name
  33. function get_ph_name($phname){
  34. $this->ph_name=$this->ph_path.$this->datetime.strrchr($phname,".") ; //strrchr gets the position where the point in the file last appeared
  35. //$this->ph_name=$this->datetime.strrchr($phname,"."); //strrchr gets the file The position where the point last appeared
  36. return $this->ph_name;
  37. }
  38. // Determine the uploaded file storage directory
  39. function check_path( ){
  40. if(!file_exists($this->ph_path)){
  41. mkdir($this->ph_path);
  42. }
  43. }
  44. //Determine whether the uploaded file exceeds the allowed size
  45. function check_size(){
  46. if($this->ph_size>$this->al_ph_size) {
  47. } $this->showerror("Uploaded image 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("Upload Wrong image type");
  54. }
  55. }
  56. //Upload image
  57. function up_photo(){
  58. if(! move_uploaded_file($this->ph_tmp_name,$this->ph_name)){
  59. $this->showerror("Error uploading file");
  60. }
  61. }
  62. //Picture preview
  63. function showphoto(){
  64. if($this->preview==1){
  65. if( $this->imgsize[0]>2000){
  66.      $this->imgsize[0]=$this->imgsize[0]*$this->previewsize;
  67.                                                                                                                                                                                                                                                                            ="{$this->ph_name}" width="{$this->imgsize[0]}" height="{$this->imgsize[1]}">");
  68. }
  69. }
  70. // Error message
  71. function showerror($errorstr){
  72. echo "";
  73. exit();
  74. }
  75. function save(){
  76. $this ->check_path();
  77. $this->check_size();
  78. $this->check_type();
  79. $this->up_photo( );
  80. $this->showphoto();
  81. }
  82. }
  83. ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486216.htmlTechArticle?php class upphoto{ public $previewsize=0.125; //Preview image ratio public $preview=0; // Whether to generate a preview, 1 if yes, 0 if not public $datetime; //random number public $ph_name; //upload...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template