How to detemine file extension on php upload?

I've read something about MIME and finfo() but I didn't understand!

Also I tried these:

if ($_FILES["uploadedFile"]["type"] == "text/docx"/*or == document/docx*/) echo "1";

It is obviously that it won't work. When I echo $_FILES['uploadedFile']['type']; then I saw this for a word document(My server is Linux):


So must I use: ?

if ($_FILES["uploadedFile"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")

What is an easy and reliable way to find file extension(Not only Images)? (I mean using some variables which are not filled on client side)

-------------Problems Reply------------

If you are using php 5.3, you can get true mime-type:

$finfo = finfo_open();
$file = $_FILES["uploadedFile"];
finfo_file($finfo, $file, FILEINFO_MIME_TYPE);

More Info:


How about

$file = $_FILES['uploadedFile'['name'];
$pathparts = pathinfo($file);
$ext = $pathparts['extension'];

This will give you the actual file extension to work with.

The best way to determine the file type is by examining the first few bytes of a file – referred to as “magic bytes”. Magic bytes are essentially signatures that vary in length between 2 to 40 bytes in the file headers, or at the end of a file.

your best bet is PECL extension called Fileinfo.

As of PHP 5.3, Fileinfo is shipped with the main distribution and is enabled by default

// in PHP 4 :
$fhandle = finfo_open(FILEINFO_MIME);
$mime_type = finfo_file($fhandle,$file); // e.g. gives for example "image/jpeg" for a jpeg file

// in PHP 5 you can do :

$file_info = new finfo(FILEINFO_MIME); // object oriented approach!
$mime_type = $file_info->buffer(file_get_contents($file));

Category:php Views:1 Time:2012-02-22

Related post

  • what type of interface we will need that will read the file extension of the uploaded file then call the necessary java file 2011-07-13

    what type of interface we will need that will read the file extension of the uploaded file then call the necessary java file. For example: If you uploaded "hello.pdf" then it will call the will clear the metadata). I don't know if this

  • Whitelist or blacklist file extensions for uploads? 2012-02-20

    I'm making a newsletter editor which will allow file uploads (the sender of the newsletter can upload files to the server which will be linked to in the email). The site is set up so that only .do URIs are actually executed/handled by servlets so it'

  • Getting the name & extension of an uploaded file using python (google app engine) 2009-07-26

    I am using a form to upload files to the google app engine and store them in the datastore. l would also like to store the original file name and extension for presentation purposes. Is there a way of retrieving this data from the post sever side or

  • Codeigniter multiple file upload messes file extension 2009-12-15

    I am trying to get a multiple upload library working for my Codeigniter based website. I have it working almost but I have a slight problem if I upload more than one image, the file extentions get screwed up; for example, if I upload three JPEGS, I g

  • php renaming (unknown) uploaded file extension 2010-08-19

    hi guys i have to rename the (unknown) uploaded file extension from .MP3 to .zip please help I have been stuck with this for a few days now. Thanks in advance:) if (!copy($file, $newfile)) { echo "failed to copy $file...\n"; } $filename = "members/$i

  • Detect if user changed file extension to upload? 2010-08-27

    Using a Java servlet, is it possible to detect the true file type of a file, regardless of its extension? Scenario: You only allow plain text file uploads (.txt and .csv) The user takes the file, mypicture.jpg, renames it to mypicture.txt and proceed

  • How to specify file extension in ASP.Net upload control? 2010-09-23

    Is there a way to specify a file extension for ASP.NET upload control? E.g. I want to upload images with jpg and png extensions only. I can implement this easily for a windows application, but struggling with ASP.Net Thank you --------------Solutions

  • Validation of file extension before uploading file 2010-11-20

    I am uploading images to a servlet. The validation whether the uploaded file is an image is done in server side only, by checking the magic numbers in the file header. Is there any way to validate the extensions in client side before submitting the f

  • What's the best way/practice to get the extension of a uploaded file in PHP 2011-01-04

    This question already has an answer here: How to extract a file extension in PHP? 13 answers I have a form that allow users to upload files, I however need to get the file extension, which I am able to get, but not sure if I'm using the most effectiv

  • Restrict file upload to some file extensions 2011-01-13

    i am having a problem in uploading files.. i want to allow users to upload files which the system allows... for example.. I allow files to be uploaded by users having extension of *.jpg. So, in the file selection window they must see only files with

  • CodeIgniter uploads files with duplicate file extensions 2011-02-01

    Any obvious reasons why CodeIgniter would duplicate the file extension when using its Upload library? Tried all sort of ways to set the file name (with and without extension) and nothing seem to work. Any tips? --------------Solutions------------- I'

  • ASP.NET MVC 2 - Uploaded image validation - Is there more to do than checking file extensions and MIME types? 2011-08-10

    I have a somewhat complex edit model coming into (surprise surprise) an Edit ActionMethod. One of the model's properties is a HttpPostedFileBase object, which I'd like to validate and display the validation results on the form if the file isn't legit

  • to validate file extension for uploading file in firefox 2011-11-21

    I created the upload page using with FileUpload server control. And I used regular expression validator to validate file extension. <asp:FileUpload ID="AttachmentUpload" CssClass="text" size="58" Width="376px" IE:Width="385px" runat="server"/>

  • Get file extension in client side with Fine-Uploader 2014-07-17

    I am using Basic Fine-Uploader. Is there any way to get the file extension before uploading it in client side when I select the it? --------------Solutions------------- Use the getName API method to retrieve the name for a file when you have its id.

  • Servlet for file upload - contentType returning 'none' when file extension in CAPS - why? 2012-03-05

    My Java Servlet processes a file sent through a form on my JSP page. I want to accept image files only. So first thing the servlet does is check to see if the content-type is that of an image. To check the content-type, I use : File f = new File(fiel

  • How to extract a file extension in PHP? 2008-10-06

    This is a question you can read everywhere on the web with various answers : $ext = end(explode('.', $filename)); $ext = substr(strrchr($filename, '.'), 1); $ext = substr($filename, strrpos($filename, '.') + 1); $ext = preg_replace('/^.*\.([^.]+)$/D'

  • XCopy - Only grab files that are fully uploaded 2009-01-26

    I have an automated job that pulls files that are uploaded to our servers via a client facing site using xcopy. Is there any way to only pull files that are fully uploaded? I have thought about creating a second "inProcess" folder that will be used f

  • I changed my file extensions, but google hasn't changed 2009-05-10

    Well, I changed my file extensions to .shtml and google still has them indexed as .html. I've tried uploading a sitemap and other things to google webmaster tools but with no luck. Is there any other way I can force google to crawl my site? Im guessi

  • Recommended Apple iPhone Audio Recording Settings and what file extension to use 2009-08-21

    I would like to get your recommendation on what settings to use for audio recording using AVAudioRecorder. Below is the settings I am using currently. Also, what file extension should I save it as so users on Mac or Windows can play it without diffic

Copyright (C), All Rights Reserved.

processed in 0.111 (s). 11 q(s)