简体   繁体   English

php包含根目录以外的目录中的文件

[英]php include files from directories other than root directory

I've got a global template page, /layout_header.php as well as some other file stored in /aFolder/index.php . 我有一个全局模板页面,/ /layout_header.php以及存储在/aFolder/index.php其他一些文件。

In /layout_header.php , I have: /layout_header.php ,我有:

<?php
    // Reference to scripts and other files
    echo "<img src='img/lollipop.png'/>";
    echo "<script src='js/move.js'/>";
    echo "<a href='aFolder/movement.php'>Click here!</a>";
?>

In /aFolder/index.php , I have: /aFolder/index.php ,我有:

<?php
    include "../layout_header.php";
?>

Now, /aFolder/index.php has no problems calling the /layout_header.php file. 现在,/ /aFolder/index.php调用/layout_header.php文件没有问题。 Unfortunately, because the directory is now /aFolder , I face the following problems: 不幸的是,因为目录现在是/aFolder ,我面临以下问题:

  • Unable to view the image, since image is now linked as /aFolder/img/lollipop.png (which does not exist) 无法查看图像,因为图像现在链接为/aFolder/img/lollipop.png (不存在)
  • Unable to execute script because of reason listed above. 由于上面列出的原因,无法执行脚本。
  • Invalid URL because of reason listed above. 由于上面列出的原因,网址无效。

Many of my scripts and css files are using relative path. 我的许多脚本和css文件都使用相对路径。 In other words, if index.php was placed in the root folder, everything works fine. 换句话说,如果index.php放在根文件夹中,一切正常。

How should I go about solving the problem of files in subdirectories? 我该如何解决子目录中的文件问题?

You can see that in all PHP frameworks (at least professionals) a variable/constant as APPPATH, BASEPATH etc. So you need to define your similar variable/constant in your project and use it as needed. 您可以在所有PHP框架(至少是专业人员)中看到变量/常量为APPPATH,BASEPATH等。因此,您需要在项目中定义类似的变量/常量并根据需要使用它。

<?php
// a.php: assuming this included everywhere at very first line 
// and located in root directory
// preferable, define a constant instead of variable, cos it 
// may used in functions directly without "global $ROOT";

// for "include"
define('ROOT', __DIR__); // for PHP >= 5.3
define('ROOT', realpath(dirname(__FILE__)));  // for PHP < 5.3
// for "src, href" etc
define('HTTP', $_SERVER['SERVER_NAME']); // for local development support i.e foo.com.local

// includes
include ROOT .'/layout_header.php';
include ROOT .'/classes/mailer.php';
include ROOT .'/foo/bar/baz.php';
...

// src,href
printf('<script src="%s/js/move.js" />', HTTP);
printf('<img src="%s/img/lollipop.png" />', HTTP);

print '<a href="'. HTTP .'/aFolder/movement.php">Click here!</a>';
...
?>

Always use $_SERVER['DOCUMENT_ROOT'].'path/to/file' for including php files when it's relative. 总是使用$ _SERVER ['DOCUMENT_ROOT']。'path / to / file'来包含php文件的相对位置。

And for images and script files and css files use yourdomain.com/path/to/file.extension 对于图像和脚本文件以及css文件,请使用yourdomain.com/path/to/file.extension

Have you tried using complete root instead of relative. 您是否尝试过使用完全root而不是relative。

define('WEB_ROOT', 'http://yourdomain.com/');  // localhost path etc


echo "<img src=".WEB_ROOT."img/lollipop.png'/>";
echo "<script src=".WEB_ROOT."'js/move.js'/>";
echo "<a href='".WEB_ROOT."aFolder/movement.php'>Click here!</a>";

There is many solutions that you can do: 你可以做很多解决方案:

  • Go to your config file to change roots of your project. 转到配置文件以更改项目的根目录。

  • Leave the index.php into the folder aFolder/index.php and make a redirection from it to an other file 将index.php保留在文件夹aFolder / index.php中,并将其重定向到另一个文件

  • Change your htacess file to make a redirection 更改您的htacess文件以进行重定向

  • Change all path by taking url from the server. 通过从服务器获取URL来更改所有路径。

I will took the second one (duplicate the index and put it with /layout_header.php) Remove the content from aFolder/index.php and add to it a redirection to the other index file: 我将采用第二个(复制索引并将其与/layout_header.php一起使用)从aFolder / index.php中删除内容并向其添加重定向到另一个索引文件:

<?php

   header( 'Location: ../index.php' ) ;

?>

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

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