sygamers
sygamers

Reputation: 57

PHP mysql Xampp failed to open stream: No such file or directory

I have this two files:

dbconfig.php

 <?php
 class database{
function __construct()
{
    $db_user="root";
    $db_pass="";
    try {
        $con = new PDO('mysql:host=localhost;dbname=hrm', $db_user, $db_pass);
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
       }
      catch(PDOException $e)
      {
            echo $e->getMessage();
        }
    }
}

and:

<?php

session_start();

 class user
  {

 private $db;

 function __construct(){

    require_once "../dbconfig.php";
     $this->db= new database();
  }

errors:

Warning: require_once(../dbconfig.php): failed to open stream: No such file or directory in C:\xampp\htdocs\hrm2\Class\class.user.php on line 12

Fatal error: require_once(): Failed opening required '../dbconfig.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\hrm2\Class\class.user.php on line 12

Path's: I have dbconfig on htdocs folder and i have the class.user.php file in Class folder. Actually i don't know how can the path be wrong because phpstorm is saying that it's right because of the recommended.

enter image description here

Upvotes: 2

Views: 21864

Answers (2)

solanki chirag
solanki chirag

Reputation: 1

Warning: require(database/db.php): Failed to open stream: No such file or directory in C:\xampp\htdocs\php project\resigistion form\insert.php on line 2

Upvotes: 0

aynber
aynber

Reputation: 23001

Using relative paths will get you into trouble. Try using an absolute path with $_SERVER['DOCUMENT_ROOT'], then dictate where the file is.

require_once $_SERVER['DOCUMENT_ROOT']."/dbconfig.php";

If it's outside of the document root, then use this to get one level up from the document root.

require_once $_SERVER['DOCUMENT_ROOT']."/../dbconfig.php";

Also make sure that the file has read permissions for your webserver's user.

Upvotes: 2

Related Questions