简体   繁体   English

PHP 包括路径子文件夹文件不包括在内

[英]PHP include path sub-folder files not included

I have a site where the PHP include path is /usr/share/php Within this path I have a sub-folder containing some utility files, eg /usr/share/php/utils.我有一个站点,其中 PHP 包含路径是 /usr/share/php 在这个路径中,我有一个包含一些实用程序文件的子文件夹,例如 /usr/share/php/utils。 my_session.php is one of these utility files. my_session.php 是这些实用程序文件之一。

My application calls我的应用程序调用

require ("my_session.php");

and this works even though the file is actually within the utils folder.即使该文件实际上位于 utils 文件夹中,这仍然有效。

I am trying to replicate this site in another installation and I am getting the error:我正在尝试在另一个安装中复制此站点,但出现错误:

Failed opening required 'my_session.php' (include_path='.:/usr/share/php)

My question is:我的问题是:

Should the php include path also include files in sub-folders in the include path? php 包含路径是否还应该包含包含路径中子文件夹中的文件? This appears to be the case on my original site and I don't know why the behaviour seems to be different on the second site.在我的原始网站上似乎就是这种情况,我不知道为什么第二个网站上的行为似乎有所不同。

According to PHP documentation when you try to include a file, only paths listed in the include_path directive are checked.根据 PHP 文档,当您尝试包含文件时,仅检查 include_path 指令中列出的路径。 PHP is not supposed to check their subfolders. PHP 不应该检查他们的子文件夹。

My guess would be that this fails because you are using a relative path for the require .我的猜测是这会失败,因为您使用的是相对路径require

Your include_path is defined as .:/usr/share/php .您的include_path定义为.:/usr/share/php That means only two folders will be checked when require('my_session.php') gets executed:这意味着当require('my_session.php')被执行时,只会检查两个文件夹:

  • the current path当前路径
  • the folder /usr/share/php文件夹/usr/share/php

I don't know your folder structure, so let's just imagine one:我不知道你的文件夹结构,所以让我们想象一个:

my_project
- app
-- index.php
- lib
-- my_session.php

Now, if my_project/app/index.php tries to require('my_session.php') this will fail , because the current folder at the time executing the require is my_project/app/ and there is no file entry of my_session.php relative to my_project/app/ (it's relative to my_project/lib/ instead).现在,如果my_project/app/index.php尝试require('my_session.php')这将失败,因为执行require时的当前文件夹是my_project/app/并且没有my_session.php相对文件条目到my_project/app/ (它是相对于my_project/lib/而不是)。

Long story short: Try to to use an absolute path instead of your relative one, eg长话短说:尝试使用绝对路径而不是相对路径,例如

require('/var/www/html/my_project/lib/my_session.php');

Edit: removed and its subfolders , which was wrong.编辑:删除及其子文件夹,这是错误的。 Too much __autoload in my brain^^我脑子里的__autoload太多了^^

Two solutions:两种解决方案:

Add /usr/share/php/utils to your include_path./usr/share/php/utils添加到您的 include_path。

or或者

Include your file with require ("utils/my_session.php");使用require ("utils/my_session.php");

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

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