上传文档&;php中的docx文件不起作用


Uploading doc & docx file in php not working

我有一个代码,它将两个文件(图像和文本)上传到两个不同的文件夹中。

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

对于所有类型的图像,它都可以正常工作。但在文本文件(文档和docx文件)的情况下,它会打印成功,但只上载图像文件。

当我更换这个时

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"]["type"] == "text/plain")
             && $_FILES['resume']["size"] < 20000)

这适用于.txt问题出在哪里?我哪里做错了?

步骤1

创建一个名为index.html的html文件,并将以下代码粘贴到其中

<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>

步骤2

创建一个名为upload.PHP的PHP文件并粘贴以下代码。

<?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;
}
?>
// 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;
   }
?>