0

I'm reworking a PHP app that was written in PHP. I want to use relative paths. The app will be running under Windows/IIS and not Linux/Apache. This is the issue I am having.

If I set an absolute path, the code works:

include_once("C:/Users/me/Documents/Projects/Projects_App/app-qas.com/site/Library/API/database.inc.php");

If I change the code to the following I can navigate to the file by clicking on it in my dev environment but when I run it I get the following error:

include_once("../../site/Library/API/database.inc.php");

Error Message: Warning: include_once(../../site/Library/API/database.inc.php): failed to open stream: No such file or directory in C:\Users\me\Documents\Projects\Projects_App\app-qas.com\site\class\Posting.class.php on line 107

Warning: include_once(): Failed opening '../../site/Library/API/database.inc.php' for inclusion (include_path='.;C:\php\pear') in C:\Users\me\Documents\Projects\Projects_App\app-qas.com\site\class\Posting.class.php on line 107

Fatal error: Call to undefined function db_server_connect() in C:\Users\me\Documents\Projects\Projects_App\app-qas.com\site\class\Posting.class.php on line 109 HTTP/1.1 500 Internal Server Error Server: Microsoft Expression Development Server/4.0.0.0 Date: Thu, 10 Nov 2016 14:26:04 GMT Content-Length: 1959 Connection: Close

If I change it to the following I can't click on the link to get to the page BUT the app will run. It doesn't immediately throw an error (it does connect to the database) but then throws the following error:

include_once("../site/Library/API/database.inc.php");

ERROR Message Warning: include_once(../site/Library/API/database.inc.php): failed to open stream: No such file or directory in C:\Users\me\Documents\Projects\Projects_App\app-qas.com\site\class\Posting.class.php on line 107

Warning: include_once(): Failed opening '../site/Library/API/database.inc.php' for inclusion (include_path='.;C:\php\pear') in C:\Users\me\Documents\Projects\Projects_App\app-qas.com\site\class\Posting.class.php on line 107

Warning: include_once(../site/Library/API/database.inc.php): failed to open stream: No such file or directory in C:\Users\me\Documents\Projects\Projects_App\app-qas.com\site\class\Posting.class.php on line 107

Warning: include_once(): Failed opening '../site/Library/API/database.inc.php' for inclusion (include_path='.;C:\php\pear') in C:\Users\me\Documents\Projects\Projects_App\app-qas.com\site\class\Posting.class.php on line 107

What am I missing?

FlyFish
  • 491
  • 5
  • 22
  • Its not an answer as such, but I tend to have an `init.php` file that includes any other necessary files. In this file I'll put something like `define('LIBPATH', dirname(__FILE__))` which means from then on I can just `include(LIBPATH . '/OtherClass.php')`. This means you can get around the current working directory for relative includes. – Djave Nov 10 '16 at 14:52
  • You can use 'relative directories' in absolute file paths. Why? 'Relative Paths' are **always** based on the _current working directory (CWD)_ which can be changed by anything. If the CWD is not the same as the script directory then included files _will not be found_. So, if you want to include a file that is relative to the current script then use something like: `include __DIR__ .'/../../file_to_include.php`;. – Ryan Vincent Nov 10 '16 at 14:55

0 Answers0