PHP Upload form, PDF, Doc & Docx

前端 未结 3 1412
萌比男神i
萌比男神i 2020-12-21 05:13

I\'m struggling to make this upload code work for a docx file, it works okay for doc and pdf..

$allowedExts = array(\"pdf\", \"doc\", \"docx\");
$extension =         


        
相关标签:
3条回答
  • 2020-12-21 05:53

    For docx check this MIME type

    application/vnd.openxmlformats-officedocument.wordprocessingml.document
    

    EDIT :

    Here's the code . You're missing parenthesis

    <?php
    
        $allowedExts = array("pdf", "doc", "docx");
        $extension = end(explode(".", $_FILES["file"]["name"]));
        if (($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword") || ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts))
        {
          if ($_FILES["file"]["error"] > 0)
          {
             echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
          }
          else
          {
            echo "Success";
          }
      }
    
    0 讨论(0)
  • 2020-12-21 05:54

    There is another way to do the task. Just check the file type and then you can work further.

    Here is code to check the filetype.

    $target_dir = "uploads/";
    $filename= $_FILES["fileupload"]["name"]; //gets filename with type
    $target_file = $target_dir . basename($filename); //uploads/file.type
    echo $target_file;
    $extension= pathinfo($target_file,PATHINFO_EXTENSION); 
    $imageFileType = strtolower($extension);
    if(strcmp($imageFileType,"docx")==0){
        echo "Its word file";
    }
    
    0 讨论(0)
  • 2020-12-21 06:06

    This following check will help you to upload .docx files:

    $_FILES["txtFile"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    
    0 讨论(0)
提交回复
热议问题