KAsh
KAsh

Reputation: 304

Uploading doc & docx file in php not working

I have a code which is uploading two files(image & text) in two different folders.

        $file_path = "users/".$uname."/dp/";
        $file_path2 = "users/".$uname."/resume/";

        $q=mkdir("users/".$uname."/dp/", 0777, $recursive=true);
        $r=mkdir("users/".$uname."/resume/", 0777, $recursive=true);
        if($q && $r)
        {
             $targate = $file_path.basename($_FILES['dp']['name']);
            //echo $targate ;die;
            if ((($_FILES['dp']["type"] == "image/gif") || ($_FILES['dp']["type"] == "image/jpeg") || ($_FILES['dp']["type"] == "image/png") || 
                ($_FILES['dp']["type"] == "image/jpg")) && ($_FILES['dp']["size"] < 20000))
            {
                if ($_FILES['dp']["error"] > 0)
                {
                    echo "Return Code: " . $_FILES['dp']["error"] . " ";
                }
                else
                {
                    move_uploaded_file($_FILES['dp']["tmp_name"], $targate);
                }
            }
            else
            {
                //echo "Invalid file";
            }

             $targate2 = $file_path2.basename($_FILES['resume']['name']);
            //echo $targate2 ;die;
            if ((($_FILES["resume"]["type"] == "text/plain")   || ($_FILES["resume"]["type"] == "application/msword")
            || ($_FILES["resume"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")) && $_FILES['resume']["size"] < 20000)
            {
                if ($_FILES['resume']["error"] > 0)
                {
                    echo "Return Code: " . $_FILES['resume']["error"] . " ";
                }
                else
                {
                    move_uploaded_file($_FILES['resume']["tmp_name"], $targate2);

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

            echo "success";die;
        }
        else{ echo "fail";die;}

For all types of images its working fine. But in case of text files (doc & docx files) it prints success but only image file is being uploaded.

When I replace this

if ((($_FILES["resume"]["type"] == "text/plain")
            || ($_FILES["resume"]["type"] == "application/msword")
            || ($_FILES["resume"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")) && $_FILES['resume']["size"] < 20000)

condition with

if (($_FILES["resume"]["type"] == "text/plain")
             && $_FILES['resume']["size"] < 20000)

this works fine for .txt What is the problem? Where I am doing it wrong?

Upvotes: 0

Views: 5721

Answers (2)

sanu
sanu

Reputation: 1

// Above answer is right but for perfection need some slight changes.......
 //  thanx...
//It will store particular document in a folder




<?php

//if we clicked on Upload button

if($_POST['Upload'] == 'Upload')

  {

  //make the allowed extensions

  $goodExtensions = array( '.doc', '.docx',); 

  $error='';

  //set the current directory where you wanna upload the doc/docx files

  $uploaddir = 'upload./ ';

  $name = $_FILES['filename']['name'];//get the name of the file that will be uploaded

  $min_filesize=10;//set up a minimum file size(a doc/docx can't be lower then 10 bytes)

  $stem=substr($name,0,strpos($name,'.'));

  //take the file extension

  $extension = substr($name, strpos($name,'.'), strlen($name)-1);

  //verify if the file extension is doc or docx

   if(!in_array($extension,$goodExtensions))

     $error.='Extension not allowed<br>';

echo "<span> </span>"; //verify if the file size of the file being uploaded is greater then 10

   if(filesize($_FILES['filename']['tmp_name']) < $min_filesize)

     $error.='File size too small<br>'."\n";
   else

  $uploadfile = $uploaddir . $stem.$extension;

  $filename=$stem.$extension;

  if ($error=='')

    {

//upload the file to

  if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {

   echo 'File Uploaded. Thank You.';

    }

   }

    else echo $error;

   }

?>

Upvotes: 0

Jerry Bala
Jerry Bala

Reputation: 11

Step 1

Create one html file named index.html and paste the following code into it.

<html>

<body>

<form enctype="multipart/form-data" method="POST" action="upload.php">This is the code for html:

<table border="0">

<tbody>

<tr>

<td align="left">File:</td>

<td><input accept="doc/docx" name="filename" size="40" type="file" /></td>

</tr>

<tr>

<td><input name="Upload" type="submit" value="Upload" /></td>

</tr>

</tbody></table>

</form>

</body>

</html>

Step 2

Create one PHP file named upload.php and paste the following code.

<?php

//if we clicked on Upload button

if($_POST['Upload'] == 'Upload')

  {

  //make the allowed extensions

  $goodExtensions = array(

  '.doc',

  '.docx',

  ); 

  $error='';

  //set the current directory where you wanna upload the doc/docx files

  $uploaddir = './ ';

  $name = $_FILES['filename']['name'];//get the name of the file that will be uploaded

  $min_filesize=10;//set up a minimum file size(a doc/docx can't be lower then 10 bytes)

  $stem=substr($name,0,strpos($name,'.'));

  //take the file extension

  $extension = substr($name, strpos($name,'.'), strlen($name)-1);

  //verify if the file extension is doc or docx

   if(!in_array($extension,$goodExtensions))

     $error.='Extension not allowed<br>';

echo "<span> </span>"; //verify if the file size of the file being uploaded is greater then 1

   if(filesize($_FILES['filename']['tmp_name']) < $min_filesize)

     $error.='File size too small<br>'."\n";

  $uploadfile = $uploaddir . $stem.$extension;

$filename=$stem.$extension;

if ($error=='')

{

//upload the file to

if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {

echo 'File Uploaded. Thank You.';

}

}

else echo $error;

}

?>

Upvotes: 1

Related Questions