Friday, May 15, 2009

How to upload a word document in php

I've taken the basic idea from w3schools on how to upload files in php. I will show you now how to modify the code a bit and upload MS-word documents.

Create a php page which contains both upload form (html) and actual php code to load the file. You can have them both separately aswell.

upload.php

<?php

if(isset($_POST['submit']))
{

if ((($_FILES["file"]["type"] == "application/msword")))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "<p>Specifications document uploaded succesfully <br />";

if (file_exists("uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];

echo" <input type='button' value='Back to Previous Page'
onClick='javascript: history.go(-1)'> ";

}
}
}
else
{
echo "Invalid file";
}

}

else {

echo"
<form action='' method='post' enctype='multipart/form-data'>

<p><label for='file'>Filename:</label>
<input type='file' name='file' id='file' />
</p>
<p>
<input type='submit' style='margin-left:110px;' name='submit' value='Submit' />
</p>
</form>";

} ?>

Do not forget to put enctype="multipart/form-data" in the form tags. Here we are uploading word document which is of type msword so this ((($_FILES["file"]["type"] == "application/msword"))) tag specifies that, the file we are going to upload ins word type. This be changed to image type also to upload image files.

As shown in the code files goto uploads directory of your project directory. So uploads directory should be created where you have this code or you will get an error.

Thats it!! once everything is done you should be able to upload the word file.

4 comments:

  1. the uploaded doc is not appearing in the uploads folder. Where did it go?

    Mathew

    ReplyDelete
  2. The uploaded doc disappears as soon the script finishes running. in other words it's quite not useful to you.

    The alternative will be to save the doc while the script is still running.


    0)
    && in_array($extension, $allowedExts))
    {
    if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "
    ";
    }
    else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "
    ";
    echo "Type: " . $_FILES["file"]["type"] . "
    ";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB
    ";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
    ";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
    /* saves doc to folder called upload*/
    {
    echo $_FILES["file"]["name"] . " already exists. ";
    }
    else
    {
    move_uploaded_file($_FILES["file"]["tmp_name"],
    "upload/" . $_FILES["file"]["name"]);
    echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
    }
    }
    }
    else
    {
    echo "Invalid file";
    echo '
    ';
    echo $_FILES["file"]["size"].' kB';
    }

    ReplyDelete
  3. sorry, i could not post HTML. Here's a link
    http://www.w3schools.com/php/php_file_upload.asp

    ReplyDelete
  4. - This code seems to be simple and well written. Nice move.

    I'll try this code.

    ReplyDelete