How to get the file extension in PHP _ PHP Tutorial

Source: Internet
Author: User
PHP to obtain the file extension. PHP methods for obtaining file extensions there are many methods. the following three methods are provided. you can study them. I will not explain them in detail, how to obtain the file extension directly from PHP

There are many methods for PHP to get the file extension. the following three methods are provided. you can study these methods without explaining them and give the final correct answer.

Echo pathinfo ('/www/htdocs/your_image.jpg', PATHINFO_EXTENSION );
Incorrect syntax:
You may write

Function get_file_extension ($ file_name ){
Return substr (strrchr ($ file_name, '.'), 1 );
}
Or write it like this

Function file_extension ($ filename ){
Return end (explode (".", $ filename ));
}
What does pathinfo do?

$ 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 +
?>
The above output

/Www/htdocs
Your_image.jpg
Jpg
Your_image


The code is as follows:
// Method 1
Function extend_1 ($ file_name)
{
$ Retval = "";
$ Pt = strrpos ($ file_name ,".");
If ($ pt) $ retval = substr ($ file_name, $ pt + 1, strlen ($ file_name)-$ pt );
Return ($ retval );
}
// Method 2
Function extend_2 ($ file_name)
{
$ Extend = pathinfo ($ file_name );
$ Extend = strtolower ($ extend ["extension"]);
Return $ extend;
}
// Method 3
Function extend_3 ($ file_name)
{
$ Extend = explode (".", $ file_name );
$ Va = count ($ extend)-1;
Return $ extend [$ va];
}
?>

Http://www.bkjia.com/PHPjc/632277.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632277.htmlTechArticlePHP get file extension method PHP get file extension there are many ways, the following provides three methods, you can study, the specific will not explain, directly to the final positive...

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.