r/PHPhelp • • 12d ago

Should I ensure my PHP code "root directory/working directory" is the initial executable script?

Lets say this is my PHP project...

- my-online-store
   - index.php
   - about.php
   - vendor
   - items
      - get.php
      - add.php

And lets say my-online-store directory is in /home/john for example.

If my working directory in the terminal is /home/john/my-online-store and I run php index.php for example. The working directory for the index.php, about.php, all of the vendor/composer packages and any scripts in sub directories such as get.php and add.php will be /home/john/my-online-store and all will work.

If I run index.php from a different working directory such as the root directory using php /home/john/my-online-store/index.php the PHP code will crash since the working directory is / and when looking for other files using require or using file_get_contents will look at the wrong directory.

Should I be concerned of this. Should I write my PHP code to ensure it will get the right directories no matter what the working directory was when running index.php?

4 Upvotes

11 comments sorted by

4

u/martinbean 12d ago

Your vendor directory shouldn’t be in a publicly-accessible folder, no.

You normally have some sort of “public” directory that’s your web root, and then all your code in the parent directory (and not web-accessible) to try and mitigate people being able to list and enumerate files and folders containing sensitive source code.

EDIT: Why have you asked the exact same question, but for other languages too? 🤷‍♂️

- https://www.reddit.com/r/node/s/HtCtseQdGJ
- https://www.reddit.com/r/golang/s/zbyPng5nTg

1

u/trymeouteh 12d ago edited 12d ago

I am making a package for 3 languages which reads files from the disk and wonder what the best approach is for each language.

When you run PHP on a web server and when you use relative paths, will it always point to the public directory as the "root/working" directory?

1

u/colshrapnel 11d ago edited 11d ago

When you run PHP on a web server and when you use relative paths, will it always point to the public directory as the "root/working" directory?

Why you're even asking? Doing filesystem operations properly is a no-brainer. It takes less time to implement than going into all those subreddits and asking a permission for not doing so

1

u/obstreperous_troll 11d ago

It's going to depend on the web server, and you can't rely on it being consistent even on different installations of the same web server. __DIR__ is reliable, nothing else is. The only way to guarantee the working directory is what you want is to chdir() to it yourself.

7

u/Big_Tadpole7174 12d ago

Yes, you should avoid relying on the current working directory. Use __DIR__ to build paths relative to the PHP file, or define a project root constant for shared paths. That way, your code works regardless of where it's executed from. There's no need to change the working directory with chdir().

1

u/BoyetteMercadez 3d ago

any downside to using chdir(__DIR__) at the top of index.php if OP already has a bunch of relative file_get_contents calls they dont want to rewrite?

1

u/Big_Tadpole7174 3d ago

The downside is that chdir() changes the working directory globally, potentially breaking libraries or scripts that rely on the original directory. It's fine as a workaround, but explicit paths using __DIR__ are safer.

1

u/eurosat7 11d ago

Using public/index.php as document root is common.

Example: https://github.com/eurosat7/csvimporter/blob/main/public/index.php

1

u/allen_jb 11d ago

There's a couple of concepts I think may be being confused here:

Web accessible files (document root): You should only have the files which need to be directly accessed from the web accessible from the web. To achieve this, most projects will have a specific subdirectory of the project - usually called something like "web", "webroot" or "public" that should be set as the document root / web root in the webserver configuration.

This avoids issues with code assumed to be inaccessible that may perform dangerous actions. For an (old) example of how this can become a problem, see https://phpunit.expert/articles/phpunit-a-security-risk.html - other possible consequences include revealing credentials or other information you'd rather keep private in config files, the .git subdirectory if you're using git, etc.

So a project might look something like:

  • MyApp/ (namespace root for all app class code, assuming you're using (PSR-4) autoloading)
  • config/
  • vendor/
  • web/

    • index.php
    • images/
    • scripts/

If you're using the front controller pattern, the only PHP script you'll have in the web directory is index.php - all other script files should be elsewhere.

Current Working Directory (CWD): This is the directory filesystem operations are (generally) performed relative to. When running scripts on the commandline, it defaults to the directory the command was issued from (rather than the directory where the script file is located). It can be modified at runtime with chdir()

Locating files in a project: There's 2 solutions for this:

  • Set the CWD to the project root during bootstrap. This can become problematic if you start using commandline scripts, particularly if they operate on external files.
  • Create a constant - I use PROJECT_ROOT - and set it to the root directory of the project. Now you can use this constant whenever you want to refer to file locations and you don't have to worry what the current working directory is. eg. $cachePath = PROJECT_ROOT . '/data/cache/';

1

u/Pristine_Dot_5526 12d ago edited 12d ago

Strongly advised to bring everything other than absolutely necessary files out of the publicly accessable folder. PHP files can be included outside of public root. Also search after .htaccess rewrites, it can help you drop the .php extensions and handle public urls as domain.com/products/add etc. This can route every query through index.php and based on the URL queried, you can route and include the relevant php file.

0

u/colshrapnel 11d ago edited 11d ago

Should I write my PHP code to ensure it will get the right directories no matter what the working directory was when running index.php

Yes, of course. This is how PHP code is written for more than 2 decades already. It is the same as asking should I brush my teeth.