1

我正在为老师制作一个网络表格,学生可以在那里提交作业,老师可以检查。但我收到错误,我无法上传 .txt 和 .doc 我的代码中有问题,这是我的努力。

error_reporting(E_ALL ^ E_NOTICE);
if ($_POST["upload"] == "1") {

    if ((($_FILES['file']['type'] == ".txt") || ($_FILES['file']['type'] == ".doc")) && ($_FILES['file']['size'] > "0")) {
        $id = 4881;
        $name = "Naeem";
        /*first image folder i i showed abd get file and move*/
        $fileName = $_FILES["file"]["name"];
        $fileName = preg_replace('#[^a-z.0-9]#i', '', $fileName);
        $kaboom = explode(".", $fileName);
        // Split file name into an array using the dot
        $fileExt = end($kaboom);
        // Now target the last array element to get the file extension
        $fileName = $id . "(" . time() . rand() . ")." . $fileExt;
        $to = "file/" . $fileName;

        /*this step is used to move file from tmp to a folder*/
        if (move_uploaded_file($_FILES['file']['tmp_name'], $to)) {
            if ($query = mysql_query("INSERT INTO `file` (
                                        `id` ,
                                        `std_id` ,
                                        `std_name` ,
                                        `file_url`
                                        )
                                        VALUES (
                                            NULL , '" . $id . "', '" . $name . "', '" . $to . "'
                                        );"))
            {
                echo "Uploaded succesfully";
            }
        }
    }
}
4

2 回答 2

3

您的代码中有小错误,您放错了扩展名。

if((($_FILES['file']['type']=="text/plain") || ($_FILES['file']['type']=="application/msword"))&&($_FILES['file']['size']>"0"))

它不是 .txt 和 .doc ,它是text/plain对于文本文件,对于 .doc 它是application/msword。我希望它会奏效。

于 2013-09-11T11:30:55.690 回答
3

这是问题 -

(($_FILES['file']['type']==".txt") || ($_FILES['file']['type']==".doc"))

文件扩展名MIME 类型不相同。

替换.txttext/plainAnd 。.doc_application/msword

于 2013-09-11T11:31:08.497 回答