0

I have been setting up a new web server to replace an aging one and I am having some problems with relative paths that work on the existing server. The existing server is Centos 6 with php 5.6, however the new server is Centos 7 with the same php version. I should also mention that both servers run WHM/cPanel with a single user (solo) licence.

I have several folders one level above the public_html folder that contain some include files and also some php files that are run from cron jobs. I put these here as there is never any need for them in the public_html folder.

// Example folder structure
-includes
-cjobs
-public_html
   -includes
   -css
   -js
   index.php

On the existing server, the following works ok on a file in the cjobs folder include "../public_html/includes/mail_include.php";, however on the new server it fails. I have ended up using include_once (dirname(__FILE__) . "/../includes/mail_include.php"); and also had to relocate the cjobs folder into the public_html folder. Note that full paths do work ok, so /home/username/includes/mail_include.php does always work, but I don't want to hard code the paths.

I feel that there may be some small configuration setting in php that I have not changed, but I just have not been able to figure out why I need to include the files this way. It means I need to modify thousands of files just so they will work on the new server which is proving to be a real pain.

Any advice is appreciated.

gjt211
  • 7
  • 4
  • Possible duplicate of [PHP include relative path](https://stackoverflow.com/questions/17407664/php-include-relative-path) – Mohammad Malek Mar 11 '19 at 06:22
  • If you do: `include __DIR__ . '/../includes/foo.php`, (the same as dirname(__FILE__)) it's not the same as "hard code the paths". What that does is to append the absolute path to the current file. This is recommended to do since using just a relative path can break things if you include this file in another file (since the include will then be from the top most PHP file and not from where the `include` is written). – M. Eriksson Mar 11 '19 at 06:32

0 Answers0