0

suppose that I am working on a similar server PHP, with more projects, each of them with multiple subfolders and includes. Let's concentrate on proj1:

/var/www/html
    /proj1
        /inc
            header.php
       /assets
            styles.css
            script.js
        page1.php
        index.php
   /proj2
    ...

Now, I want to include the css and the js with a relative path (not absolute) from header.php, but if I use $_SERVER["DOCUMENT_ROOT"] it returns to /var/www/html. What do you think it could be the better solution to echo "script.js" in such scenario, without relying on absolute paths? Thanks

PS: For the sake of clarity, we work on nested directories and structures, so I am searching for a way in which the relative path can change on these different folders, so it needs to be dynamic.

p-marco
  • 59
  • 5
  • Does this answer your question? [Get parent directory of running script](https://stackoverflow.com/questions/1882044/get-parent-directory-of-running-script) – FUZIION Oct 31 '22 at 09:58
  • You could get from `header.php` to `styles.css` by using `include("../assets/styles.css");`. However, I'm worried about your need for it to be "dynamic". I don't have a good idea what you mean by that? – KIKO Software Oct 31 '22 at 10:07
  • I have the impression that you did not fully understand what relative paths are. Something like `$_SERVER["DOCUMENT_ROOT"] . "/path/to/file.js` actually is an absolute path. Just one your created in a dynamic manner. `path/to/file.js` will get interpreted as a _relative_ path, relative to the location of the file using that path. – arkascha Oct 31 '22 at 10:08
  • @KIKOSoftware thanks. What I mean by 'dynamic' is that I need to include these files on different subfolders, so the relative path should change, relative to a ROOT point that I'd like to define (as a relative path, btw). If it's not clear please tell me and I try to explain it better, thanks. – p-marco Oct 31 '22 at 10:16
  • It does indeed sound like you want to use absolute paths. A "ROOT point" is by definition not relative. – KIKO Software Oct 31 '22 at 10:37
  • @KIKOSoftware I would like that dinamically it populates with "../../folder" or "../folder" and so on, in respect to the ROOT point. For it, I am saying relative and not absolute. – p-marco Oct 31 '22 at 11:30
  • Well, actually programming something is not the same as saying it. Many of my clients have found that out the hard way. I think you need to update your question to make clear what it is you want. Currently it sounds like you don't really know, and want us to do the impossible. Don't get me wrong, this is normal, why else would you ask the question? All I ask is that you think through what it is you want, and how that would work. – KIKO Software Oct 31 '22 at 11:40
  • @KIKOSoftware thanks for your time. Actually it was to me clear in my mind, so my apologies if it wasn't when communicating it. The problem I am trying to face is that I want /proj1 as my Root folder ("./") and then everything should be addressed by there. If I include header.php in some subfolders, the references of js and css should be changed in respect to the folder where the include page php resides, otherwise they won't be loaded properly. Let's say that for example header.php has a ./main/script.js, but header is included in a subfolder php file, it should have ../main/script.js – p-marco Oct 31 '22 at 11:58
  • Yes, I got that some time ago, but you don't want to use any fixed point in the directory structure. The problem is: `header.php` has a calling script that can be located anywhere, and you want to find out how its directory is related to the directory of the file you want include in `header.php`. We don't know the absolute location of that file, so how are we ever going to find the relative relation with the location of the calling script? – KIKO Software Oct 31 '22 at 12:18
  • Unless, of course, you want to use an absolute path. We only need one. When you say _"I want /proj1 as my Root folder"_, you're almost suggesting that. – KIKO Software Oct 31 '22 at 12:21
  • @KIKOSoftware thanks, this makes sense to me! I'd use an absolute path but without the "http" part, so that the root folder stills as "./" and not "/var/www/html/proj1". Maybe I could just put a str_replace at this point? – p-marco Oct 31 '22 at 12:24
  • I guess that could work, so the assumption is that everything always starts with "/var/www/html/". Basically you have an absolute path, but you pretend not to. You are familiar with `dirname()`? That would get you the absolute paths of the calling script, you can then remove the "/var/www/html/" section, and count how many parent directories are left, something like `count(explode("/", $parentDirs)) + 1`, that's how many times you need to `str_repeat()` the "../" bit before the path of the file you want to include. – KIKO Software Oct 31 '22 at 12:35
  • It would be much better to just use absolute paths. You don't need to actually write them all out. You could have `const MY_ROOT_DIR = "/var/www/html/";` and then in `header.php` do: `include(MY_ROOT_DIR . "assets/styles.css");`. Of course you would need to put the constant somewhere absolute, and start each script with `include("/var/www/config.inc.php");` or something similar. Somehow you have to pull yourself up by your bootstraps. – KIKO Software Oct 31 '22 at 12:36
  • Yes @KIKOSoftware the problem is that I am working for a project with no time, and I inherit the code from a previous version (no framework, a huge project), so I am forced to reuse the existing logic, for which is not feasible to have the include :( – p-marco Oct 31 '22 at 14:29
  • I'm not sure what the restrictions are you need to work under. If you cannot include that `config.inc.php` file you can go with what you've got. For instance in "header.php" you can access `__DIR__`, which is the directory in which that header script is located. From there you can find the other files. So, to include the CSS file, you would do: `include(dirname(__DIR__) . "/assets/styles.css");`. Again: This uses an absolute path, not a relative path. – KIKO Software Oct 31 '22 at 15:14

0 Answers0