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))

在那里您正在检查文件大小是否小于允许的最大值。因此,如果更多 if() 语句将返回 FALSE 并且其余代码将不会运行。在 if() 块内部就是这种情况,然后您可以执行以下操作:

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

您检查它是否更多,但是您已经使用第一个 if() 验证了大小,因此文件太大不会触发错误,因为它已经使第一个 if()失败

因此,要修复它,您将删除此行,以便您可以单独输出错误:

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

我不确定你是否想为文件上传进行客户端验证,但如果你想这样做,有一个不错的 jQuery 插件。

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

这会很有帮助。您可以自定义代码以验证文件的大小和类型中的一个或两个。

于 2012-12-29T06:50:22.570 回答
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 回答