2

how can i upload .doc and .docx using a php script? my script works well on pdf and txt but for doc and docx its not uploading. i need the script to upload pdf doc docx and txt only.

below is my code

session_start();

$targetfolder = "testupload/";

 $targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;

 $ok=1;

$file_type=$_FILES['file']['type'];

if ($file_type=="application/pdf" || $file_type=="application/msword" || $file_type=="text/plain") {

 if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))

 {

 
  $_SESSION['message'] ="The file ". basename( $_FILES["file"]["name"]). " uploaded successfully.";
       header("location:lecsubmit?done") ;
    } 



 else {

   $_SESSION['message']= "Sorry, your file was not uploaded.";
    header("location:lecsubmit?error") ;

 }

}

else {

 
  $_SESSION['message']= "You may only upload PDFs, DOCXs, DOCs or TXT files..";
    header("location:lecsubmit?error") ;
}
Rayobeats
  • 141
  • 2
  • 12

1 Answers1

2

Your if statement checking mime types won't accept docx files.

See here for a list of mime types for MS Office files. In your case you need to allow application/vnd.openxmlformats-officedocument.wordprocessingml.document as well.

May I suggest a minor refactor:

$allowedMimes = [
    'application/pdf',
    'application/msword',
    'text/plain',
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
];

if (in_array($_FILES['file']['type'], $allowedMimes)) {
    // .. continue to upload
} else {
    // show supported file types message
}
scrowler
  • 24,273
  • 9
  • 60
  • 92