Php obtains the file suffix (format file)
// Method 1:
Copy codeThe Code is as follows: <? Php
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
Copy codeThe Code is as follows: function extend_2 ($ file_name)
{
$ Extend = pathinfo ($ file_name );
$ Extend = strtolower ($ extend ["extension"]);
Return $ extend;
}
// Method 3
Copy codeThe Code is as follows: function extend_3 ($ file_name)
{
$ Extend = explode (".", $ file_name );
$ Va = count ($ extend)-1;
Return $ extend [$ va];
}
// Method 4
Copy codeThe Code is as follows: function getFileExt ($ file_name)
{
While ($ dot = strpos ($ file_name, ".")
{
$ File_name = substr ($ file_name, $ dot + 1 );
}
Return $ file_name;
}
?>
In addition:
PHP pathinfo () function
PHP Filesystem Function
Definition and usage
The pathinfo () function returns the file path information in an array.
Syntax
Pathinfo (path, options)
Parameters
Description
Path
Required. Specifies the path to be checked.
Process_sections
Optional. Specifies the array element to be returned. The default value is all.
Possible values:
PATHINFO_DIRNAME-returns only dirname
PATHINFO_BASENAME-only return basename
PATHINFO_EXTENSION-returns only extension
Description
Pathinfo () returns an associated array containing path information.
Includes the following array elements:
[Dirname]
[Basename]
[Extension]
Tips and comments
Note:If not all units are required, the pathinfo () function returns a string.
Example
Example 1
Copy codeThe Code is as follows: <? Phpprint_r (pathinfo ("/testweb/test.txt");?>
// Output:
// Array ([dirname] =>/testweb [basename] => test.txt [extension] => txt)
Example 2
Copy codeThe Code is as follows: <? Phpprint_r (pathinfo ("/testweb/test.txt", PATHINFO_BASENAME);?>
// Output:
// Test.txt