简体   繁体   English

php包含和其他文件的路径

[英]php Include and path to other files

When using php include function, I can easily use the same code on multiple pages, now the problem I ran into was, that my include file, outputs and image. 当使用php include函数时,我可以轻松地在多个页面上使用相同的代码,现在遇到的问题是我的include文件,输出和图像。

As the page where the include is called changes, so does the location of the files, that the include calls. 随着包含调用的页面发生变化,包含调用的文件位置也随之变化。 Get it? 得到它?

Example: I got a called "index.php". 示例:我有一个名为“ index.php”的文件。 I also got a file called "load.php". 我还得到了一个名为“ load.php”的文件。 and I got a file at last called "/folder/index2.php" 最后得到一个名为“ /folder/index2.php”的文件

index.php code: <?php include("load.php"); ?> index.php代码: <?php include("load.php"); ?> <?php include("load.php"); ?>

index2.php code: <?php include("../load.php"); ?> index2.php代码: <?php include("../load.php"); ?> <?php include("../load.php"); ?>

load.php code: <?php echo '<img src="image.png"/>'; ?> load.php代码: <?php echo '<img src="image.png"/>'; ?> <?php echo '<img src="image.png"/>'; ?>

When loading index.php, the image is shown perfectly as it ought to. 加载index.php时,该图像将正确显示。 When loading index2.php, which is located in another folder, but loads the "load.php" file successfully, the image is saying it cannot be found, which makes sense, since index2.php is calling the image, as if it was in the same folder, which it isn't. 当加载位于另一个文件夹中的index2.php但成功加载“ load.php”文件时,该映像表示找不到该映像,这很有意义,因为index2.php正在调用该映像,就好像它是在同一个文件夹中,不是。

So, is there any way to do this? 那么,有什么办法可以做到这一点? I'm trying to avoid using a full url, but if it's the only way I'll use that. 我试图避免使用完整的URL,但是如果这是我将使用的唯一方法。 I'm just looking for a better option. 我只是在寻找更好的选择。

First off, try specifying your include files using the document root: $_SERVER['DOCUMENT_ROOT'] ... this will always direct to the public root folder so it will be the same no matter where in the site you are - this is particularly useful when you start including files which themselves include / require other files ... in that scenario an included script which itself included sub-scripts relatively ( ../load.php | subfolder/load.php , etc) would never work from two different levels in the folder hierarchy. 首先,尝试使用文档根目录指定include文件: $_SERVER['DOCUMENT_ROOT'] ...这将始终定向到公共根文件夹,因此无论您在站点中的哪个位置,它都相同- 特别是当您开始包含本身include / require其他文件的文件时很有用 ……在这种情况下,包含脚本本身包含相对的子脚本( ../load.php | subfolder/load.php等)永远无法从两个工作文件夹层次结构中的不同级别。

<?php include($_SERVER['DOCUMENT_ROOT']."/load.php"); ?>

Then for resources that are being called in html , simple make them all relative to the public root as well: 然后, 对于在html中被调用的资源,也可以简单地使它们全部相对于公共根目录:

<?php echo '<img src="/image.png"/>'; ?>

Notice the / in front of the image src ? 注意图像src前面的/吗? That will always call the image no matter what folder the currently displayed page is in. 无论当前显示的页面位于哪个文件夹中,都将始终调用该图像。

Alternately, you could just specify resources absolutely (though this isn't ideal as if the site domain changes it will break the resource links): 或者,您可以完全指定资源(尽管这不太理想,因为站点域更改会破坏资源链接):

<?php echo '<img src="http://www.yoursite.com/image.png"/>'; ?>

Either way, no matter what script you're calling the include from it will always grab it from the correct location (no need to add ../ etc in your include calls) and images/scripts/css/etc will all work no matter what page they're pulled into 无论哪种方式,无论您从哪个脚本调用include,它都将始终从正确的位置抓取它(无需在include调用中添加../等),并且image / scripts / css / etc都可以工作他们被拉到什么页面

$_SERVER['DOCUMENT_ROOT'] indicates the current public root in include, as mentioned. 如上所述, $_SERVER['DOCUMENT_ROOT']指示include中的当前公共根目录。

The image source can be started with slash: <img src="/image.png"> to tell the browser to look at the domain root. 图像源可以以斜杠: <img src="/image.png">以告诉浏览器查看域根。 The mentioned src would tell the browser to check www.example.com/image.png, if placed in any document under www.example.com, or any of its sub-directories. 提及的src将告诉浏览器检查www.example.com/image.png(如果放在www.example.com下的任何文档中或其任何子目录中)。

Edit: Ben was faster at editing 编辑:Ben的编辑速度更快

i suggest make a config.php 我建议做一个config.php

use a variable for full path Eg $imgPath = 'www.host.com/path/images/'; 使用完整路径的变量,例如$imgPath = 'www.host.com/path/images/'; path/ $imgPath = 'www.host.com/path/images/';

include it at start of every page, 在每个页面的开头都包含它,

in load.php use <?php echo '<img src="'.$imgPath.'image.png"/>'; ?> 在load.php中使用<?php echo '<img src="'.$imgPath.'image.png"/>'; ?> <?php echo '<img src="'.$imgPath.'image.png"/>'; ?>

now when you include the load.php, you will have no errors :) 现在,当您包含load.php时,您将没有错误:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM