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 effective solution
I can get it using the following ways
$fileInfo = pathinfo($_FILES['File']['name']);
echo $fileInfo['extension'];
$ext = end(explode('.',$_FILES['File']['name']));
echo $ext;
Which method is the best to use or are there even better solutions that would get the extension?
The simplest way to get file extension in PHP is to use PHP's built-in function pathinfo. Save this answer.
The full filename is first obtained by selecting the file input and getting its value property. This returns the filename as a string. By the help of split() method, we will split the filename into 2 parts. The first part will be the filename and the second part will be the extension of the file.
pathinfo($_FILES['File']['name'], PATHINFO_EXTENSION)
options = PATHINFO_EXTENSION
)Better way - fileinfo extension with PHP >=5.3
PS: do not trust file name extension, any user can any how rename the file extension
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With