pathinfo() 函数以数组的形式返回关于文件路径的信息。
pathinfo是什么:
pathinfo不是nginx的功能,pathinfo是php的功能。
php中有两个pathinfo,一个是环境变量$_SERVER['PATH_INFO'];另一个是pathinfo函数,pathinfo() 函数以数组的形式返回文件路径的信息;。
nginx能做的只是对$_SERVER['PATH_INFO]值的设置。
具体参数说明如下:
参数 描述
path 必需。规定要检查的路径。
options 可选。规定要返回的数组元素。默认是 all。
可能的值:
PATHINFO_DIRNAME - 只返回 dirname
PATHINFO_BASENAME - 只返回 basename
PATHINFO_EXTENSION - 只返回 extension
PATHINFO_FILENAME - 只返回 filename
例如如下代码:
<?php
print_r(pathinfo("/testweb/test.txt"));
?>
则输出结果为:
Array
(
[dirname] => /testweb
[basename] => test.txt
[extension] => txt
[filename] => test
)
案例2如下代码:
<?php
print_r(pathinfo("/testweb/test.txt",PATHINFO_BASENAME));
?>
则输出结果为:
test.txt
具体的配置方法参考如下:
在Nginx.conf配置中,添加
location ~ 1.php(/|$)
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi.conf;
include pathinfo.conf;
}
在Nginx低版本中,是不支持PATHINFO的,但是可以通过在Nginx.conf中配置转发规则实现。
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
一个完整示例,请根据自己服务器加以修改
server {
listen 80;
server_name www.xxx.com *.xxx.com;
root "/home/www/wwwroot/xxx";
location / {
index index.html index.htm index.php;
#主要是这一段一定要确保存在
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
#结束
#autoindex on;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
php中的pathinfo()
pathinfo()函数可以对输入的路径进行判断,以数组的形式返回文件路径的信息,数组包含以下元素。
● [dirname] 路径的目录
● [basename] 带后缀 文件名
● [extension] 文件后缀
● [filename] 不带后缀文件名(需php5.2以上版本)
例如
<?php
print_r(pathinfo("/nginx/test.txt"));
?>
输出
1234567 Array( [dirname] => /nginx [basename] => test.txt [extension] => txt [filename] => test)
php中的$_SERVER['PATH_INFO']
PHP中的全局变量$_SERVER['PATH_INFO'],PATH_INFO是一个CGI 1.1的标准,经常用来做为传参载体。
被很多系统用来优化url路径格式,最著名的如THINKPHP框架。
对于下面这个网址:
http://www.test.cn/index.php/test/my.html?c=index&m=search
我们可以得到 $_SERVER['PATH_INFO'] = ‘/test/my.html’,而此时 $_SERVER['QUERY_STRING'] = 'c=index&m=search';
如果不借助高级方法,php中http://www.test.com/index.php?type=search 这样的URL很常见,大多数人可能会觉得不太美观。
而且对于搜索引擎也是非常不友好的(实际上有没有影响未知),因为现在的搜索引擎已经很智能了,可以收入带参数的后缀网页,不过大家出于整洁的考虑还是想希望能够重写URL。
- / ↩
版权属于: 三三世界-百宝箱
本文链接: http://33f.net/build/pathinfo_setup.html
本文最后更新于2021年11月14日 ,已超过1172天没有更新,若内容或图片失效,请留言反馈。
本文允许转载,但请在转载时请以超链接或其它形式标明文章出处
@Doug Shume it's ok for me , you can post here.
Saved as a favorite, I like your website!
If some one wishes to be updated with hottest technologies after that he must be visit this site and be up to date daily.
Heello would you mind sharing which blog platform you're using? I'm planning to start my own blog in the near future but I'm having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then moost blogs and I'm looking for something completely unique. P.S Apologies forr being off-topic butt I had to ask!
Thanks to my father who shared with me regarding this webpage, this website is genuinely amazing.
Hi, I have an overflow of customers that I'd like to send to you but I want to make sure you can handle more leads, let me know if you'd like me to send you more info.
zh.us.to 有效
kms.03k.org 有效
kms.chinancce.com
kms.shuax.com 有效
kms.dwhd.org 有效
kms.luody.info 有效
kms.digiboy.ir 有效
kms.lotro.cc 有效
www.zgbs.cc 有效
cy2617.jios.org 有效
@ 权限问题,试试sudo 再加命令。
你好提示Permission denied 怎么办啊