对php文件上传的限制不起作用

时间:2012-06-27 10:48:15

标签: php forms

我想允许用户上传txt,doc或pdf格式的文件。我已经为它编写了以下代码。

if ((($_FILES["uploaded"]["type"] == "text/plain")
        || ($_FILES["uploaded"]["type"] == "application/pdf")
        || ($_FILES["uploaded"]["type"] == "application/doc"))
        && ($_FILES["uploaded"]["size"] < 50000))
{
    if ($_FILES["uploaded"]["error"] > 0)
    {
        echo "Error: " . $_FILES["uploaded"]["error"] . "<br />";
    }
    else
    {
        $target = "upload/";
        $target = $target . basename($_FILES['uploaded']['name']);
        $ok = 1;
        if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
        {
            echo "The file " . basename($_FILES['uploadedfile']['name']) . " has been uploaded";
        }
        else
        {
            echo "Sorry, there was a problem uploading your file.";
        }
    }
}
else
{
    echo "Invalid file";
}

但是这段代码不起作用。无论是什么情况,它总是显示无效文件。可能是什么问题?

1 个答案:

答案 0 :(得分:0)

问题与MIME类型有关。正如@ ksg91所说,doc文件的正确MIME类型是application / msword,对于docx文件,它是:application / vnd.openxmlformats-officedocument.wordprocessingml.document