Finding Extension of uploaded file (PHP) [duplicate]

Posted on

Finding Extension of uploaded file (PHP) [duplicate] – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and file . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about Finding Extension of uploaded file (PHP) [duplicate].

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

Solution :

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

echo $ext;

You could use pathinfo():

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

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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *