Finding Extension of uploaded file (PHP)

PhpFile

Php Problem Overview


Can We find out the extension of the original file from $_FILES["file"]["tmp_name"] ? For example jpg or png etc?

Php Solutions


Solution 1 - Php

$name = $_FILES["file"]["name"];
$ext = end((explode(".", $name))); # extra () to prevent notice

echo $ext;

Solution 2 - Php

You could use pathinfo():

$path_parts = pathinfo($_FILES["file"]["name"]);
$extension = $path_parts['extension'];

Solution 3 - Php

Yes you can use $_FILES['file']['name'] to get the original name of the uploaded file. Just keep in mind that the extension may not always represent the real contents of the file.

Solution 4 - Php

Yes, assuming it's accurately named. It will retain its original name and extension.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeterView Question on Stackoverflow
Solution 1 - PhpDejan MarjanovićView Answer on Stackoverflow
Solution 2 - PhpJKSView Answer on Stackoverflow
Solution 3 - PhpGWWView Answer on Stackoverflow
Solution 4 - PhpthfView Answer on Stackoverflow