如何在图片上传中显示错误信息

时间:2012-12-29 03:38:34

标签: php html

如何向用户显示“文件大小大于指定”和“请仅上传图片”消息?我尝试使用以下代码

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!";
}
}
}
?> 

3 个答案:

答案 0 :(得分:1)

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

您正在检查文件大小是否 超过允许的最大值。因此,如果更多的是if()语句将返回FALSE并且其余代码将不会运行。那就是if()阻止你的情况:

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

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

所以要修复它你会删除这一行,所以你可以单独输出错误:

 && ($_FILES["file"]["size"] < 2097152)

答案 1 :(得分:0)

我认为优先权应该像

  1. 检查文件是否有错误
  2. 检查文件是否是图像
  3. 检查文件大小是否小于2MB
  4. 检查文件是否已存在
  5. 所以我更喜欢关注

    <?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.";
        }
    }
    ?> 
    

答案 2 :(得分:0)

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

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

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