php檔案上傳(強大檔案圖片上傳類)

來源:互聯網
上載者:User

/*
 * created on 2010-6-21
 *
 * the class for image to upload
 *
 * made by s71ence
 *
 * @$user_id
 * @$max_file_size
 * @$max_image_side
 * @$destination_folder
 *
 * return:
 * @$_cookie['img_path']
 * @$img_unfind
 * @$img_type
 * @$mkdir_warry
 * @$img_side_too_big
 * @$img_exist
 * @$img_move
 * @$img_upload_sucess
 */
 

 代碼如下 複製代碼

header('content-type:text/html;charset=utf-8');
 
 class image_upload extends fn_function
 {
 private $user_id;
 private $max_file_size; //allow the image's size
 private $max_image_side; //allow the image's side
 private $destination_folder; //image's storage path
 private $img_preview;
 private $img_preview_size;
 private $cookie_set; //the cookie's name

 function __construct($user_id,$max_file_size,$max_image_side,$destination_folder,$img_preview,$img_preview_size,$cookie_set)
 {
  $this->user_id=$user_id;
  $this->max_file_size=$max_file_size;
  $this->max_image_side=$max_image_side;
  $this->destination_folder=$destination_folder;
  $this->img_preview=$img_preview;
  $this->img_preview_size=$img_preview_size;
  $this->cookie_set=$cookie_set;
  $this->get_date();
  $this->get_image_type();
 }

 private function get_date()
 {
  $this->date=fn_function::get_server_date();
  return $this->date;
 }

 function get_image_type()
 {
  $this->up_img_types=array(
            'image/jpg',
            'image/jpeg',
            'image/png',
            'image/pjpeg',
            'image/gif',
            'image/bmp',
            'image/x-png'
        );

  return $this->up_img_types;
 }

 function upload_image()
 {
  if ($_server['request_method'] == 'post')
  {
   //check the iamge is exist
   if (!is_uploaded_file($_files["upfile"]["tmp_name"]))
   {
    return $img_unfind=fn_function::alert_msg('圖片不存在!');
    exit;
      }

      $file = $_files["upfile"];

   //check the iamge's size
      if($this->max_file_size < $file["size"])
      {
       return $img_side_too_big=fn_function::alert_msg('圖片大小超過系統允許最大值!');
       exit;
      }

      //check the iamge's type
      if(!in_array($file["type"], $this->up_img_types))
      {
       return $img_type=fn_function::alert_msg('圖片類型不符!');
       exit;
      }

      if(!file_exists($this->destination_folder))
      {
       if(!fn_function::mkdirs($this->destination_folder))
       {
        return $mkdir_warry=fn_function::alert_msg('目錄建立失敗!');
        exit;
       }
      }

      $file_name=$file["tmp_name"];
      $image_size = getimagesize($file_name);
      $pinfo=pathinfo($file["name"]);
      $file_type=$pinfo['extension'];
      $destination = $this->destination_folder.time().".".$file_type;
   setcookie($this->cookie_set, $destination);

   if($image_size[0]>$this->max_image_side || $image_size[1]>$this->max_image_side)
   {
    return $img_side_too_big=fn_function::alert_msg('圖片解析度超過系統允許最大值!');
    exit;
   }

   $overwrite="";
   if (file_exists($destination) && $overwrite != true)
   {
    return $img_exist=fn_function::alert_msg('同名檔案已經存在了!');
    exit;
      }

      if(!move_uploaded_file ($file_name, $destination))
      {
       return $img_move=fn_function::alert_msg('移動檔案出錯!');
       exit;
      }

   $img_upload_sucess="<font color=red face=微軟雅黑>上傳成功</font><br>";
   if($this->img_preview==1)
   {
    $img_src="<img src="".$destination."" width=".($image_size[0]*$this->img_preview_size)." height=".($image_size[1]*$this->img_preview_size);
    $img_upload_sucess.=$img_src;
   }
      return $img_upload_sucess;
  }
 }

 function __destruct()
 {
  //clear
 }
 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.