In PHP file upload we need to get the file name suffix after the simple file type judgment, and in PHP, the file name suffix is very simple and there are many kinds, let me summarize below.
1.basename ()-Returns the file name of the path
Take a look at the following PHP code:
| The code is as follows |
Copy Code |
$path = "/usr/www/html/index.php"; Echo basename ($path). " "; If you select suffix, the extension is ignored Echo basename ($path, ". php"); ?> |
Operation Result:
index.php
Index
2.dirname ()-Returns the file path of the current script!
PHP Code:
| The code is as follows |
Copy Code |
--file__ return file Full path $dir = DirName (__file__); Echo $dir; ?>
|
Operation Result:
F:webzendexercise
3.pathinfo () returns an associative array containing information about path.
Includes the following array cells: path name dirname, file name basename, and extension name extension.
Take a look at the following simple code demo:
| The code is as follows |
Copy Code |
$path = "/usr/www/html/index.php"; $pathinfo = PathInfo ($path); echo "Catalog name: $pathinfo [dirname] "; echo "File name: $pathinfo [basename] "; echo "extension: $pathinfo [extension]"; ?> |
Operation Result:
Catalog Name:/usr/www/html
File name: index.php
Extension: PHP
4.realpath--Returns the normalized absolute path name
The PHP code is as follows:
| The code is as follows |
Copy Code |
$path = "./exercise/php.txt"; $realpath = Realpath ($path); Echo $realpath; ?>
|
Finally note a tip: Different paths of File path operators may, different, Windows can use "/" and "",
Linux can only use "/", so the development of the time, the recommendations are used "/", such as my file path above the wording!
Method One:
| The code is as follows |
Copy Code |
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
PHP code
| The code is as follows |
Copy Code |
function Extend_2 ($file _name) { $extend = PathInfo ($file _name); $extend = Strtolower ($extend ["extension"]); return $extend; } |
Method Three
PHP code
| The code is as follows |
Copy Code |
function Extend_3 ($file _name) { $extend =explode (".", $file _name); $va =count ($extend)-1; return $extend [$va]; } |
Method Four
PHP code
| The code is as follows |
Copy Code |
function Getfileext ($file _name) { while ($dot = Strpos ($file _name, ".")) { $file _name = substr ($file _name, $dot + 1); } return $file _name; }
|
?>
http://www.bkjia.com/PHPjc/633058.html www.bkjia.com true http://www.bkjia.com/PHPjc/633058.html techarticle in the php file upload we need to get the file name suffix after the simple file type judgment, and in PHP, the file name suffix is very simple and there are many kinds, let me summarize ...