PHP How to get file extensions _php Tutorial

Source: Internet
Author: User
How PHP Gets the file name extension

PHP get file extension There are many ways, the following provides three of the methods, you can study the next, specifically do not explain, directly to the final correct answer

echo pathinfo ('/www/htdocs/your_image.jpg ', pathinfo_extension);
Error wording:
You might write that.

function get_file_extension ($file _name) {
Return substr (STRRCHR ($file _name, '. '), 1);
}
or write like this

function File_extension ($filename) {
Return End (Explode (".", $filename));
}
By the way, what do you think pathinfo doing?

$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 will output

/www/htdocs
Your_image.jpg
Jpg
Your_image


, the code is as follows:
Method One
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 Two
function Extend_2 ($file _name)
{
$extend = PathInfo ($file _name);
$extend = Strtolower ($extend ["extension"]);
return $extend;
}
Method Three
function Extend_3 ($file _name)
{
$extend =explode (".", $file _name);
$va =count ($extend)-1;
return $extend [$va];
}
?>

http://www.bkjia.com/PHPjc/632277.html www.bkjia.com true http://www.bkjia.com/PHPjc/632277.html techarticle php Get file extension method php get file extension There are many ways, the following provides three of the methods, you can study the next, specifically do 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.