2016-02-12 140 views
3

我的问题类似于PHP include file strategy needed。我有以下文件夹结构:PHP包含来自根文件夹中不同子目录的文件

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

在 “/根” 所有页面和 “/根/条” 目录有 “header.php文件” 和“页脚.php“文件,它们存储在”/root/includes“目录中。

页眉和页脚页面都包含存储在“root/img/”目录中的图像。

正如所建议的:

how it`s possible to include a file (include();) from the root folder to a files from different subfolders?

How to use a PHP includes across multiple directories/sub directories with relative paths

我试过**dirname**解决方案和$_SERVER['DOCUMENT_ROOT]解决方案,包括存储在目录和文章文件的页眉和页脚子目录。

不过,我遇到问题时,我尝试使用这两种方法(页眉和页脚文件中)链接图片:

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

浏览器无法显示图像,并引发以下错误:

locally: Not allowed to load local resource: file:///C:/root/img/image.jpg

on the server: GET http://host.com/home/public_html/root/img/image.jpg 500 (Internal Server Error)

我检查了网址,他们似乎是正确的。在搜索解决方案后,我发现Chrome出于安全原因阻止显示包含完整文件路径的文件。

我该如何解决这个问题?或者有没有其他方法将文件(包括图像)包含到存储在任何子目录中的文件中?

回答

1

@ Sandeep的答案是正确的部分。因为

$_SERVER['DOCUMENT_ROOT']; 

将再次返回到根的完全限定路径。

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

将返回图像,因为现在我不给它根据您的问题本地路径,但我正在运行的服务器的网址。

0

这可以帮助你

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

没有解决它。我遇到了同样的问题。不让我用file://协议打开文件。 –

0

将../img/image.jpg用于包含/ root/articles内的文件。

使用img/image。JPG用于包括内部/根

0

文件您包含脚本尝试设置includes_path使用set_include_path

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

然后,包括您的网页中的脚本,你可以使用:

include('script.php'); 

对于图像以前导斜杠的路径前缀/〜即:

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

它倾向于更容易使用根相对路径而不是目录相对路径,所以前导斜杠表示根目录中的文件夹。

相关问题