我嘗試上傳代碼,但無法找到如何使錯誤僅在其真實時才顯示。因爲現在例如如果文件不是圖像,顯示我所有可能的錯誤。 「文件不是image.Sorry,您的文件太大。只有JPG,JPEG,PNG &允許GIF文件。不過,您的文件沒有上傳。」一切都很好,除了即時嘗試上傳圖像。我想告訴我只有錯誤,並非所有可能的錯誤。所以我希望它現在只顯示只有JPG,JPEG,PNG & GIF文件是允許的。不要它顯示你的文件太大,即使它不太大。我該怎麼做?這裏是全碼:編碼的Php - 即使它們不相關也會顯示錯誤消息
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif") {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {enter code here
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
P.S林總初學者:)這樣做的時候也有很多潛在的錯誤
鏈接的所有條件與其他檢查錯誤的順序。實際上他們都是獨立的,他們都在你的榜樣中評價爲真。被別的鏈子連接,你的代碼會停在第一個真實的 –