0

Hi I have this database_setup script witten in php. I have a project in a Linux debian machine. The top of the file looks like this:

database_setup.php

require_once("../classes/admin.php");
require_once("../classes/client.php");
require_once("../classes/database.php");
require_once("./config.php");

But when I run php database_setup.php I get the following error:

PHP Warning:  require_once(../classes/admin.php): failed to open stream: 
No such file or directory in /path/to/the/file/database_setup.php on line 6
PHP Fatal error:  require_once(): 
Failed opening required '../classes/admin.php'
(include_path='.:/usr/share/php:/usr/share/pear')
in /path/to/the/file/database_setup.php on line 6

When I run other code as usual with "normal" contact with the database (I mean the normal case being one where the files of models (as client and admin ) are in the same directory as the database) there is no fatal error or warning.

The config.php file contains the password and name of the db, and so on, which the database.php requires(_once). And the database_setup is in the same folder as the config file.

I really don't understand why the code works on my MAMP server on my mac but not on another system. Has it to do with apache2.conf or something else? Thanks.

patriques
  • 5,057
  • 8
  • 38
  • 48
  • 2
    Do yourself a favor and don't use relative file paths. You would be much better off by defining your web root folder (or some other directory and then doing requires/include using that constant). So something like `define(CLASS_DIR, '/path/to/classes/'); require_once(CLASS_DIR . 'admin.php');` If yo uneed to you can define one root directory for you entire app and define `CLASS_DIR` and such relative to that. – Mike Brant Jul 30 '13 at 20:24
  • One often runs into this error, and to quickly troubleshoot it, follow these steps : http://stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 17:03

1 Answers1

0

I think that this

require_once("../classes/admin.php");

Will be relative to where you are running the php command from.

Try changing directory to the folder above classes and then running php database_setup.php

You can change directory via the command line like this

cd /var/www/httpdocs/classes
Jake N
  • 10,535
  • 11
  • 66
  • 112