Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 677235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:01:01+00:00 2026-05-14T01:01:01+00:00

So I have an upload script, and I want to check the file type

  • 0

So I have an upload script, and I want to check the file type that is being uploaded. I only want pdf, doc, docx and text files

So I have:

$goodExtensions = array('.doc','.docx','.txt','.pdf', '.PDF');
$name = $_FILES['uploadFile']['name'];
$extension = substr($name, strpos($name,'.'), strlen($name)-1);


 if(!in_array($extension,$goodExtensions) || (($_FILES['uploadFile']['type'] != "applicatioin/msword") || ($_FILES['uploadFile']['type'] != "application/pdf"))){
     $error['uploadFile'] = "File not allowed. Only .doc, .docx, .txt and pdf";
 }

Why I’m getting the error when testing and including correct documents?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:01:02+00:00Added an answer on May 14, 2026 at 1:01 am

    Since you are using OR instead of AND in your expression:

     if (!in_array($extension,$goodExtensions)
         || (($_FILES['uploadFile']['type'] != "applicatioin/msword")
             || ($_FILES['uploadFile']['type'] != "application/pdf"))) {
         $error['uploadFile'] = "File not allowed. Only .doc, .docx, .txt and pdf";
     }
    

    this always evaluates to true: if the file extension is listed in the array goodExtensions, the first expression is false. However, since the file type can not be both Word and PDF at the same time, the second bracketed expression is always true.

    So if you want to ensure that either the file extension or the MIME type is good, the correct expression would be (including the fix for the typo in “applicatioin/msword”):

     if (!in_array($extension,$goodExtensions)
         || (($_FILES['uploadFile']['type'] != "application/msword")
             && ($_FILES['uploadFile']['type'] != "application/pdf"))) {
         $error['uploadFile'] = "File not allowed. Only .doc, .docx, .txt and pdf";
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file upload script that works [finally] but want to have users
I have an upload script that write a index.html file, I modified it to
I have an image upload script that batch uploads loads of images. I want
I have used the a jquery script that is used to upload files: http://aquantum-demo.appspot.com/file-upload
Hi I have a question, I want to do an upload script that can
Let's say i have an image uploader script, i want to prevent the upload
I have a PHP script that processes file uploads. The script tries to organise
I have upload file functionality on one of the page. I check for the
I have put together a script which will upload a CSV file and then
I have a script that I've written that I sell to others who want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.