file path issue when running via command line versus durect URL - php

We have a few processes that we run via the command line (CRON)
/usr/bin/php /path/to/site/root/index.php --uri=cli/fifteen/clear_apc
With a recent change to the index file:
require_once(../opt/local/config/setting.php)
PHP returns:
No such file or directory
However, running it directly from the browser (same file, path), it runs correctly, including that file:
www.site.com/cli/fifteen/clear_apc
Im wondering if there is a work around for that path issue, without having to include the entire path from root (/path/to/site/opt/local/config/setting.php)

The working directory is different when running under cron and apache. Relative paths are relative to the current working directory, so that's why your code is not working as expected.
You can try require_once(dirname(__FILE__) . "../opt/local/config/setting.php"). This should generate an absolute path starting from your file's location.
Check out all the magic constants here: http://php.net/manual/en/language.constants.predefined.php

Related

PHP script (include) not working from command line

I have a php script that works perfectly fine when run by the web server but when I run the script from the command line (or as a cron job) then it fails to find the file. The code that errors out is:
include('fpdf181/fpdf.php');
I tried including DIR but that does not work either
include(__DIR__ .'fpdf181/fpdf.php');
Can someone tell me how to include files for a script that will run from the command line
can you see this (PHP Cron Job: Including file not working?)
as the link, you use "require" and not include
Code:
require('./fpdf181/fpdf.php')
happy codeing.
You can use the full path to the file (independent of current directory / env).
include('/path/to/lib/fpdf181/fpdf.php');
I was able to execute my script by doing a cwd on the directory and going to the folder where the script was placed

PHP - No such file or directory (LAMP server)

I've been on this bug for too long so I'm looking for some help from more experienced server programmers.
I'm running a Bitnami LAMP stack. I'm trying to use a PHP script (maintenance.php) to move files on the server. So I'm using rename(filepath, newpath) in my script. However, the PHP script cannot find the file even though it exists on the server.
SOLVED: The problem was that I was calling the script from two different places during debug (my browser, and my linux shell). The "working directory" in each of these places was different so the filepaths represented different locations depending on where I called the PHP script from.
There is no pictures directory at root so /pictures wont work.
Instead you will have to use absolute path to the pictures directory.
There are multiple ways to do this.
You can use $_SERVER['DOCUMENT_ROOT'] which will give you root directory under which the current script is executing.
reserved.variables.server
Also, you can use __FILE__ which will give you the path to current file.
With dirname(__FILE__) you can get directory and then you can work up to the pictures directory by going level down using ../.
This is also a similar question that you can check,
PHP include absolute path

get current directory of symlink'd php script and not actual php script

I have a script that is symlinked from one folder to another
/var/www/default/index.php
which is symlinked to
/var/www/mysite/index.php
However when i call DIR from mysite the path resolves to the origial path under default. How do i make it return the mysite path (the symlinked folder, not the actual folder)
For Web Server requests
dirname($_SERVER['SCRIPT_FILENAME']) will give you what you need.
Failing that $_SERVER['PHP_SELF'] or even REQUEST_URI may have what you need, depending on your server configuration.
For CLI (command Line)
this won't work in cli (command line) scripts as $_SERVER is added by the web server.
Fortunately it's a lot easier with cli scripts (as there is no web server to mess about with things).
All you need is to read the command line that started the script: http://php.net/manual/en/reserved.variables.argv.php. The first argument $argv[0] is always the name that was used to run the script.
If you want to get the current path with symlink you can ask your host OS:
$path = exec('pwd');
May be useful:
var_dump($_SERVER['PWD']);
If you are looking for the full unresolved system path of a cli script, SCRIPT_PATH will be insufficient.
php -f symlink/script.php
SCRIPT_FILENAME will contain symlink/script.php
I have created a php/gist that gets the unresolved path to the php file.
Here is the outcome of the function:
$ php -f subdir/mysymlink/subdir/mysymlink/subdir/mysymlink/app.php
PWD: /tmp/phpcode
SCRIPT_FILENAME: subdir/mysymlink/subdir/mysymlink/subdir/mysymlink/app.php
___FILE__ : /tmp/phpcode/app.php
getSymlink(): /tmp/phpcode/subdir/mysymlink/subdir/mysymlink/subdir/mysymlink

How to set current working directory in PHP to the script executed directory?

I have a website on iis7 with php.
When i call getcwd() it returns base directory whith php-cgi.exe. But I whant cwd automaticly change to current executed script directory.
How can i configure it via php.ini or iis or else?
I think you just need to chdir() in your script:
chdir(__DIR__);
Or, before PHP 5.3:
chdir(dirname(__FILE__));
To the currently executed script directory: chdir(dirname($_SERVER['SCRIPT_FILENAME']));
To the currently executed file directory: chdir(dirname(__FILE__));
There is a slight difference between them. Script directory is always the directory of the main script file being executed, while the file directory can also be the directory of the included php script file.

Does it matter if I use relative or absolute path's when running a php script via cron?

I am currently working on a script that I am attempting to automate via cron. Running the script via terminal is just fine, but when I attempt to run the script with crontab, I am getting some issues.
Part of my script loads and validates and xml file via DOMDocument::loadXML() and DOMDocument::validate() and php throws an error when attempting to validate stating:
Failed to load external entity: /linuxuser/homefolder/my_dtd.dtd
Within the xml file, the dtd is set to:
../../../../../../../my_dtd.dtd
Is there some misconfiguration of the server or is it more likely something wrong with my php code at this point? It seems to grab my linux home directory rather than the path relative to the xml file. Just wondering if anyone else has seen an issue like this or could point me in the right direction. Thanks.
Quoting the PHP documentation for differences in CLI usage (command-line interface):
The CLI SAPI does not change the
current directory to the directory of
the executed script!
When PHP scripts are run via CRON, it is executed on the user's home directory. You can either replace all relative path references used by the script to absolute, or place this on the start of the script:
chdir(dirname(__FILE__)); # for PHP 5.2.x and below
# or
chdir(__DIR__); # for PHP 5.3+
Most likely, the problem is in working dir and resolving relative path.
Try absolute path in your xml file.

Categories