php script includes html in different directory

I have a form handler which is written in PHP and resides in a different directory than the html files. When the handler runs, it needs to include one of the html files. The html files have relative hrefs in them, which break because the page was served from the PHP directory, not the html directory.

For example, index.html contains

<link rel="stylesheet" type="text/css" href="css/site_global.css?4013920463"/>

These links are produced by Adobe Muse and expect that "css" is a subdirectory under the location of the html files and that the page was served from the html directory. Again, since I'm serving the page from the PHP directory, the relative links break.

Short of putting in absolute paths for the hrefs, is there any other technique I should consider? I really don't want to put in absolute paths because they will break for other reasons.

Ideally, I'd like to use some sort of method that allows me to set the "working path" in the browser - so that I can tell it to fetch hrefs from the right place.


Relative paths in a browser are computed based on the current page path (see here). If you are looking at http://foo.bar/one/page.html , the site_global.css path will be http://foo.bar/one/css/site_global.css .

If I understood your question, you can use the element to set a base URL for all the relative links in the page.

See here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base


try $_SERVER['DOCUMENT_ROOT'], gives the path to your base directory with current working dir

or

try echo realpath(dirname( FILE ));

链接地址: http://www.djcxy.com/p/30540.html

上一篇: 图像路径正确,为本地主机

下一篇: PHP脚本包含不同目录中的HTML