Jump to content

how to only allow upload of ms word documents?


chrisuk

Recommended Posts

Can anybody tell me how to check that an uploaded file is a .doc?

 

I tried this:

 

<?php

$file = $_FILES['uploadedfile']["name"];

//gets the filename and extension from the file input in the form 
$filenamecheck = $file_name; 
$accept_mtype = "application/ms-word"; 
$m_type =  $HTTP_POST_FILES['uploadedfile']['type']; 
/*Checks whether input file exists  & checks to see whether the file is a pdf 
file. It only uploads PDF files 
*/ 
//comment for debug
if ($m_type != "application/ms-word" ) {
echo "error"; }
else { 
...continue...
?>

 

from a script sample, but this just gives "error" for any file attachment.

 

Can anybody spot what i am doing wrong?

Link to comment
Share on other sites

Normally you'd just validate against the extension since Word documents have the .doc extension. For example, here's how I validate an image extension on a file upload:

 

<?php
$allowedUpload = preg_match('/\\.(gif|jpg|jpeg|png)$/i', $_FILES['photo']['name']) ? true : false;
?>

Link to comment
Share on other sites

That is really weird. If you want to, try Orio's code suggestion instead. It's similar but uses some additional techniques that could be really helpful like the strtolower function that changes everything to lowercase which, in turn, makes it easier to validate.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.