PHP:从绝对 URL 获取绝对路径 [英] PHP: Get absolute path from absolute URL

查看:39
本文介绍了PHP:从绝对 URL 获取绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件的绝对路径,有没有办法获取文件的绝对路径

I have an absolute path of a file, Is there a way to get the file absolute path

http://domainname/rootfolder/filename.php

我想得到类似的东西

/home/domainname/public_html/foldername/filename.php

/home/domainname/public_html/foldername/filename.php

推荐答案

您可以使用parse_url 函数 获取 URL 的本地部分.

You can use the parse_url function to get the local part of the URL.

然后将其转换为文件路径需要您知道网络服务器的文档根目录在哪里.如果它与运行页面的服务器是同一台服务器,那么您通常可以从 获取该服务器$_SERVER['DOCUMENT_ROOT'].

Converting that into a file path then requires that you know where the document root of the webserver is. If it is the same server as the one the page is running on, then you can usually get that from $_SERVER['DOCUMENT_ROOT'].

它还取决于没有什么可以阻止 URL 不直接映射到文件系统(例如 mod_rewrite、mod_alias、通过 MVC 框架路由的 URI 等).例如,对于我目前正在处理的系统,如果您要点击 http://example.com/blog/2012/01/01/ 那么所涉及的文件将是 /home/user/recall/script/recall.psgi/home/user/recall/root/blog/day.tt 但 DocumentRoot 将是 /home/user/recall/htdocs/)

It also depends on there being nothing preventing the URL from not being a direct mapping onto a file system (such as mod_rewrite, mod_alias, URIs being routed through an MVC framework, etc). For example, for a system I'm working on at the moment, if you were to hit http://example.com/blog/2012/01/01/ then the files involved would be /home/user/recall/script/recall.psgi and /home/user/recall/root/blog/day.tt but the DocumentRoot would be /home/user/recall/htdocs/)

这篇关于PHP:从绝对 URL 获取绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆