[php] 上傳多檔並判斷檔案格式與上傳錯誤訊息
除了判斷副檔名外還要判斷格式是不是符合副檔名。
抓取上傳錯誤訊息
- $uptypes = array (
- 'image/jpg',
- 'image/jpeg',
- 'image/pjpeg',
- 'image/gif',
- 'image/png'
- );
- foreach($_FILES['newPic']['tmp_name'] as $key => $tmp_name ){
- $fileName=$_FILES["newPic"]["name"][$key];
- //取得檔案副檔名
- $fileExt=pathinfo($_FILES["newPic"]["name"][$key], PATHINFO_EXTENSION);
- if(in_array($_FILES['newPic']['type'][$key], $uptypes)) {
- /*上傳圖片類型為jpg,pjpeg,jpeg */
- if (strstr(strtolower($_FILES['newPic']['type'][$key]), "jp")) {
- if(!($source = @ imageCreatefromjpeg($_FILES['newPic']['tmp_name'][$key]))){
- echo '檔案類型錯誤';
- return;
- }
- /*上傳圖片類型為png */
- }elseif(strstr(strtolower($_FILES['newPic']['type'][$key]), "png")) {
- if(!($source = @ imagecreatefrompng($_FILES['newPic']['tmp_name'][$key]))){
- echo '檔案類型錯誤';
- return;
- }
- /*上傳圖片類型為gif */
- }elseif(strstr(strtolower($_FILES['newPic']['type'][$key]), "gif")) {
- if(!($source = @ imagecreatefromgif($_FILES['newPic']['tmp_name'][$key]))){
- echo '檔案類型錯誤';
- return;
- }
- /*其他例外圖片排除 */
- } else {
- echo '檔案類型錯誤';
- return;
- }
- }
- }
抓取上傳錯誤訊息
- if($_FILES['newPic']['error'] > 0){
- switch($_FILES['newPic']['error']){
- case 1:
- $error="檔案大小超出 php.ini:upload_max_filesize 限制";
- break;
- case 2:
- $error="檔案大小超出 MAX_FILE_SIZE 限制";
- break;
- case 3:
- $error="檔案僅被部分上傳";
- break;
- case 4:
- $error="檔案未被上傳";
- break;
- }
- }
留言