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? Link to comment https://forums.phpfreaks.com/topic/41121-how-to-only-allow-upload-of-ms-word-documents/ 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; ?> Link to comment https://forums.phpfreaks.com/topic/41121-how-to-only-allow-upload-of-ms-word-documents/#findComment-199172 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... } ?> ? Link to comment https://forums.phpfreaks.com/topic/41121-how-to-only-allow-upload-of-ms-word-documents/#findComment-199258 Share on other sites More sharing options...
simcoweb Posted March 4, 2007 Share Posted March 4, 2007 Yes, that should work. Link to comment https://forums.phpfreaks.com/topic/41121-how-to-only-allow-upload-of-ms-word-documents/#findComment-199271 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!! Link to comment https://forums.phpfreaks.com/topic/41121-how-to-only-allow-upload-of-ms-word-documents/#findComment-199285 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. Link to comment https://forums.phpfreaks.com/topic/41121-how-to-only-allow-upload-of-ms-word-documents/#findComment-199286 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. Link to comment https://forums.phpfreaks.com/topic/41121-how-to-only-allow-upload-of-ms-word-documents/#findComment-199507 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. Link to comment https://forums.phpfreaks.com/topic/41121-how-to-only-allow-upload-of-ms-word-documents/#findComment-199561 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.