php realpath 不产生可用的结果
<?php
$server = $_SERVER["SERVER_NAME"];
$pathpath = realpath("../../files/uploaded_file.jpg");
echo "You can link to the file using the following link... $server$pathpath";
?>
不幸的是,这会产生以下结果...
www.example.com/home/fhlinux123/g/example.com/user/htdocs/ninja/base/files/1.doc
而我所追求的结果如下...
www.example.com/files/uploaded_file.jpg
我不能假设文件夹“文件”将始终位于同一目录中。
<?php
$server = $_SERVER["SERVER_NAME"];
$pathpath = realpath("../../files/uploaded_file.jpg");
echo "You can link to the file using the following link... $server$pathpath";
?>
Unfortunately this produces the following...
www.example.com/home/fhlinux123/g/example.com/user/htdocs/ninja/base/files/1.doc
Whereas what I am after is as follows...
www.example.com/files/uploaded_file.jpg
I can't assume that the folder 'files' will always be in the same directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为 realpath 返回服务器框根的绝对路径,而不是 Web 服务器 htdocs 根的绝对路径。
您可以从 $_SERVER["DOCUMENT_ROOT"] 检索网络服务器 htdocs 根目录,然后从 realpath 返回的结果的开头将其删除
简单而肮脏的示例:
That's because realpath returns the absolute path from the server box root, not the webserver htdocs root.
You can retrieve the webserver htdocs root from $_SERVER["DOCUMENT_ROOT"], then strip that from the beginning of the result returned by realpath
Quick and dirty example: