Relative path with a pipe script

I have [root]/includes/helpdesk/pipe.php with this code:

#!/opt/cpanel/ea-php55/root/usr/bin/php
<?php
require_once("pipeprocess.php");

In the same location in pipeprocess.php in first line I include another file still in the same location: helpdesk.php

then in [root]/includes/helpdesk/helpdesk.php in the first line I have:

require_once ("../../config.php");

config.php is in [root], this is why I have twice …/…/, helpdesk.php works fine with direct access, but if run pipe.php via pipe command setup in cPanel I get a bounce error that config. php is not found:

PHP Warning:  require_once(../../config.php): failed to open stream: No such file or directory in /home/[user]/public_html/[root]/includes/helpdesk/helpdesk.php on line 21

Warning: require_once(../../config.php): failed to open stream: No such file or directory in /home/[user]/public_html/[root]/includes/helpdesk/helpdesk.php on line 21
PHP Fatal error:  require_once(): Failed opening required '../../config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/[user]/public_html/[root]/includes/helpdesk/helpdesk.php on line 21

Fatal error: require_once(): Failed opening required '../../config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/[user]/public_html/[root]/includes/helpdesk/helpdesk.php on line 21

Why I am getting this pipe error as direct access to helpdesk.php works fine? does pipe not accept …/ or …/…/ as parth of the path? or is there an EasyApache4 issue?

Hi @nimasdj I think the problem is that when you run the command via cpanel it runs from a different working directory and so the file could not be found. I suggest using absolute paths instead, and then you shouldn’t run into any of these problems.

Hope that helps,

Andres

Hi Andres, you are correct my host said:

It appears that this script is expecting to find the config.php file relative to its own path
/home/user/public_html/root/includes/helpdesk/…/…/config.php (or, more specifically, /home/user/public_html/root/config.php)
However, when the pipe is executed, it is not executed from the same working directory as a HTTP request. The exim pipe is executed from the user’s home directory instead of its own parent directory so when the script searches for “…/…/config.php” it is searching for /config.php which doesn’t exist.

And suggested me to use chdir. But absolute path and chdir are not portable. Is it possible to make my pipe script portable? Any suggestion and trick please?

Hi @nimasdj when you say portable what do you exactly mean, to work for any scenario or to work just for the two cases you described?
What you may be able to do (not sure if this exactly fits into your definition of portability) is to get the working directory with PHP $workingDir = getcwd(); and if it’s different to the expected one then just chdir() to the desired location.

Hope that helps

1 Like

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