Php get file extension from uploaded file

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

Machavity

30.1k26 gold badges87 silver badges98 bronze badges

asked Mar 25, 2011 at 0:25

0

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

echo $ext;

answered Mar 25, 2011 at 0:26

Dejan MarjanovićDejan Marjanović

19.1k7 gold badges51 silver badges66 bronze badges

5

You could use pathinfo[]:

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

answered Mar 25, 2011 at 0:29

JKSJKS

3,6302 gold badges28 silver badges39 bronze badges

4

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.

answered Mar 25, 2011 at 0:27

GWWGWW

42k11 gold badges110 silver badges106 bronze badges

2

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

answered Mar 25, 2011 at 0:28

thfthf

5841 gold badge10 silver badges22 bronze badges

The file extension is very useful for validation and upload. You can easily get extension from the file name or file location using PHP. In this tutorial, we will show you two simple way to get file extension in PHP.

Custom PHP Function

The get_file_extension[] function returns extension of the file using substr[] and strrchr[] in PHP.

  • substr[] – Returns a part of a string.
  • strrchr[] – Finds the last occurrence of a string inside another string.
function get_file_extension[$file_name] {
    return 
substr[strrchr[$file_name,'.'],1];
}

Usage
You need to pass the filename in get_file_extension[], it will return the extension of the given file.

echo get_file_extension['image.jpg'];

PHP pathinfo[] Function

The pathinfo[] function provides the easiest way to get file information in PHP.

  • pathinfo – Returns the details information about a file path.

Usage
The file path needs to be passed in pathinfo[], it will return the information [directory name, base file name, extension, and filename] of the given file.

$path_parts pathinfo['files/codexworld.pdf'];//file directory name
$directoryName $path_parts['dirname'];//base file name
$baseFileName $path_parts['basename'];//file extension
$fileExtension $path_parts['extension'];//file name
$fileName $path_parts['filename'];

How can I get file extension in PHP?

There are a few different ways to extract the extension from a filename with PHP, which is given below: Using pathinfo[] function: This function returns information about a file. If the second optional parameter is omitted, an associative array containing dirname, basename, extension, and the filename will be returned.

How do I find the file extension?

Viewing the file extension of a single file.
Right-click the file..
Select the Properties option..
In the Properties window, similar to what is shown below, see the Type of file entry, which is the file type and extension. In the example below, the file is a TXT file with a . txt file extension..

What is $_ files in PHP?

$_FILES is a two-dimensional associative global array of items which are being uploaded via the HTTP POST method and holds the attributes of files such as: Attribute. Description. [name] Name of file which is uploading.

How can I get file extension in codeigniter?

I created simple example for getting file extension in codeigniter project. so you can see both example, i hope it can help you. $fileName = $this->upload->data['image']; $fileExt = pathinfo[$fileName, PATHINFO_EXTENSION];

Chủ Đề