0

我想允许用户上传 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";
}

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

4

1 回答 1

0

问题出在 MIME 类型上。正如@ksg91 所说,doc 文件的正确 MIME 类型是 application/msword,而 docx 文件的 MIME 类型是:application/vnd.openxmlformats-officedocument.wordprocessingml.document

于 2012-06-27T11:18:32.170 回答