1

So I'm just learning PHP. I am trying to run my login script that I built, and I'm getting this error.

Warning: require_once(../../configs/db_config.php): failed to open stream: No such file or directory in C:\xampp\htdocs\test\assets\includes\logininc.php on line 4

Fatal error: require_once(): Failed opening required '../../configs/db_config.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\test\assets\includes\logininc.php on line 4

Is this because this path is incorrect?

This code is on page.

require_once "../../configs/db_config.php";
require_once "../../includes/memberfunc.php";

db_config sits in a folder below the directory of the page that I ran (logininc.php)

Update : Here's the code in question. It sits in the base directory next to index.php

<form id="login-form" method="post" action="assets/includes/logininc.php"> <fieldset> 
  <legend>Login </legend> 
  <p>Please enter your username and password to access the administrator's panel</p>

   <label for="username"> <input type="text" name="username" id="username" />Username: </label> <label for="password"> <input type="password" name="password" id="password" />Password: </label> <label for="submit"> <input type="submit" name="submit" id="submit" value="Login" /> </label> </fieldset> </form>
1
  • Goes like this. Index > assets. In assets there are 2 folders, includes, configs. In includes sits logininc.php.
    – John Smith
    Jul 22, 2013 at 20:30

6 Answers 6

0

Use DIR to specificy a path relative to the current file:

require_once __DIR__ "/../../configs/db_config.php";

Otherwise the path is relative to the directory that contains the script where execution started.

0

Given the (made up) path /home/sites/example.com/html/logininc.php, then

../../configs/db_config.php would evaluate to /home/sites/configs/db_config.php
and
../../includes/memberfunc.php would evalate to /home/sites/includes/memberfunc.php.

If, as you say, db_config sits in a folder BELOW the directory where logininc.php sits, then you'd want something more like

require('subdir/logininc.php');
1
  • No luck it seems. I tried 'require('configs/db_config.php'); require('includes/memberfunc');' This is my file structure. test/assets/includes/logininc.php and test/assets/configs/db_connect.php? Ugh so frustrating. Maybe I need to put logininc.php into my base directory as login.php makes the call to logininc.php which is located well above it?
    – John Smith
    Jul 22, 2013 at 21:22
0

The error Failed to open stream and No such file or directory mean that you are trying to require or include a file that does not exist in the specified directory.

This can happen for a couple of reasons:

  • The file you are trying to require does not exist

  • You are specifying a wrong path

I hope this helps you!

0

Try this:

require_once dirname(dirname(dirname(__FILE__))) . "/configs/db_config.php";

Adjust the number of dirname according to the directory level. To debug the path:

echo dirname(dirname(dirname(__FILE__))) . "/configs/db_config.php";
3
  • So does dirname make it look down level. So if I was at test1/test2/test3 dirname the first time would be test1, then dirname again test2 and so on?
    – John Smith
    Jul 22, 2013 at 21:26
  • Reversed, if you are in file 1/2/3/file.php, first dirname is 1/2/3, second dirname is 1/2, third dirname is 3. Jul 22, 2013 at 21:31
  • ...where the first dirname is the innermost and the last dirname is the outermost. Jul 23, 2013 at 5:16
0

for dubugging, or finding exactly where require_once expects your file to be located, use realpath(), which will provide you absolute path. Then, you can decide how to navigate from there, meaning to add/remove the number of ..s

echo realpath("../../configs/db_config.php");

0

Others pointed out, your path is wrong.

.. means you are going back a directory from the current

You mention that your are a beginner , I advice you to always specify the full path until you are comfortable

Edit: if both configs and includes are in assets then they should be like this:

require_once("/test/assets/configs/db_config.php"); 
require_once("/test/assets/includes/memberfunc.php");

This way you will never get confused

1
  • Hi this makes sense, thanks for that but I'm STILL getting this error. Can you take another look at my original question because I have updated it with some possibly relevant code that I mistakenly omitted...?
    – John Smith
    Jul 22, 2013 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.