n ways to get file extensions in PHP summary _php tips

Source: Internet
Author: User
Tags explode file upload php explode
The 1th method:
Copy Code code as follows:

function Get_extension ($file)
{
substr (STRRCHR ($file, '. '), 1);
}

The 2nd method:
Copy Code code as follows:

function Get_extension ($file)
{
Return substr ($file, Strrpos ($file, '. ') +1);
}

The 3rd method:
Copy Code code as follows:

function Get_extension ($file)
{
Return End (Explode ('. ', $file));
}

The 4th method:
Copy Code code as follows:

function Get_extension ($file)
{
$info = PathInfo ($file);
return $info [' extension '];
}

The 5th method:
Copy Code code as follows:

function Get_extension ($file)
{
Return PathInfo ($file, pathinfo_extension);
}

The above several ways coarse look, seems to be all right, especially 1, 2 kinds of methods, before I do not know PathInfo has the second parameter has been used. But think about it, the first four methods have all kinds of problems. To get the file extensions exactly right, you must be able to handle the following three special cases.
No file name extension
The path contains characters. such as/home/test.d/test.txt
The path contains characters., but the file does not have an extension. such as/home/test.d/test
Obviously: 1, 2 can not deal with the third situation, 3 can not correctly deal with the 13th case. 4 can be handled correctly, but a warning is issued when no extension exists. Only the 5th method is the right approach. By the way, look at the PathInfo method. The official website introduces as follows:
$file _path = pathinfo ('/www/htdocs/your_image.jpg ');

echo "$file _path [' dirname ']\n";
echo "$file _path [' basename ']\n";
echo "$file _path [' extension ']\n";
echo "$file _path [' filename ']\n]; Only in PHP 5.2+
It returns an array containing up to four elements, but does not always have four, for example, without an extension, there will be no extension element, so the 4th method will find the warning. But Phpinfo also supports the second parameter. You can pass a constant that specifies the data to return a part of:
Pathinfo_dirname-Table of Contents
Pathinfo_basename-file name (including extension)
Pathinfo_extension-name extension
Pathinfo_filename-filename (without extension, php>5.2)
The values for these four constants are 1, 2, 4, 8, and at first I thought you could specify multiple by or operation:
PathInfo ($file, Pathinfo_extension | Pathinfo_filename);
It turns out that this is not going to work, and that only returns a few of the ones that are the smallest of the constants or operands. That is, a constant with a minimum of 1 digits in four flags.
here are some additional methods
One, PHP explode function
Copy Code code as follows:

$pic = ' abc.php ';
$pics = Explode ('. ', $pic);
echo $num = count ($pics);
Echo ' <br> '. $pics [$num-1];


This will allow you to output
. php.

The following uses foreach
Copy Code code as follows:

foreach ($pics as $value)//2
{
$a = $value;
}
echo $a. ' <br> ';


To have a better function end I recommend using this function shortcut End Function usage
Copy Code code as follows:

Echo End ($pics);
Echo ' <br> ';


Other methods can be used to determine the file upload, but that requires file upload and not upload we can not use $_files to operate.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.