简体   繁体   English

PHP require和require_once在相对路径上失败

[英]PHP require and require_once fails on relative path

We have moved our hosting from server which hosted PHP 5.6 to a new server which hosts PHP 7.0. 我们已经将托管从托管PHP 5.6服务器迁移到托管PHP 7.0.的新服务器PHP 7.0. After the migration, we've noticed the following issue: 迁移后,我们注意到了以下问题:

PHP Warning: include_once(../classes/ClassA.php): failed to open stream: No such file or directory in /var/www/html/addons/addon.php php on line 37 PHP警告:include_once(../ classes / ClassA.php):无法打开流:在第37行的/var/www/html/addons/addon.php php中没有此类文件或目录

I understand it should work with 我知道它应该与

include_once __DIR__."/../classes/ClassA.php";

But I don't understand why it worked on previous server and not on new one. 但是我不明白为什么它可以在以前的服务器上工作,而不是在新服务器上工作。 Are there any php.ini changes that were allowing it to work without __DIR __ prefix? 是否有任何php.ini更改都允许它在没有__DIR __前缀的情况下工作?

Use $_SERVER['DOCUMENT_ROOT'] instead of _DIR_ . 使用$ _SERVER ['DOCUMENT_ROOT']而不是_DIR_ Remove the path components ../../ and point absolutely to a file using the $_SERVER variable. 删除路径组件../../并使用$ _SERVER变量绝对指向文件。

Or 要么

Create a BASEPATH constant with value of absolute path to your site's root directory. 创建一个BASEPATH常量,其值为站点根目录的绝对路径。 Which you can use to absolutely point to an include file. 您可以用来绝对指向一个包含文件。 If you move to another server, just update the value of that variable if needed. 如果移至另一台服务器,则仅在需要时更新该变量的值。

try realpath() function..! 尝试realpath()函数..!

include_once realpath(__DIR__."/../classes/ClassA.php");

The realpath() function returns the absolute pathname. realpath()函数返回绝对路径名。

This function removes all symbolic links (like /./ , ' /../ ' and extra ' / ') and returns the absolute pathname. 这个函数删除所有符号链接(如/./ ,“ /../ ”以及多余的“ / ”),并返回绝对路径。

This function returns FALSE on failure. 失败时此函数返回FALSE。

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

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