简体   繁体   中英

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:

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

and my file tree looks like this:

/home/name/public_html/

  • admin/dashboard.php
  • resources/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.

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.

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.

In this example my tree structure looks like this:

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

(main domain)/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):

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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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