File uploading code not working for .doc file type

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

iRaajan
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Apr 22, 2013 1:58 am

File uploading code not working for .doc file type

Postby iRaajan » Mon Apr 22, 2013 2:05 am

I'm writing PHP code to upload .doc file but it does not giving me desired functionality. I've tested the code for .txt and .pdf file for which it is uploading file but not working with .doc. Here's my code.
BTW, it echos message 'Invalid File'

Code: Select all

<?php
   if(isset($_REQUEST['btn_upload']))
    {
        if ((($_FILES["file"]["type"] == "application/msword")) && ($_FILES["file"]["size"] < 20000))
        {
            if ($_FILES["file"]["error"] > 0)
            {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
            }
            else
            {
                if (file_exists("Documents/" . $_FILES["file"]["name"]))
                {
                    echo $_FILES["file"]["name"] . " already exists. ";
                }
                else
                {
                    move_uploaded_file($_FILES["file"]["tmp_name"],"Documents/" . $_FILES["file"]["name"]);
                    echo "Stored in: " . "Documents/" . $_FILES["file"]["name"];
                }
            }
        }
        else
        {
            echo "Invalid file";
        } 
    }
?>
.../* HTML */
...
<form name="form_add_document" method="post" enctype="multipart/form-data">
<select name="SelectDocFormat">
    <option value="Select Document Format" selected="selected">Select Category</option>
        <option value="TEXT">Text</option>
        <option value="DOC">Word Document</option>
</select>
<input type="file" name="file" value="Browse File">
<input type="submit" name="btn_upload" value="Upload Document">
...
...

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: File uploading code not working for .doc file type

Postby johnj » Mon Apr 22, 2013 6:15 am

The $_FILES['file']['type'] is a user supplied value and hence not a good candidate for validation. Use one of the fileinfo() functions, http://www.php.net/manual/en/function.finfo-file.php


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 3 guests