ajoo Posted February 24, 2017 Share Posted February 24, 2017 Hi all ! My program works just fine on xampp but the exact same code ported over to VM running ubuntu12.04 / apache 2.4/ php 5.6 and mysql 5.5 gives errors. These errors occur when the program executes the require_once / include commands with paths. More specifically the following works on xampp : require_once("/path/to/file.php") but on the VM this gives a "no such file or folder present" error. On the VM the following works : require_once("path/to/file.php") I wonder if this is a know issue and that on a linux server the paths are defined like this or do they change from one linux strain to another? I have not decided on a web-host just yet and so I am just wondering if I should make changes to the paths. Thanks all ! Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 24, 2017 Share Posted February 24, 2017 Have you tried making the path root relative using $_SERVER['DOCUMENT_ROOT']? Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted February 24, 2017 Solution Share Posted February 24, 2017 Either the path is correct, or it isn't. This has nothing to do with VMs, the Linux distribution or whatever. If you're trying to use absolute Windows paths on a Linux machine, that of course won't work, because the two operating systems have entirely different filesystem layouts. That's why I suggested to create the paths dynamically based on an environment variable which contains the application directory: <?php require_once getenv('MY_APP_BASE_DIR').'/path/to/file/within/base/directory'; $_SERVER['DOCUMENT_ROOT'] is usually the wrong answer, because that implies your entire application resides in the document root -- which it shouldn't. Only the frontend scripts like index.php belong there, the core application should be outside of any publicly accessible directory. 1 Quote Link to comment Share on other sites More sharing options...
ajoo Posted February 25, 2017 Author Share Posted February 25, 2017 Hi, Thanks Guru Jacques and cyberRoot for the replies. That's why I suggested to create the paths dynamically based on an environment variable... I must have missed it Guru Jacques. Thanks loads. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 27, 2017 Share Posted February 27, 2017 $_SERVER['DOCUMENT_ROOT'] is usually the wrong answer, because that implies your entire application resides in the document root... For what it's worth, you can leave the root with $_SERVER['DOCUMENT_ROOT']/../ Quote Link to comment Share on other sites More sharing options...
benanamen Posted February 27, 2017 Share Posted February 27, 2017 For what it's worth, you can leave the root with $_SERVER['DOCUMENT_ROOT']/../ An application doesn't need to now the entire path to where it sits. It can just start from where it is. For example: Say the app is here /some/long/path/to/app/root And your root/index.php needs to reference a file in root/includes, all you need is include './includes/somefile.php' To go one directory above the app root you would just go include '../logs/anotherfile.php' Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 27, 2017 Share Posted February 27, 2017 An application doesn't need to now the entire path to where it sits. It can just start from where it is. It depends. What about includes within includes? If the primary include file is being brought in at different levels, document-relative links can be problematic. I also prefer to use root-relative links so that I can duplicate a page without needing to modify the path to the website template, for example. I may be duplicating a page that ends up in a different directory. Quote Link to comment Share on other sites More sharing options...
benanamen Posted February 27, 2017 Share Posted February 27, 2017 (edited) What about includes within includes? It works the same way with a slight modification depending on who is calling who. I am working on an app where an include form includes an ajax chained select form that is also in the includes. No problem. I also prefer to use root-relative links so that I can duplicate a page without needing to modify the path to the website template, for example. I may be duplicating a page that ends up in a different directory. I was going to edit my post that that is a side benefit. You can move the app around without having to mess with changing paths. Interested in @Jaques1 comments on this method. Edited February 27, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 27, 2017 Share Posted February 27, 2017 (edited) Relying on relative paths is not a good idea. First, it's always unclear what the paths are relative to. Different PHP APIs handle this differently, and if any part of the application (maybe a library) calls chdir(), you're screwed. Traversing deep directory structures quickly leads to incomprehensible paths like ../../../some/script.php Changing the structure is also hell, because all paths depend on each other. If you're lucky, your IDE will assist you, but I wouldn't bet on that. And relative paths again assume that the entire application sits in the document root, which really shouldn't be the case for any modern project. This greatly increases the attack surface and the risk of leaking internal data (configuration files etc.). Applications today should be split into a public interface within the document root (all scripts which are meant to be directly accessible) and the application core outside of the document root. All paths should start with the (absolute) path of the core directory which can be stored in an environment variable, as explained in #3: require_once getenv('MY_APP_BASE_DIR').'/path/to/file/within/core/directory'; Even better: Use the environment variable to include a configuration script which defines shortcuts for all relevant directories. // include bootstrapping script which provides core features require_once getenv('MY_APP_BASE_DIR').'/bootstrap.php'; // now use specialized configuration parameters or functions to resolve paths load_library('foo/bar.php'); load_functions('database.php'); load_functions('security.php'); Edited February 27, 2017 by Jacques1 Quote Link to comment Share on other sites More sharing options...
ajoo Posted April 6, 2017 Author Share Posted April 6, 2017 Hi Guru Jacques ! getenv('MY_APP_BASE_DIR') yields nothing. Do I have to define 'MY_APP_BASE_DIR' somewhere for this to take effect ? Maybe something like this in the folder containing the index.php: define('MY_APP_BASE_DIR', dirname(__FILE__)); Thanks Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 6, 2017 Share Posted April 6, 2017 Do I have to define 'MY_APP_BASE_DIR' somewhere for this to take effect ? Of course. This was just a placeholder. The webserver needs to define an environment variable containing the path (I'm pretty sure I've already explained this), and then PHP can use this path. Quote Link to comment Share on other sites More sharing options...
ajoo Posted April 7, 2017 Author Share Posted April 7, 2017 Hi Guru Jacques, Thanks for the reply, The Environment variable is now echoing out in PHP. Just for my information, is there some command to help me check, inside ubuntu/linux, for the value i set for SetEnv variable in Virtualhost ? I tried printenv but it does not list the environment variable I defined in the Virtual host. Thank you. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 7, 2017 Share Posted April 7, 2017 The variables only exist during script execution. You cannot access them outside of the PHP process. If you want to list the variables within PHP, you can inspect the $_ENV superglobal. Quote Link to comment Share on other sites More sharing options...
ajoo Posted April 8, 2017 Author Share Posted April 8, 2017 Thanks Guru Jacques ! I have a another question in continuation of this one (paths) but i'll put it as a separate question. Thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.