PHP Get file name suffix example summary_PHP tutorial

WBOY
Release: 2016-07-13 10:44:57
Original
1048 people have browsed it

When uploading PHP files, we need to obtain the file name suffix and then make a simple file type judgment. The method of obtaining the file name suffix in PHP is very simple and there are many ways. Let me summarize it below.

1.basename()-returns the file name of the path

Please see the php code below:

The code is as follows Copy code
 代码如下 复制代码

$path = "/usr/www/html/index.php";
echo basename($path)."
";
//如果选择suffix则忽略扩展名
echo basename($path,".php");
?>

$path = "/usr/www/html/index.php";
echo basename($path)."
";
//If suffix is ​​selected, the extension is ignored
echo basename($path,".php");
?>
 代码如下 复制代码


//——FILE__返回文件完整路径
$dir = dirname(__FILE__);
echo $dir;
?>

Running result:

index.php
index

2.dirname()-returns the file path of the current script!

php code:

The code is as follows Copy code

//——FILE__ returns the full path of the file

$dir = dirname(__FILE__);
代码如下 复制代码
$path = "/usr/www/html/index.php";
$pathinfo = pathinfo($path);
echo "目录名称:$pathinfo[dirname]
";
echo "文件名称:$pathinfo[basename]
";
echo "扩展名:$pathinfo[extension]";
?>
echo $dir;

?>

Run result:
F:webzendexercise

3.pathinfo() returns an associative array containing path information.

Includes the following array elements: path name dirname, file name basename and extension name extension.
 代码如下 复制代码


$path = "./exercise/php.txt";
$realpath = realpath($path);
echo $realpath;
?>

Please see the simple code demonstration below:


The code is as follows Copy code
$path = "/usr/www/html/index.php";
$pathinfo = pathinfo($path);

echo "Directory name: $pathinfo[dirname]
";
 代码如下 复制代码
function extend_1($file_name)
{
$retval=“”;
$pt=strrpos($file_name, “.”);
if ($pt) $retval=substr($file_name, $pt+1, strlen($file_name) - $pt);
return ($retval);
}
echo "File name: $pathinfo[basename]
"; echo "Extension: $pathinfo[extension]"; ?>
Run result: Directory 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, pay attention to a little tip: the file path operators of different paths may be different. You can use "/" and "" under Windows, Under Linux, you can only use "/", so when developing, it is recommended to use "/", as I wrote the file path above! //Method 1:<🎜>
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 2
Php code

Php code
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);
代码如下 复制代码
function extend_3($file_name)
{
$extend =explode(“.” , $file_name);
$va=count($extend)-1;
return $extend[$va];
}
$extend = strtolower($extend["extension"]);

return $extend;
}

代码如下 复制代码
function getFileExt($file_name)
{
while($dot = strpos($file_name, “.”))
{
$file_name = substr($file_name, $dot+1);
}
return $file_name;
}

//Method 3
The code is as follows
Copy code

function extend_3($file_name)
{ $va=count($extend)-1; return $extend[$va]; } //Method 4 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.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633058.htmlTechArticleWhen uploading php files, we need to obtain the file name suffix and then make a simple file type judgment, and in php files There are many ways to obtain a name suffix. Let me summarize it below...
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template