Get extension of file php

Published 19 Nov by SuperNOVA

Get extension of file phpMany times we handle file uploads or other file related tasks in which we need to check the extension of some file. In this post we will be doing just that.
Let’s start by making a simple file upload form: We need to provide the enctype attribute with a value of multipart/form-data to make it clear to the browser that this is a file upload form. Here, upload.php is the file that is going to handle the file upload functionality. Now, let’s see the code that gets the extension of the file: So, if your file name is something.php.jpg, the $ext variable will contain jpg.
If you want to use extensions to increase security in your file uploads, have a backup plan as this method is not very effective.
There are other methods to find the extension as well, for example, you can find the position of the last dot in file name and return the substring from the next position till the end: Again, $ext will have a value of jpg.