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;
}
}
'$ Uploads-> isValidated()=== TRUE' – raina77ow
'如果($這個 - > containsError()'應該是'如果($這個 - > containsError()' - 它只是讓更多感覺 – Masadow
看不到包含錯誤正在做什麼,但它看起來像你在說「如果有一個錯誤,其他一切正常)...怎麼會有錯誤,一切正常?該功能檢查並返回什麼? – Gavin