anujgarg Posted October 12, 2009 Share Posted October 12, 2009 Hi I have following query: I need to upload a file on server. Before uploading it, I need to check its type and its genuineness. Suppose, I am willing to uplaod an image file having extension .jpg. But, mistakenly, I renamed a .txt file to .jpg file on my local system and then tried to upload it. I want to avoid this fake .jpg file to get uploaded on server. Will the function filetype() be suitable for that? Will it check the file type and its behaviour (if it is real .jpg file or not)?? Please suggest me if there is some other function for this... TIA Anuj Garg Link to comment https://forums.phpfreaks.com/topic/177402-check-file-type/ Share on other sites More sharing options...
cags Posted October 12, 2009 Share Posted October 12, 2009 When you upload a file using a normal form the $_FILES[] array has a type value $_FILES['file']['type'] this would probably be a good place to start. Link to comment https://forums.phpfreaks.com/topic/177402-check-file-type/#findComment-935368 Share on other sites More sharing options...
anujgarg Posted October 12, 2009 Author Share Posted October 12, 2009 Thanks cags for quick reply. According to you, $_FILES['file']['type'] will also check the real type of the file like it is indeed an image file and not a txt file. Link to comment https://forums.phpfreaks.com/topic/177402-check-file-type/#findComment-935379 Share on other sites More sharing options...
cags Posted October 12, 2009 Share Posted October 12, 2009 It checks the MIME type of the file, which should be irrelevant of the file extension. However bare in mind that the value is set client side so it can be overriden if somebody is attempting to circumvent security, also note that not all browsers use the same MIME types. There was another thread fairly recently on the topic, which suggested using a combination of techniques, if you search the forum you should find it. Link to comment https://forums.phpfreaks.com/topic/177402-check-file-type/#findComment-935387 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.