N methods for getting File extensions in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags php explode
Summary of N methods for getting File extensions in PHP. Method 2: Copy the code as follows: functionget_extension ($ file) {substr (strrchr ($ file ,.), 1);} 2nd methods: Copy the code as follows: functionget_extension ($ fil 1st methods:

The code is as follows:


Function get_extension ($ file)
{
Substr (strrchr ($ file, '.'), 1 );
}


2nd methods:

The code is as follows:


Function get_extension ($ file)
{
Return substr ($ file, strrpos ($ file, '.') + 1 );
}


3rd methods:

The code is as follows:


Function get_extension ($ file)
{
Return end (explode ('.', $ file ));
}


4th methods:

The code is as follows:


Function get_extension ($ file)
{
$ Info = pathinfo ($ file );
Return $ info ['extension'];
}


5th methods:

The code is as follows:


Function get_extension ($ file)
{
Return pathinfo ($ file, PATHINFO_EXTENSION );
}


The above methods seem to work, especially the 1 and 2 methods, which have been used until I know that pathinfo has a second parameter. However, the first four methods have various problems. To obtain the file extension correctly, you must be able to handle the following three special cases.
No file extension
The path contains characters such as/home/test. d/test.txt.
The path contains characters. but the file has no extension. For example,/home/test. d/test
Obviously: 1 and 2 cannot handle the third case, and 3 cannot correctly handle the first three cases. 4. if the extension does not exist, a warning is issued. Only 5th methods are the most correct methods. Take a look at the pathinfo method. The official website is 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 that contains up to four elements, but does not always have four elements. for example, if there is no extension, no extension element exists, therefore, warnings are detected in the 4th methods. However, phpinfo also supports the second parameter. You can pass a constant to specify a part of data to be returned:
PATHINFO_DIRNAME-Directory
PATHINFO_BASENAME-file name (including extension)
PATHINFO_EXTENSION-extension
PATHINFO_FILENAME-file name (excluding the extension, PHP> 5.2)
The values of these four constants are 1, 2, 4, and 8. at the beginning, I thought that multiple constants can be specified through the OR operation:
Pathinfo ($ file, PATHINFO_EXTENSION | PATHINFO_FILENAME );
Later, we found that this would not work. this would only return the smallest of several or arithmetic constants. That is, the constant whose minimum bit is 1 in the four flag bits.
Below are some supplementary methods
I. php explode function

The code is as follows:


$ Pic = 'ABC. php ';
$ Pics = explode ('.', $ pic );
Echo $ num = count ($ pics );
Echo'
'. $ Pics [$ num-1];



In this way, you can output
. Php.

The following uses foreach

The code is as follows:


Foreach ($ pics as $ value) // 2
{
$ A = $ value;
}
Echo $ .'
';



To have a better function end. I recommend using this function for quick end function usage.

The code is as follows:


Echo end ($ pics );
Echo'
';



Other access methods can be determined during File upload. However, we cannot use $ _ FILES for file upload without uploading.

The signature code is as follows: function get_extension ($ file) {substr (strrchr ($ file ,'. '), 1);} 2nd methods: Code: function get_extension ($ fil...

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.