標籤:style blog io ar color sp on 檔案 div
如果你的項目對上傳不需要很強大的上傳,就可以採用下面的函數,進行傳輸!
public function GrabImage($data) { $php_path = dirname(__FILE__) . ‘/‘; $php_url = dirname($_SERVER[‘PHP_SELF‘]) . ‘/‘; //檔案儲存目錄路徑 $save_path = $php_path . ‘../../../upload/image/leavemessagimg/‘; //檔案儲存目錄URL $save_url = $php_url . ‘../../../upload/image/leavemessagimg/‘; //新檔案名稱 $file_name = $_FILES[‘uploadfiles‘][‘name‘]; $temp_arr = explode(".", $file_name); $file_ext = array_pop($temp_arr); $file_ext = trim($file_ext); $file_ext = strtolower($file_ext); $new_file_name = date("YmdHis") . ‘_‘ . rand(10000, 99999) . ‘.‘ . $file_ext; $file_path = $save_path . $new_file_name; if ((($data[‘uploadfiles‘]["type"] == "image/gif") || ($data[‘uploadfiles‘]["type"] == "image/jpeg") || ($data[‘uploadfiles‘]["type"] == "image/pjpeg")) && ($data[‘uploadfiles‘]["size"] < 1000000)) { if ($data[‘uploadfiles‘]["error"] > 0) { echo "Return Code: " . $data[‘uploadfiles‘]["error"] . "<br />"; } else { if (file_exists($file_path)) { echo $data[‘uploadfiles‘]["name"] . " already exists. "; } else { if(move_uploaded_file($data[‘uploadfiles‘]["tmp_name"],$file_path)==false) { echo ‘上傳失敗‘;exit; } @chmod($file_path, 0644); //儲存圖片路徑 $file_url = ‘/upload/image/leavemessagimg/‘. $new_file_name; } } } else { echo "Invalid file"; } return $file_url; }
原生檔案上傳