PHP pathinfo被查询字符串中的url欺骗,任何解决方法?

Maj*_*our 2 php pathinfo

我正在开发一个小函数来获取一个url并根据它所在的位置返回一个相对路径.

如果url在查询字符串中包含路径,则pathinfo返回不正确的结果.以下代码证明了这一点:

$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>';
Run Code Online (Sandbox Code Playgroud)

该代码输出:

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
)
Run Code Online (Sandbox Code Playgroud)

这显然是错的.任何解决方法?

Pek*_*ica 8

任何解决方法?

是的,做得 ;)

$url = urlencode('http://localhost/demos/some/dir/afile.txt');
$p = 'http://localhost/demos/image_editor/dir_adjuster.php?u='.$url;
Run Code Online (Sandbox Code Playgroud)

对于URL,尤其是具有查询字符串的URL,parse_url()提取路径组件应该更可靠; 之后,运行pathinfo()它.