简体   繁体   English

PHP包含根文件夹中不同子目录的文件

[英]PHP include files from different subdirectories within the root folder

My question is similar to PHP include file strategy needed . 我的问题类似于PHP包含所需的文件策略 I have the following folder structure: 我有以下文件夹结构:

 /root/pages.php /root/articles/pages.php /root/includes/include_files.php /root/img/images.jpg 

All pages in the " /root " and " /root/articles " directories have a "header.php" and "footer.php" file included within them, which are stored within the " /root/includes " directory. / root ”和“ / root / articles ”目录中的所有页面都包含“header.php”和“footer.php”文件,这些文件存储在“ / root / includes ”目录中。

The header and footer pages both contain images stored in the " root/img/ " directory. 页眉和页脚页面都包含存储在“ root / img / ”目录中的图像。

As suggested by: 如下所示:

how it`s possible to include a file (include();) from the root folder to a files from different subfolders? 如何将根文件夹中的文件(include();)包含到来自不同子文件夹的文件中?

How to use a PHP includes across multiple directories/sub directories with relative paths 如何使用PHP包括具有相对路径的多个目录/子目录

I tried the **dirname** solution and the $_SERVER['DOCUMENT_ROOT] solution to include the header and footer in the files stored in the root directory and articles subdirectory. 我尝试了**dirname**解决方案和$_SERVER['DOCUMENT_ROOT]解决方案,将页眉和页脚包含在目录和articles子目录中的文件中。

However, I run into issues when I try and use either method (within the header and footer files) to link images: 但是,当我尝试使用任一方法(在页眉和页脚文件中)链接图像时,我遇到了问题:

<img src=" <?php echo dirname(__FILE__). '/img/image.jpg';?>">

Chrome fails to display the images and throws the following error: Chrome无法显示图片并引发以下错误:

locally: Not allowed to load local resource: file:///C:/root/img/image.jpg local 不允许加载本地资源:file:/// C:/root/img/image.jpg

on the server: GET http://host.com/home/public_html/root/img/image.jpg 500 (Internal Server Error) 在服务器上: GET http://host.com/home/public_html/root/img/image.jpg 500(内部服务器错误)

I have checked the URL's and they appear to be correct. 我检查了网址,看起来是正确的。 Having googled for solutions, I found that chrome prevents the display of files that contain it's complete file path for security reasons. 搜索完解决方案后,我发现chrome出于安全原因阻止显示包含完整文件路径的文件。

How do I get around this? 我该如何解决这个问题? or is there an alternative way to include files (including images) into files stored in any subdirectory? 或者是否有另一种方法将文件(包括图像)包含在存储在任何子目录中的文件中?

@sandeep's answer is partially right. @ sandeep的回答是部分正确的。 because 因为

$_SERVER['DOCUMENT_ROOT'];

will again give back fully qualified path to the root. 将再次返回到root的完全限定路径。

<img src=" <?php echo 'http://localhost/favicon.ico';?>"/>

will return image back because now I am not giving it local path as per your problem but url of the server I am running. 将返回图像,因为现在我没有根据您的问题给它本地路径,但我正在运行的服务器的URL。

for your included scripts try setting the includes_path using set_include_path 您包含脚本尝试设置includes_path使用set_include_path

set_include_path( $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR );

Then, to include a script within your pages you can use: 然后,要在您的页面中包含脚本,您可以使用:

include( 'script.php' );

For the images, prefix the path with a leading slash / ~ ie: 对于图像,在路径前面加上前导斜杠/ 〜,即:

<img src='/img/image1.jpg' />

It tends to be easier to use root relative paths rather than directory relative paths so prefixing with the leading slash means a folder within the root. 它往往更容易使用根相对路径而不是目录相对路径,因此前缀为前导斜杠表示根目录中的文件夹。

This may be help you 这可能会对你有所帮助

$url = $_SERVER['DOCUMENT_ROOT'].'/img/image.jpg';
<img src="<?=$url?>">

Use ../img/image.jpg for including in files inside /root/articles. 使用../img/image.jpg包含/ root / articles中的文件。

Use img/image.jpg for including in files inside /root 使用img / image.jpg包含在/ root中的文件中

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

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