Aitazaz Khan
Aitazaz Khan

Reputation: 1605

I am getting error while upload the .txt extension file and .doc extension files

I am making a web form for teachers where student can submit there assignment and teacher can check. but i am getting error i can not upload the .txt and .doc i am having probelm in my code here is my effort.

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

Upvotes: 1

Views: 368

Answers (2)

Ashwini Agarwal
Ashwini Agarwal

Reputation: 4858

Here is the issue -

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

File Extension and MIME Type are not same.

Replace .txt by text/plain And .doc by application/msword.

Upvotes: 3

syed shah
syed shah

Reputation: 678

You have minor mistake in your code that you have put wrong extension.

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

It is not .txt and .doc it is text/plain for text files and for .doc it is application/msword. I hope it will work.

Upvotes: 3

Related Questions