PHP:从绝对URL获取绝对路径 [英] PHP: Get absolute path from absolute 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.
将其转换为文件路径后,您需要知道Web服务器的文档根目录在哪里.如果它与页面运行所在的服务器相同,则通常可以从 $_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屋!