简体   繁体   English

require_once无法打开流:没有相对应的文件或目录-即使相对路径似乎正确(?)

[英]require_once failed to open stream: No such file or directory - even if relative path seems to be correct(?)

Well, I have this dashboard.php file: 好吧,我有这个dashboard.php文件:

<?php
require_once ('../resources/config.php');
?>

and my file tree looks like this: 我的文件树如下所示:

/home/name/public_html/ /家/名/的public_html /

  • admin/dashboard.php 管理员/ dashboard.php
  • resources/config.php 资源/ config.php文件

I really don't know what the heck is wrong with it, I'm using exact same include in the exact same directory for another php script, and everything runs great. 我真的不知道这到底是怎么回事,我在另一个PHP脚本的完全相同的目录中使用了完全相同的include,并且一切运行良好。

Any ideas? 有任何想法吗?

EDIT: I've noticed that in some cases relative path for files called from the same directory on the server is different, and now I know why. 编辑:我注意到在某些情况下,从服务器上同一目录调用的文件的相对路径是不同的,现在我知道为什么了。 I use index.php to control URI requests and have nicer URLs, I have a switch statement there which use include to call requested file, so if it includes a script which also has an include/require statement, the path changes. 我使用index.php来控制URI请求并拥有更好的URL,我在那里有一个switch语句,该语句使用include来调用所请求的文件,因此,如果其中包含一个脚本也具有include / require语句,则路径会改变。

I'm trying to fix that now, but I'm very very beginner, so any of your advice will be extremely helpful. 我现在正在尝试解决此问题,但是我是一个非常初学者,因此您的任何建议都将非常有帮助。

And great thanks to all of you, who answered so far! 非常感谢大家,到目前为止,他们都回答了!

I usually use the real path of the file like this: 我通常使用文件的真实路径,如下所示:

require_once(realpath(dirname(__FILE__)) . '/../../general_controler/database_manager.php');

This example is from my DataImporter abstract class using a DatabaseManager abstract class. 本示例来自我的DataImporter抽象类,使用DatabaseManager抽象类。

In this example my tree structure looks like this: 在此示例中,我的树结构如下所示:

(main domain)/PC_administration_interface/Controler/data_importer.php (主域)/PC_administration_interface/Controler/data_importer.php

(main domain)/general_controler/database_manager.php (主域)/general_controler/database_manager.php

In your case it would be something like this: 在您的情况下,将是这样的:

require_once (realpath(dirname(__FILE__)) . '/../resources/config.php');

Otherwise, starting your path with "/" to start at the root. 否则,以“ /”开头的路径从根开始。 In Xampp htdocs is the root folder for the site, in my case it could be something like this (as my general_controler folder is directly in the root): 在Xampp中,htdocs是网站的根文件夹,在我的情况下,它可能是这样的(因为我的general_controler文件夹直接在根目录中):

require_once('/general_controler/database_manager.php')

I guess for you it would be something like (supposing home is a sub folder of the root directory): 我想对您来说,它就像(假设home是根目录的子文件夹):

require_once('/home/name/public_html/resources/config.php');

The first solution is probably the best...anyway, you should give it a try. 第一个解决方案可能是最好的……无论如何,您应该尝试一下。

I hope it will solve your problem, 我希望它能解决您的问题,

Jonathan Parent-Lévesque from Montreal 蒙特利尔的JonathanParent-Lévesque

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

相关问题 require_once:无法打开流:没有这样的文件或目录? - require_once :failed to open stream: no such file or directory? Require_once不起作用(无法打开流:无此类文件或目录) - Require_once not working (failed to open stream: No such file or directory) require_once无法打开流:无此文件或目录 - require_once failed to open stream: No such file or directory PHP警告:require_once:无法打开流:在目录中没有这样的文件或目录 - PHP Warning: require_once: failed to open stream: No such file or directory in require_once:无法打开流:没有这样的文件或目录 - require_once :failed to open stream: no such file or directory require_once的问题无法打开流:没有这样的文件或目录 - trouble with require_once failed to open stream: No such file or directory Require_once 无法打开流:没有这样的文件或目录 - Require_once failed to open stream: No such file or directory PHP 警告:require_once 无法打开流:中没有这样的文件或目录 - PHP Warning: require_once failed to open stream: No such file or directory in require_once:无法打开流错误:没有这样的文件或目录 - require_once :failed to open stream error: no such file or directory PHP-require_once:无法打开流:没有此类文件或目录 - PHP - require_once: Failed to open stream: No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM