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?

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;
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.