chrisuk Posted March 4, 2007 Share Posted March 4, 2007 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? Quote Link to comment Share on other sites More sharing options...
simcoweb Posted March 4, 2007 Share Posted March 4, 2007 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; ?> Quote Link to comment Share on other sites More sharing options...
chrisuk Posted March 4, 2007 Author Share Posted March 4, 2007 Thanks for that - so could i just restructure it to: <?php if !preg_match('/\\.(doc)$/i', $_FILES['uploadedfile']['name']) { echo "error"; } else { ...continue... } ?> ? Quote Link to comment Share on other sites More sharing options...
simcoweb Posted March 4, 2007 Share Posted March 4, 2007 Yes, that should work. Quote Link to comment Share on other sites More sharing options...
chrisuk Posted March 4, 2007 Author Share Posted March 4, 2007 and it does! the amount of clarting on I have been doing and in the end it comes down to one line of code typical Thanks!! Quote Link to comment Share on other sites More sharing options...
Orio Posted March 4, 2007 Share Posted March 4, 2007 I suggest: <?php if(strtolower(strrchr($_FILES['uploadedfile']['name'], ".")) != ".doc") echo "Invalid!"; ?> Orio. Quote Link to comment Share on other sites More sharing options...
chrisuk Posted March 5, 2007 Author Share Posted March 5, 2007 simcoweb - for some reason jpg files still upload?? bizarre, other formats produce the expected error. Quote Link to comment Share on other sites More sharing options...
simcoweb Posted March 5, 2007 Share Posted March 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.