Jump to content

PHP pathing difference when running on CLI vs from bash script?


Recommended Posts

Hey y'all.

Probably a dumb question here but I'm at a loss. I've got a PHP service script that has an include, written as so:

$config = new Config('config/config.json');

The config directory is next to the PHP script and does contain the config.json file, so when I run this from the CLI everything works great. However, I've got about 17 PHP services I need to start and really don't feel like typing them all out, so I've written a shell script that does this:

#!bin/bash


cd /path/to/my/script

/usr/local/bin/php ./my-script.php

This does start the service, but it bombs out because it can't find the config.json file.

If I change the PHP to 

$config = new Config('./config/config.json');

it works as expected from the shell script. Updating the files is technically possible, but fraught right now for reasons I can't really get into (sorry). Anybody know what the difference is, or have any ideas on how to get around this?

Edited by maxxd

Depends what Config is doing with the file path. Probably something simple, like file_get_contents()?

Relative paths for plain file access (so not using include/require) are according to the current working directory. From the CLI that's where you were when you started PHP, while for web things it could be the web root or the directory of the executed file or something else.

If the config/config.json path is always relative to the same /path/to/my location then you could add that to Config, along the lines of

public function __construct($path) {
	if (strncmp($path, "/", 1) == 0) {
		// absolute path
	} else if (!empty($_SERVER["DOCUMENT_ROOT"])) {
		$path = $_SERVER["DOCUMENT_ROOT"] . "/" . $path;
	} else {
		// relative to cwd
		$path = getcwd() . "/" . $path;
	}

Using a relative path based on __DIR__ might be easier, as in __DIR__/../$path.

2 hours ago, requinix said:

Relative paths for plain file access (so not using include/require) are according to the current working directory. From the CLI that's where you were when you started PHP

That's what I'm confused on, I guess. I cd to the directory containing the script on the command line, and the config.json file is in the 'config' directory that lives next to the script. The Config object actually doesn't include or require anything, it parses the values in the config.json file. So I kinda assumed that doing a cd in the shell script would actually move the location pointer (for lack of a better term - not really sure what to call it) to the script directory, but it looks like it's just creating a reference to the script directory that the php script doesn't know about? So when the php script hits the relative path of 'config/config.json' it doesn't know where to start, as opposed to './config/config.json', which gives the php a starting directory.

25 minutes ago, maxxd said:

The Config object actually doesn't include or require anything, it parses the values in the config.json file.

...which it has to do by reading the file. That's the important part.

 

25 minutes ago, maxxd said:

So I kinda assumed that doing a cd in the shell script would actually move the location pointer (for lack of a better term - not really sure what to call it)

"Working directory". AKA current working directory (cwd).

 

25 minutes ago, maxxd said:

to the script directory, but it looks like it's just creating a reference to the script directory that the php script doesn't know about? So when the php script hits the relative path of 'config/config.json' it doesn't know where to start, as opposed to './config/config.json', which gives the php a starting directory.

Both of those paths should mean the same thing.

What is the code for actually reading the file?

32 minutes ago, requinix said:

...which it has to do by reading the file. That's the important part.

Good point, thanks.

33 minutes ago, requinix said:

"Working directory". AKA current working directory (cwd)

That's what I would've called it until I decided there may be a difference depending on the context, so thanks for the confirmation there!

34 minutes ago, requinix said:

What is the code for actually reading the file?

I'll have to pull up the actual code tomorrow - it's on my work system; it's inherited code, and I don't remember off the top of my head the exact code at the moment.

35 minutes ago, requinix said:

Both of those paths should mean the same thing.

That's what I thought, too.

I don't know why "config/config.json" and "./config/config.json" would work any differently. Are you sure that's happening? You have some code with the former that works fine, you add the leading ./ with no other changes, and it stops working?

I'm still looking into it, but honestly it may be something with my docker setup - I'm getting weird results where sometimes it'll work, other times it seems to just hang, or some of the services will work as expected but others won't. It seems to be pretty random, which is awesome.

Thanks for taking a look and confirming that the functionality should be the same.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.