【原】flash图片批量上传处理专用php类。
生活随笔
收集整理的這篇文章主要介紹了
【原】flash图片批量上传处理专用php类。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2010年06月26日 星期六 20:00
搬離寫了5年的渣度空間,這些都是文章備份。
?
昨天晚上寫了個flash上傳圖片處理類。
1.使用gd庫檢測接收文件的格式,而非普通的后綴名或MIME格式檢查。排除jpeg木馬。2.可設定圖片存儲的高寬限制。超過限制將強制按原寬高比例縮小到限制大小。
3.啟用了相關身份認證。
4.接收圖片旋轉參數,可旋轉原圖片。
5.日志記錄功能。保存圖片錯誤或成功時都有日志記錄。
6.詳細的utf-8編碼的xml格式相關處理反饋。便于flash操作。
調用:
? <?php include_once("YImgUpload.class.php"); $upObject=new YImgUpload(); //如設置$upObject->needIdentity=false;將不進行身份認證,便于在flash中調試 $upObject->needIdentity=false; $upObject->files=$_FILES; //設置file數組。(只能單張) $upObject->session=$_POST["session"]; //設置session_id() //下面是可選參數。 $upObject->params["rotation"]=$_POST["rotation"]; $upObject->params["cookie"]=$_POST["cookie"]; $upObject->params["allowComment"]=$_POST["allowComment"]; $upObject->params["allowReprint"]=$_POST["allowReprint"]; //請記住先檢測,再上傳 $upObject->check(); $upObject->upload(); ?>
文件處理類。名稱:YImgUpload.class.php
? <?php/*----------------------------------------------------------------- * 圖片上傳處理專用封裝類。適合配合flash上傳使用。*@author: yukon12345 http://blog.csdn.net/yukon12345 *默認與flash上傳控YUploadImg配合能接受post參數: *session,(傳輸而來的session_id()。因為flash上傳本身并不能在http頭附加session_id來進行認證。) *rotation,(圖片旋轉角度) *cookie(用js獲取到的當前瀏覽器cookie) *allowComment,allowReprint(存儲是否允許評論,允許轉帖。現并無特殊用途僅用于日志記錄,便于開發時調試) *默認的$_FILES["Filedata"]flash上傳 *----------------------------------------------------------------------------------------- */ class YImgUpload {public $needIdentity = true; //是否進行身份認證 public $session = "noSession"; //需賦值的session_id。 public $files; //需賦值的$files.為$_FILES public $params; //次要的附加參數 public $upload_name = "Filedata"; //接收上傳來的標示名。flash默認"Filedata" public $max_file_size_in_bytes = 2097152; //上傳大小限制默認2M public $max_size = 2000; //圖片最大寬高注意允許的寬高過大會造成服務器資源消耗。并造成執行30秒超時。 public $MAX_FILENAME_LENGTH = 260; //最長文件名 public $extension_whitelist = array ( "jpg", "gif", "png", "jpeg" ); // 允許的后綴 public $upload_contain = "/uploads/"; //上傳圖片所在文件夾 public $log_path = "logs"; //日志記錄的目錄。請確認是否有此文件夾 private $save_path; //保存的路徑 private $file_name; //原文件名 private $file_extension = ""; private $pic_titler; private $uploadErrors = array ( 0 => "", 1 => "超過最大字節", 2 => "超過最大字節", 3 => "文件不完整", 4 => "無上傳文件aaa", 6 => "找不到暫存路徑" ); private $width; private $height; public function YImgUpload() { } public function check() {$this->file_name = $this->files[$this->upload_name]['name']; $this->save_path = dirname(__FILENAME__) . $this->upload_contain; if ($this->needIdentity == false) {} else { session_id($this->session); session_start(); //下面這一部分可修改成你自己的session檢測函數 //注釋掉這一段將不會驗證發送者的身份 if ($_SESSION["check"] != "yukonTestCode") {$this->HandleError("身份驗證錯誤"); exit (0); } } //參數設置 date_default_timezone_set('PRC'); //設定時區//檢測gd庫 if (function_exists("gd_info")) { //檢測是否開啟gd庫 $gdinfo = gd_info(); $gdversion = $gdinfo['GD Version']; //獲得版本號 preg_match("/([0-9])\.([0-9]+)\.([0-9]+)/", $gdversion, $ver); //獲得特定版本號 if ($ver[1] < 2 && $ver[3] < 28) //檢查庫支持。過低版本不能處理gif { $this->HandleError("gd低于2.0.28"); exit (0); } } else { $this->HandleError("沒有開啟gd庫"); exit (0); }//檢測文件大小和POST數據長度 $POST_MAX_SIZE = ini_get('post_max_size'); //獲取php中上傳限制參數 $unit = strtoupper(substr($POST_MAX_SIZE, -1)); //獲取K,M,G //獲取大小倍數 $multiplier = ($unit == 'M' ? 1048576 : ($unit == 'K' ? 1024 : ($unit == 'G' ? 1073741824 : 1))); //設定可接收的最大上傳字節數。php.ini中限制和你設定的限制中最小的一個。 $max_file_size_in_bytes = $max_file_size_in_bytes > $multiplier * (int) $POST_MAX_SIZE && $POST_MAX_SIZE ? $multiplier * (int) $POST_MAX_SIZE && $POST_MAX_SIZE : $max_file_size_in_bytes;if ((int) $_SERVER['CONTENT_LENGTH'] > $multiplier * (int) $POST_MAX_SIZE && $POST_MAX_SIZE) { //header("HTTP/1.1 500 Internal Server Error"); // 觸發一個錯誤 $this->HandleError("文件大小錯誤"); exit (0); }// 初步檢查文件 if (!isset ($this->files[$this->upload_name])) { $this->HandleError("無上傳文件");exit (0); } else if (isset ($this->files[$this->upload_name]["error"]) && $this->files[$this->upload_name]["error"] != 0) { //發生錯誤時 $this->HandleError($this->uploadErrors[$this->files[$this->upload_name]["error"]]); exit (0); } else if (!isset ($this->files[$this->upload_name]["tmp_name"]) || !@ is_uploaded_file($this->files[$this->upload_name]["tmp_name"])) { $this->HandleError("合法性驗證失敗"); exit (0); } else if (!isset ($this->files[$this->upload_name]['name'])) { $this->HandleError("無文件名"); exit (0); }// 檢查php.ini中上傳文件大小允許值。 $file_size = @ filesize($this->files[$this->upload_name]["tmp_name"]); if (!$file_size || $file_size > $this->max_file_size_in_bytes) { $this->HandleError("文件過大"); exit (0); }if ($file_size <= 0) { $this->HandleError("0字節文件"); exit (0); }// 檢查文件后綴 $path_info = pathinfo($this->files[$this->upload_name]['name']); $this->file_extension = strtolower($path_info["extension"]); $is_valid_extension = false; $is_valid_extension = in_array($this->file_extension, $this->extension_whitelist); //檢查后綴是否在數組中if (!$is_valid_extension) { $this->HandleError("文件后綴錯誤"); exit (0); }//如果沒有設定旋轉就為0度 if (!isset ($params["rotation"])) { $params["rotation"] = 0; }$stamp = time(); //獲取時間戳 $pic_title = htmlspecialchars($this->file_name, ENT_QUOTES); //此為可存入數據庫的圖片標題。需注意防止各種xss攻擊和SQL注入。此處僅做特殊字符<>""&編碼處理。 $pic_titler = $this->file_name; //此為返回給flash的文件名,用于上傳完畢的修改提示 //文件實際在服務器的名字.結合原文件名+暫存名+上傳時間的MD5值。 $this->file_name = md5($this->file_name . $stamp . $this->files[$this->upload_name]["tmp_name"]) . "." . $this->file_extension; //檢查同名文件 if (file_exists($this->save_path . $this->file_name)) { $this->HandleError("已有同名文件"); exit (0); }}/*----------------------- * 檢測上傳文件的圖片編碼,并保存。 * --------------------------- * */public function upload() {//用gd庫檢查圖片的實際編碼,并執行相關操作。 if ($isjpeg = @ imagecreatefromjpeg($this->files[$this->upload_name]["tmp_name"])) { //如果是jpeg 獲取原圖片大小 $this->width = imagesx($isjpeg); $this->height = imagesy($isjpeg); if ($this->width > $this->max_size || $this->height > $this->max_size || $this->params["rotation"] != 0) { //圖片大小超過或者需要旋轉,啟用gd庫特別操作 $new_img = $this->resize_image($isjpeg, $this->params["rotation"]);if (@ imagejpeg($new_img, $this->save_path . $this->file_name)) { $this->HandleOk("文件保存成功", $this->save_path . $this->file_name, $this->pic_titler); } else { $this->HandleError("無法保存"); exit (0); } }//gd庫特別操作end else{//圖片不超過大小寬高并且無旋轉,直接保存 if (@ move_uploaded_file($this->files[$this->upload_name]["tmp_name"], $this->save_path . $this->file_name)) { $this->HandleOk("文件保存成功", $this->save_path . $this->file_name, $this->pic_titler); } else { $this->HandleError("無法保存"); exit (0); } }//直接保存end } //jpeg完 else if ($ispng = @ imagecreatefrompng($this->files[$this->upload_name]["tmp_name"])) { //如果是png //變成合適大小并旋轉 獲取原圖片大小 $this->width = imagesx($ispng); $this->height = imagesy($ispng); if ($this->width > $this->max_size || $this->height > $this->max_size || $this->params["rotation"] != 0) { //圖片大小超過或者需要旋轉,啟用gd庫特別操作 $new_img = $this->resize_image($ispng, $this->params["rotation"]);if (@ imagepng($new_img, $this->save_path . $this->file_name)) { $this->HandleOk("文件保存成功", $this->save_path . $this->file_name, $this->pic_titler); } else { $this->HandleError("無法保存"); exit (0); } }//gd庫特別操作end else{//圖片不超過大小寬高并且無旋轉,直接保存 if (@ move_uploaded_file($this->files[$this->upload_name]["tmp_name"], $this->save_path . $this->file_name)) { $this->HandleOk("文件保存成功", $this->save_path . $this->file_name, $this->pic_titler); } else { $this->HandleError("無法保存"); exit (0); } }//直接保存end } //png完 else if ($isgif = @ imagecreatefromgif($this->files[$this->upload_name]["tmp_name"])) { //如果是gif,由于使用imagegif將只保留一幀圖片,因此使用普通文件保存方法。if (@ move_uploaded_file($this->files[$this->upload_name]["tmp_name"], $this->save_path . $this->file_name)) { $this->HandleOk("文件保存成功", $this->save_path . $this->file_name, $this->pic_titler); } else { $this->HandleError("無法保存"); exit (0); }} //gif完 else { $this->HandleError("圖過大或編碼錯"); exit (0); }exit (0); } /*------------------------------------------------ * 調整圖片大小。寬高大于設定的變小,并旋轉圖片 * resize_image("文件資源句柄","旋轉度") * * @param string $source接收待處理的文件資源句柄 * @param int $rotation旋轉多少度 * @return handler $new_img返回處理后的文件資源句柄 * ----------------------------------------- * */ private function resize_image($source, $rotation) { //獲取原圖片大小 $max = $this->max_size; if ($this->width < $max && $this->height < $max) {//寬高都不超過就只做旋轉修改 if ($rotation != 0) { //角度為0直接返回 $new_img = imagerotate($source, $rotation, 0); } else { $new_img = $source; } return $new_img; } else { //有超過的邊 if ($this->width > $max && $this->height > $max) { //都超過時 if ($this->width >= $this->height) { //寬大于高 $new_width = $max; //寬為max $new_height = $max / ($this->width / $this->height); //保持寬高比 } else { $new_height = $max; $new_width = $max / ($this->height / $this->width); } } else if ($this->width > $max) { //只有寬超過 $new_width = $max; $new_height = $this->height / ($this->width / $max); } else if ($this->height > $max) { //只有高超過 $new_height = $max; $new_width = $this->width / ($this->height / $max); } $dest = imagecreatetruecolor($new_width, $new_height); //調整圖片大小。將原圖片填充到目的圖片上 imagecopyresized($dest, $source, 0, 0, 0, 0, $new_width, $new_height, $this->width, $this->height); if ($rotation != 0) { $new_img = imagerotate($dest, $rotation, 0); } else { $new_img = $dest; } return $new_img; } }/*--------------------------------- * 上傳失敗后的操作。用于輸出xml,并記錄相關參數 * 接收參數: * @param string $message輸出給flash的文字提示 * ---------------------------------- * */private function HandleError($message) { //發生錯誤時的日志記錄。 $xml = "<?xml version='1.0' encoding='utf-8'?>"; $xml .= "<root><information errorId='1' message='" . $message . "'/></root>"; echo $xml; //下面為日志記錄 $log = date("[H:i:s]"); $log .= "【錯誤消息:】" . $message; $log .= "【IP:】" . $this->getIP(); $log .= "【用戶名:】" . $this->getUser(); $log .= "【Session:】" . $this->session; $log .= "【文件名:】" . $this->files[$this->upload_name]['name']; $log .= "【圖片字節數:】" . $this->files[$this->upload_name]['size']; //$log .= "【Cookie:】" . $this->params['cookie'] ; $log .= "【AllowComment:】" . $this->params['allowComment']; $log .= "【AllowReprint:】" . $this->params['allowReprint']; $log .= "【Rotaion:】" . $this->params['rotation'] . "\r\n"; //日志默認目錄:logs 請確定是否有這個目錄 @ error_log($log, 3, "./" . $this->log_path . "/errorlog" . date("[Y-m-d]") . ".log");}/*--------------------------------- * 上傳完畢后的操作。用于輸出xml,并記錄上傳參數 * 接收參數: * @param string $message輸出給flash的文字提示 * @param string $imgURL圖片在服務器的相對地址。 * @param string $title圖片原名稱。 * ---------------------------------- * */private function HandleOk($message, $imgURL, $title) { //上傳成功時的記錄 $xml = "<?xml version='1.0' encoding='utf-8'?>"; $xml .= "<root><information errorId='0' message='" . $message . "'url='" . $imgURL . "' title='" . $this->files[$this->upload_name]['name'] . "' /></root>"; echo $xml; //下面為日志記錄 $log = date("[H:i:s]"); $log .= "【IP:】" . $this->getIP(); $log .= "【用戶名:】" . $this->getUser(); $log .= "【Session:】" . $this->session; //$log .= "【Cookie:】" . $this->params['cookie'] ; $log .= "【圖片字節數:】" . $this->files[$this->upload_name]['size']; $log .= "【原圖片寬高:】" . $this->width . "*" . $this->height; $log .= "【AllowComment:】" . $this->params['allowComment']; $log .= "【AllowReprint:】" . $this->params['allowReprint']; $log .= "【Rotaion:】" . $pthis->arams['rotation'] . "\r\n"; error_log($log, 3, "./" . $this->log_path . "/oklog" . date("[Y-m-d]") . ".log"); } /*---------------------------------- * 返回訪問者的IP * @return IP * --------------------------- * */ private function getIP() { //獲取上傳者IP if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) { $ip = getenv("HTTP_CLIENT_IP"); } else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) { $ip = getenv("HTTP_X_FORWARDED_FOR"); } else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) { $ip = getenv("REMOTE_ADDR"); } else if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) { $ip = $_SERVER['REMOTE_ADDR']; } else { $ip = "unknown"; } return ($ip); }private function getUser() { //這里添加獲取上傳者的網站登陸ID }} //class ?>
總結
以上是生活随笔為你收集整理的【原】flash图片批量上传处理专用php类。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 恶心的中国电信
- 下一篇: 欧盟能源相关产品(ErP)指令