php返回文件路径
1 basename — 返回路径中的文件名部分
如果文件名为test.php,路径为www/hj/test.php
echo basename($_SERVER['PHP_SELF']);
输出为:
test.php
2:直接使用 $_SERVER['PHP_SELF']
echo $_SERVER['PHP_SELF'];
输出为:
/hj/test.php
3:dirname 输出文件的根目录
echo dirname($_SERVER['PHP_SELF']);
输出结果为:
/hj
4:pathinfo 解析完整路径
<?php $path_parts = pathinfo ( '/www/htdocs/inc/lib.inc.php' ); echo $path_parts [ 'dirname' ], "\n" ; echo $path_parts [ 'basename' ], "\n" ; echo $path_parts [ 'extension' ], "\n" ; echo $path_parts [ 'filename' ], "\n" ; // since PHP 5.2.0 ?>
以上例程会输出:
/www/htdocs/inc lib.inc.php php lib.inc
5:parse_url — 解析 URL,返回其组成部分
例子1:
<?php $url = 'http://username:password@hostname/path?arg=value#anchor' ; print_r ( parse_url ( $url )); echo parse_url ( $url , PHP_URL_PATH ); ?>
输出为:
Array ( [scheme] => http [host] => hostname [user] => username [pass] => password [path] => /path [query] => arg=value [fragment] => anchor )
例子2:
<?php $url = '//www.example.com/path?googleguy=googley' ; // 在 5.4.7 之前这会输出路径 "//www.example.com/path" var_dump ( parse_url ( $url )); ?>
输出为:
array(3) { ["host"]=> string(15) "www.example.com" ["path"]=> string(5) "/path" ["query"]=> string(17) "googleguy=googley" }