2013-06-29 92 views
0

我做了一個上傳文件的功能。應用某些驗證,但我的驗證無法正常工作我試過但我無法得到該錯誤。PHP上傳驗證不起作用

這裏是我的代碼:

function upload($file){ 

     $allowedExts = array("gif", "jpeg", "jpg", "png"); 
     $filename=$_FILES[$file]['name'];      //file name 
     $filetmp=$_FILES[$file]['tmp_name'];     //file in php tmp folder 
     $filesize=$_FILES[$file]['size'];      //file size in bytes 
     $filetype=$_FILES[$file]['type'];      //file type 
     $fileerror=$_FILES[$file]['error'];      //0=false && 1=true 
     $extension = end(explode(".",$filename));    //split the file name into array using a dot 

     if (( ($filetype == "image/gif") 
      || ($filetype == "image/jpeg") 
      || ($filetype == "image/jpg") 
      || ($filetype == "image/pjpeg") 
      || ($filetype == "image/x-png") 
      || ($filetype == "image/png")) 
      && ($filesize < 20000) 
      && in_array($extension, $allowedExts) 
      && ($fileerror==0)){ 
      if (file_exists("assets/upload/park_logo/upload/" . $filename)?FALSE :TRUE) 
      { 
       if(TRUE){ 
        move_uploaded_file($filetmp,'assets/upload/park_logo/'.$filename);  
       } 
      }else{ 
       return FALSE; 
      } 
     }else{ 
      return FALSE; 
     } 
    } 

調用此函數: -

if(upload('logo_upload')==FALSE) { 
    $error[]="please upload file size:2mb,type:png or jpeg format"; 
} 

它顯示錯誤消息,並上傳成功。

注意: - 我們可以像這樣工作。我有兩個上傳字段我可以用這樣的

if(upload('logo_upload'|| 'pic_upload')==FALSE) { 
    $error[]="please upload file size:2mb,type:png or jpeg format"; 
} 
+0

是什麼錯誤消息? –

+0

'if(TRUE){'是沒有意義的。它會一直運行,所以你不需要它。三元'if(file_exists(...)?FALSE:TRUE)'也是不必要的。 –

+0

@K Abhishek: - 請上傳文件大小:2mb,請輸入:png或jpeg格式 –

回答

1

更改此:

if (file_exists("assets/upload/park_logo/upload/" . $filename)?FALSE :TRUE) 
{ 
     if(TRUE){ 
      move_uploaded_file($filetmp,'assets/upload/park_logo/'.$filename);  
     } 
}else{ 
    return FALSE; 
} 

這樣:

if (file_exists("assets/upload/park_logo/upload/" . $filename)) { 
    if (move_uploaded_file($filetmp,'assets/upload/park_logo/'.$filename) { 
     return true; 
    } 
    else { 
     return false; 
    } 
} 
else { 
    return false; 
} 
1

試試這樣說:

function upload($file){ 
    $allowedExts = array("gif", "jpeg", "jpg", "png"); 
    $filename=$_FILES[$file]['name'];      //file name 
    $filetmp=$_FILES[$file]['tmp_name'];     //file in php tmp folder 
    $filesize=$_FILES[$file]['size'];      //file size in bytes 
    $filetype=$_FILES[$file]['type'];      //file type 
    $fileerror=$_FILES[$file]['error'];      //0=false && 1=true 
    $extension = end(explode(".",$filename));    //split the file name into array using a dot 

    if (( ($filetype == "image/gif") 
     || ($filetype == "image/jpeg") 
     || ($filetype == "image/jpg") 
     || ($filetype == "image/pjpeg") 
     || ($filetype == "image/x-png") 
     || ($filetype == "image/png")) 
     && ($filesize < 20000) 
     && in_array($extension, $allowedExts) 
     && ($fileerror==0)){ 
     if (!file_exists("assets/upload/park_logo/upload/" . $filename)) 
     { 
      return move_uploaded_file($filetmp,'assets/upload/park_logo/'.$filename);  
     }else{ 
      return FALSE; 
     } 
    }else{ 
     return FALSE; 
    } 
}