PHP How to get the file suffix name, _php tutorial

Source: Internet
Author: User

How PHP gets the suffix name of the file,


The suffix of a slice file, jpg or GIF, etc.

There are two ways of

One, if $img is the picture file name

$img =12345.gif; $img _ext = substr ($img, Strrpos ($img, '. '));

Second, use the PHP array, first read the file name string into the array

$ext 2=explode (".", $img);      $count =count ($ext 2);      $count 2= $count-1;      $file _name  = $urlstr. '. '. $ext 2[$count 2];//to get the suffix and redefine the new file name $file_name

Also attached are the methods summarized by other small partners:

1th method: Function Get_extension ($file) {substr (STRRCHR ($file, '. '), 1);} 2nd method: Function Get_extension ($file) {return substr ($file, Strrpos ($file, '. ') +1);} 3rd method: Function Get_extension ($file) {return End (Explode ('. ', $file));} 4th Method: Function Get_extension ($file) {$info = PathInfo ($file); return $info [' extension '];} 5th method: Function Get_extension ($file) {return pathinfo ($file, pathinfo_extension);}

The above several ways to look at a bit, seems to be OK, especially 1, 2 methods, I do not know PathInfo has a second parameter has been in use. But think about it, there are all sorts of problems with the first four methods. To get the file extension exactly right, you must be able to handle the following three special cases.

    1. No file name extension
    2. The path contains characters., such as/home/test.d/test.txt
    3. The path contains characters., but the file does not have an extension. such as/home/test.d/test

Obviously: 1, 2 cannot handle the third case, 3 cannot handle the 13th case correctly. 4 can be handled correctly, but a warning is issued when there is no extension. Only the 5th method is the most correct method. By the way, look at the PathInfo method. The official web site is described below:

$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 will return an array containing up to four elements, but there will not always be four, for example, without an extension, there will be no extension element, so the 4th method will not find a warning. But Phpinfo also supports the second parameter. You can pass a constant that specifies the data that returns a part:

Pathinfo_dirname-Directory Pathinfo_basename-file name (with extension) pathinfo_extension-Extension pathinfo_filename-file name (without extension, php>5.2)

The values for these four constants are 1, 2, 4, 8, and at first I thought I could specify multiple by or operation:
PathInfo ($file, Pathinfo_extension | Pathinfo_filename);
It turns out that this is not going to work, and it only returns the smallest of the few that are being performed or operational. This is the constant with the smallest bit 1 in the four flag bits.

http://www.bkjia.com/PHPjc/1133019.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133019.html techarticle PHP How to get the suffix name of the file, compared to the file suffix, jpg or gif, there are two methods one, if $img for the picture file name $img =12345.gif; $img _ext = substr ($img, Strrpos ($img,...

  • 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.