PHP 包括调用其他文件
我意识到我在 PHP 文件中一遍又一遍地使用了某个特定函数,因此我从所有文件中取出该函数并将其放入一个单独的 PHP 文件中,并将其包含在我需要的地方。
现在,当我更新该函数时,使用它的所有文件都会更新。伟大的。
现在只有一个问题,函数本身使用相对 URL,当您包含 PHP 时,文件是相对于调用 PHP 的,而不是包含的 PHP 目录。我知道快速而肮脏的答案是使用绝对链接,但是我怎样才能让包含的 PHP 引用它本身而不是调用它的 PHP。
示例:
index.php 包含一个名为 create_table.php 的文件..... create_table.php 将 CSS 和 javascript 回显到 index.php。 CSS和javascript与create_table.php位于同一目录中,但是当使用src =“javascript.js”时,它想要找到index.php所在的javascript.js,而不是create_table.php所在的位置。
编辑:结构如何工作
/includes/create_table.php
/includes/style.css
/includes/javascript.js
/test/dir/1/2/3/index.php
因此,当我尝试使用 src="javascript.js" 而不是寻找 /includes/javascript.js 时,它会尝试找到/test/dir/1/2/3/javascript.js
I realized I used a particular function over and over again in my PHP files, so I took the function out of all of them and put it in a seperate PHP file and included it where I need it.
Now when I make updates to the function, all the files that use it get updated. Great.
Now there is just one problem, the function itself uses relative URLs, and when you include the PHP the files are relative to the calling PHP, not the included's PHP directory. I know the quick and dirty answer is to use absolute links, but how can I get the included PHP to reference things to ITSELF and not to the PHP that called it.
Example:
index.php includes a file called create_table.php..... create_table.php echoes CSS and javascript to index.php. The CSS and javascript are in the same directory as create_table.php, but when using src="javascript.js" it wants to find the javascript.js where index.php is located, not where create_table.php is located.
Edit: how the structure works
/includes/create_table.php
/includes/style.css
/includes/javascript.js
/test/dir/1/2/3/index.php
So when I attempt to use src="javascript.js" instead of looking for /includes/javascript.js it tries to find /test/dir/1/2/3/javascript.js
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用绝对链接并不是“又快又脏”,而是绝对必要的事情。
而且您的问题与 PHP 几乎没有共同点。这是 HTML 问题。在读取 HTML 代码中的链接后,不是 PHP 调用您的 CSS 文件,而是 浏览器 调用了 CSS 文件。
绝对路径的唯一目的是解决您的问题 - 从任何位置查找特定文件。
当然它应该只是路径,而不是 URI。所有 HTML 链接都可以(并且应该)缩短为路径部分。
Bingo!
因此,只需告诉它寻找这个地址,一个绝对的地址 -
/includes/javascript.js
Use of absolute links is not "quick and dirty" but absolute necessary thing.
And your problem has very little in common with PHP. It is HTML problem. It is not PHP calling your CSS file, but browser, after reading the link from HTML code.
The only purpose of absolute path is to solve your problem - to find a particular file from any location.
Of course it should be only path, not URI. All HTML links can be (and should be) shortened to path parts.
Bingo!
Therefore, just tell it to look for this very address, an absolute one -
/includes/javascript.js
可能有一种更好的方法来处理您想要执行的任何操作,而不是从 PHP 函数中动态请求文件。
至少,绝对路径是关键,并且通常是从根开始的绝对路径,例如:
require_once(“/lib/fileOne.php”);
不是: require_once("lib/fileOne.php");
There is likely a better way to handle whatever you're trying to do rather than dynamically require files from within PHP functions.
At the very least, absolute paths is key, and generally absolute from root, like:
require_once("/lib/fileOne.php");
not: require_once("lib/fileOne.php");
我认为你最终必须使用绝对路径。问题是如何在不进行硬编码的情况下获取这些绝对路径...
您可以使用魔术常量 __FILE__ 来获取当前脚本的绝对路径(即create_table.php),然后执行某些操作像:
获取您知道的相对于当前脚本的文件的绝对路径。 (即向上两个目录并向下进入 css/)
或者您可以以另一种方式开始并使用
$_SERVER['DOCUMENT_ROOT']
。假设您知道与您的网络根目录相关的所有路径。这将是我的首选方式。编辑:我认为你几乎可以忽略上面的内容!我假设(在编辑之前)您实际上是在服务器端读取 CSS 和 JavaScript 文件并将内容添加到 index.php - 我想不是!
重新回答...由于这个问题,我绝不会使用客户端CSS和JavaScript的相对
路径URL。有些人总是使用绝对 URL,我个人更喜欢根相对 URL。如果您在服务器端生成此 URL,则没有多大关系。根相对 URL:
/includes/style.css
您认为这有什么问题?我会说这是首选。
I think you're going to have to ultimately use absolute paths. The question is how to get those absolute paths without hard coding it...
You can use the magic constant
__FILE__
to get the absolute path of the current script (ie. create_table.php) and then do something like:To get the absolute path of a file you know as being relative to the current script. (ie. up two directories and down into css/)
Or you could start the other way and use
$_SERVER['DOCUMENT_ROOT']
. Assuming you know all the paths relative to your webroot. This would be my preferred way.EDIT: I think you can pretty much ignore the above! I was assuming (before your edit) that you were literally reading your CSS and JavaScript files server-side and adding the contents of to index.php - I guess not!
To re-answer... I would never use relative
pathsURLs for client-side CSS and JavaScript because of exactly this problem. Some people always use absolute URLs, personally I prefer root-relative URLs. If you are generating this URL server-side, it doesn't much matter.Root-relative URL:
/includes/style.css
What do you perceive is wrong with this? I would have said this was preferred.
所以你是说有一个文件总是需要的。在此文件中,有一个函数将
__FILE__
与SCRIPT_FILENAME
进行比较。例如,您知道includes/create_table.php
到根目录的路径是../
,因此它的文件夹差异为1
code>,如果路径是includes/foo/create_table.php
,则 root 的路径将为../../
,因此相差2
等...现在只需在代码中添加
src="$path.includes/style.css"
So you are saying there is a file that always get required. In this file, have a function that compares
__FILE__
toSCRIPT_FILENAME
. So, for example, you knowincludes/create_table.php
's path to your root directory is../
so it has a folder difference of1
, if the path was wasincludes/foo/create_table.php
, the path to root would be../../
so a difference of2
, etc...Now just have in your code
src="$path.includes/style.css"