PHP mysql Xampp failed to open stream: No such file or directory
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.
sygamers
Updated on June 14, 2022Comments
-
sygamers almost 2 years
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.