Home  >  Article  >  Backend Development  >  Several effective ways to get file extensions in php_PHP Tutorial

Several effective ways to get file extensions in php_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:13:231239browse

There are many advertisements in php to obtain the file extension name. For example, $extend["extension"] after the result of the php function pathinfo is a good method. The following are private customizations for friends who need to know more. You can refer to it.

Use pathinfo function

The code is as follows
 代码如下 复制代码
function extend_2($file_name)
{
$extend = pathinfo($file_name);
$extend = strtolower($extend["extension"]);
return $extend;
}
Copy code


function extend_2($file_name) {

$extend = pathinfo($file_name);
$extend = strtolower($extend["extension"]);

return $extend;

}

Definition and usage
The pathinfo() function returns file path information in the form of an array.

Grammar

pathinfo(path,options)

pathinfo() returns an associative array containing path information.
 代码如下 复制代码

function getextension($filename)
{
  return substr(strrchr($filename,”.”),1);
}

Includes the following array elements:

 代码如下 复制代码

function GetFiletype($filename){
 $filer=explode(".",$filename);
 $count=count($filer)-1;
 return strtolower(".".$filer[$count]);
}

[dirname]

[basename]

[extension]
 代码如下 复制代码
 
function getfile($filestr){
  // 用点号分隔文件名到数组
    $get = explode('.',$filestr); 
  //把上面数组倒序
    $get = array_reverse($get); 
  //返回倒序数组的第一个值
    return $get[0];
}
$filename =  getfile('nowamagic.doc');
echo $filename;


//Get the file extension

The code is as follows Copy code

function getextension($filename)
{
Return substr(strrchr($filename,”.”),1);

}

Use segmentation method

The code is as follows Copy code
function GetFiletype($filename){ $filer=explode(".",$filename); $count=count($filer)-1; return strtolower(".".$filer[$count]); } Use the segmentation method to bring the system’s own function array_reverse
The code is as follows Copy code
function getfile($filestr){ // Use dots to separate file names into arrays $get = explode('.',$filestr); //Reverse the above array $get = array_reverse($get); //Return the first value of the reversed array Return $get[0]; } $filename = getfile('nowamagic.doc'); echo $filename; Detailed explanation of array_reverse (PHP 4, PHP 5) array_reverse - Returns an array with its contents reversed Description array_reverse(array$array[, boolean$preserve_keys=false]) A command that takes an input array and returns a new array with the contents reversed. http://www.bkjia.com/PHPjc/629188.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629188.htmlTechArticleTo get the file suffix name, there are many advertisements in php, such as $extend after the result of php function pathinfo ["extension"] is a good method. The following are private customizations that need to be understood...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn