1

「ファイルサイズが指定を超えています」と「画像のみをアップロードしてください」というメッセージをユーザーに表示するにはどうすればよいですか?以下のコードで試してみました

if ($_FILES["file"]["size"] > 2097152) { // if file is larger than we want to allow echo "ERROR: Your file was larger than 2MB in file size.";

しかし、それはうまくいきませんでした。

<form action="" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" ><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

これが私のphpコードです。最後に追加することで機能しelse { echo "Invalid file"; }ますが、ユーザーに個別のエラーを表示したかった... txs

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2097152)
&& in_array($extension, $allowedExts))
{

if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

if ($_FILES["file"]["size"] > 2097152) { // if file is larger than we want to allow
echo "ERROR: Your file was larger than 2MB in file size.";
}
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "your photo has been uploaded successfully!";
}
}
}
?> 
4

3 に答える 3

1
&& ($_FILES["file"]["size"] < 2097152)
&& in_array($extension, $allowedExts))

There you are checking to see if the filesize is less than the max allowed. So if it is more that if() statement will return FALSE and the rest of code will not run. That being the case inside that if() block you then do:

if ($_FILES["file"]["size"] > 2097152)

Which you check if it is more, but you have already validated the size with the first if() so a file too large won't trigger the error as it would have already failed the first if()

So to fix it you would remove this line, so you can output the error individually:

 && ($_FILES["file"]["size"] < 2097152)
于 2012-12-29T04:20:06.710 に答える
0

優先順位は次のようにする必要があると思います

  1. ファイルにエラーがあるかどうかを確認しています
  2. ファイルが画像かどうかの確認
  3. ファイル サイズが 2MB 未満かどうかを確認する
  4. ファイルが既に存在するかどうかを確認する

だから私はフォローすることを好む

<?php
if ($_FILES["file"]["error"] > 0) {//Checking file has error or not
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/pjpeg")) { //Checking file is an image or not

        if ($_FILES["file"]["size"] < 2097152) { //checking file size is less than 2MB or not
            if (file_exists("upload/" . $_FILES["file"]["name"])) { //Checking file already exists or not

                echo $_FILES["file"]["name"] . " already exists. ";
            } else {
                move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
                echo "Your photo has been uploaded successfully!";
            }
        } else {
            echo "ERROR: Your file was larger than 2MB in file size.";
        }
    } else {
        echo "Please upload only Image.";
    }
}
?> 
于 2012-12-29T05:33:28.403 に答える
0

ファイルのアップロードでクライアント側の検証を行うかどうかはわかりませんが、そうしたい場合は、すばらしい jQuery プラグインがあります。

http://adamsanderson.github.com/jQuery-File-Validator/

役に立ちます。コードをカスタマイズして、ファイルのサイズとタイプのいずれかまたは両方を検証できます。

于 2012-12-29T06:50:22.570 に答える