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.

  1. Make sure UserModel.php lies in the same folder as the one from which you are running the above code.

  2. 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.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!