March 12, 2012
PHP require_once error
Question by Garrett
I am using CODA, MAMP and am working out of my HTdocs folder all locally. I am learning PHP and MVC structure and an coming across the following error when using require_once. I have read many of the other posts on this topic and no solution has worked.
The error I am receiving is:
Warning: require_once(UserModel.php): failed to open stream:
No such file or directory in - on line 13 Fatal error: require_once():
Failed opening required 'UserModel.php' (include_path='.:') in - on line 13
My file structure is:
database
----bdfdatabase.sql
index.php
UserModel.php
My code is as follows
<?php
require_once('UserModel.php');
$dsn = "mysql:host=127.0.0.1;port=8889;dbname=bdfdatabase";
$db_user= "root";
$db_pass= "root";
$model = new UserModel($dsn, $db_user, $db_pass);
$rows = $model->displayUsers();
foreach($rows as $row){
echo "<h3><a href='details.php'>";
echo "${row['user_name']}";
echo "</a></h3>";
}
?>
Any suggestions would be much appreciated.
Answer by Starx
Most probably, The problem is once again the Path.
-
Make sure
UserModel.php
lies in the same folder as the one from which you are running the above code. -
Make sure you have access to read that file. The file must be readable by owners, group, and others, to be included as such. Change the mode of the file to
0774
to confirm this.