[英]PHP pathinfo gets fooled by url in query string, any workaround?
I am working on a small function to take in a url and return a relative path based on where it resides itself. 我正在开发一个小函数来获取一个url并根据它所在的位置返回一个相对路径。
If the url contains a path in the query string, pathinfo
returns incorrect results. 如果url在查询字符串中包含路径,则
pathinfo
返回不正确的结果。 This is demonstrated by the code below: 以下代码证明了这一点:
$p = 'http://localhost/demos/image_editor/dir_adjuster.php?u=http://localhost/demos/some/dir/afile.txt';
$my_path_info = pathinfo($p);
echo $p . '<br/><pre>';
print_r($my_path_info);
echo '</pre>';
That code outputs: 该代码输出:
http://localhost/demos/image_editor/dir_adjuster.php?u=http://localhost/demos/some/dir/afile.txt
Array
(
[dirname] => http://localhost/demos/image_editor/dir_adjuster.php?u=http://localhost/demos/some/dir
[basename] => afile.txt
[extension] => txt
[filename] => afile
)
Which obviously is wrong. 这显然是错的。 Any workaround?
任何解决方法?
Any workaround?
任何解决方法?
Yeah, doing it right ;) 是的,做得对 ;)
$url = urlencode('http://localhost/demos/some/dir/afile.txt');
$p = 'http://localhost/demos/image_editor/dir_adjuster.php?u='.$url;
and for URLs, especially those with query strings, parse_url()
should be more reliable to extract the path component; 对于URL,特别是那些带有查询字符串的URL,
parse_url()
应该更可靠地提取路径组件; After that, run the pathinfo()
on it. 之后,在其上运行
pathinfo()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.