Specify PHP filesystem root and keep relative paths

I have a php file in c:\project\

In that file, if I write include ‘somefile.php’, I want php to look for the file in c:\project. (relative to current file)
But if I write include ‘/somefile.php’, I want php to look for the file in c:\ (from a specified root)

Anyone know how I can enable this, which values to set?

You already did the second one. The first one is ./somefile.php

Mybad, I wasn’t clear enough.
Yes it works, but if I want to specify another root instead of default c:, how? I tried doing it with "include_path = " and that’s when things got messed up (because then nothing was relative from the current file)

What is the real problem you are trying to solve by doing this?

The real problem is that I want a consistent system through out my project. My files that are handled by Apache (html, js) uses paths defined like this:

<img src="/somefolder/resource.js">

I want to continue on that road

What is the architecture of your site? Is it a bunch of individual files linked together? Optimally you would want a single point of entry and then “route” requests to call the appropriate resource.

I created a similar project and inside the calling index.php file defined a constant where all the application files reside:

define(‘APP’, ‘../application’);

Inside the application folder are numerous directories such as the following:

./application/
Config
Controllers
Models
Views

The APP folder can copied and be renamed such as application-ver-001 so that the latest version can be ”rolled back” on the off chance there are problems with the latest version.

Also in the same directory as index.php I have an ./asset/ folder with sub directories of css, js, imgs

Included files are either prefixed with APP or /assets/.

That’s not actually a problem, you’re just perceiving it to be a problem.

Apache only has access to the docroot of your site, and it references that docroot as /. PHP has access to the full filesystem, so you can’t treat it the same way.

IMO it’s far easier to accept that and use them as they are. In the long run that will cost less energy than trying to force one of them to work in a way it wasn’t meant for.

1 Like

It may or may not be relevant or be the best answer but on pages I create I almost always want all queries done above output I will define content to be displayed and I also define a relative path back to the primary directory.

$relativepath = "../";

And so all files that I include might have assets will use this path regardless of the depth of the file use. For example the same header file can be used in the primary directory,

$relativepath = "";

in a first level directory,

$relativepath = "../";

or a second level directory.

$relativepath = "../../";

And on the header I might have,

<link type='text/css' rel='stylesheet' href="<?php echo $relativepath;?>css/bootstrap.css" />

Or in a footer I might have,

<script type="text/javascript" src="<?php echo $relativepath;?>js/jquery.js"></script>

Anyway, if the relative path is defined on the page and is based on where the page is within the site structure any other included page can use this defined path to access images or other files. After all is defined I then output the page to the browser.

include '../includes/templates/header.php';
if(isset($content)){ echo $content;}
include '../includes/templates/footer.php';

I tend to keep all my PHP files above the root where they can’t be accessed, with the exception of index.php which is the entry point, but consists of just a require_once of the entry script above the root.
So I treat paths differently on the server side and client side, but both sides are consistent within themselves.

4 Likes

Yes, that’s exactly what I meant.
I do the same thing, which works absolutely fine.

1 Like

Using the relative path saves having to test and hardcode the conflicting $_SYSTEM[“DOCUMENT_ROOT”]; paths between the online and local mirror images of the domains.

Currently I have multiple domains with the relative paths set to the common ../application/ path and they work satisfactorily both locally and online.

Each domain index.php sets different constants applicable to the individual domain such as APP, title, database, tablename, etc before include APP .”index.php”;

I have to say, nowadays with composer the only file I actually require is composer’s autoloader. Everything else is taken care of by composer through PSR-4 autoloading.

When I need open some other file I’ll either use __DIR__ or Symfony’s project_dir parameter. The latter only makes sense when using Symfony.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.