上传.txt扩展名文件和.doc扩展名文件时出错

时间:2013-09-11 11:26:14

标签: php file-upload

我正在为教师制作一个网络表格,学生可以在那里提交作业,教师可以查看。但我收到错误我无法上传.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";
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

您的代码中存在轻微错误,导致扩展错误。

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

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

答案 1 :(得分:3)

以下是问题 -

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

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

.txt改为text/plain而将.doc改为application/msword

相关问题