2013-10-27 124 views
0
if(isset($_POST['submit'])&& isset($_FILES['file'])){ 
    $Uploads = new Uploads($_FILES); 
    $Uploads->UploadLocation = "../images/categories/"; 
    if($Uploads->isValidated()== TRUE){ 
    $Image = array("image" => $Uploads->upload()); 
    $_POST = array_merge($_POST,$Image); 
    unset($_POST['submit']); 
    $Category = new Categories(); 
    $Category->insertCategory("users", $_POST); 
    } 
    else { 
     print_r($Uploads->isValidated()); 
    } 
} 
?> 

如果在$Uploads->isValidated()函數之前聲明瞭聲明,那麼其餘的代碼只能在它返回true時才運行。以下是功能代碼。每次都返回TRUE

public function isValidated(){ 
     if($this->containsError() && $this->isValidFile() && $this->isValidSize()){ 
      return TRUE; 
     } 
     else 
     { 
      return $this->ValidationMessages; 
     } 
    } 

如果所有的三種方法返回TRUE比我isValidated()方法應該返回TRUE我已經檢查。 $this->ValidationMessages是一個消息數組,如果三種驗證方法中的任何一種都不返回TRUE,則填充這些消息。

現在我刻意不傳遞任何文件到這個類來檢查我是否收到錯誤消息,但它仍在運行代碼的其餘部分,似乎我的isValidated()方法返回TRUE,它不應該。

請注意,我的3個驗證方法工作完美,因爲我已經檢查了所有這些,這就是爲什麼我不在這裏發佈它們。但如果你需要我可以發佈代碼。

我需要幫助弄清楚爲什麼我沒有得到驗證信息。

更新部分:

private function containsError(){ 
     //checking if file contains any error 
     if(!empty($this->FileError)){ 
      $this->ValidationMessages[] = "Sorry, This file contains error"; 
     } 
     else { 
      return TRUE; 
     }  
    } 

    private function isValidFile() { 
     // putting the allowed files in array 
     $AllowedExt = array('jpg', 'jpeg', 'gif', 'png'); 
     // matching with allowed file types 
     if (in_array($this->FileExt, $AllowedExt)) { 
      return TRUE; 
     } else { 
      $this->ValidationMessages[] = "This extension is not allowed"; 
      return FALSE; 
     } 
    } 

private function isValidSize() { 
    // setting the maximum size limit in bytes 
    $AllowedSize = 1048576; 
    // checking if the user file does not exceed the allowed size 
    if (!$this->FileSize < $AllowedSize) { 
     return TRUE; 
    } else { 
     $this->ValidationMessages[] = "File should not be greater than 1MB"; 
     return FALSE; 
    } 
} 
+4

'$ Uploads-> isValidated()=== TRUE' – raina77ow

+4

'如果($這個 - > containsError()'應該是'如果($這個 - > containsError()' - 它只是讓更多感覺 – Masadow

+0

看不到包含錯誤正在做什麼,但它看起來像你在說「如果有一個錯誤,其他一切正常)...怎麼會有錯誤,一切正常?該功能檢查並返回什麼? – Gavin

回答

1

isValidated()總是返回一個值,該值計算結果爲true(即不是純粹的布爾與true值,但評價爲true儘管如此)。這是因爲它要麼返回TRUE,要麼返回一個(假定)非空的字符串數組。

你有兩個選擇:

  1. 要麼讓isValidated()回報falseelse情況下

  2. 或更改ifif($Uploads->isValidated()===TRUE)(注意三倍=)。這將不僅檢查價值,而且還檢查由isValidated()返回的值的類型。換句話說,只有在返回值爲的值爲true而不僅僅是任何值爲true的值時,它纔會成功。

+0

我已經嘗試了更早..我再次做,因爲我在課堂上做了一些改變。 –

+0

@geogmagas感謝它的工作好,但在這裏得到了一個更多的問題..我print_r(消息)及其顯示 數組([0] =>對不起,此文件包含錯誤[1] =>對不起,此文件包含錯誤)它顯示了2次? –

+1

因爲你調用'$ Uploads-> isValidated()'兩次 - 一次在條件中,一次在'else'子句中。 – geomagas

相關問題