簡體   English   中英

在php中上傳doc和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;}

對於所有類型的圖像,它都能正常工作。 但是如果是文本文件(doc和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;

   }

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM