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
Related
So I have a a php script I'd like to run from a bash file script.sh. This is the code:
#!/bin/bash
/usr/bin/php /var/www/html/folder/file.php
The php file sends push notifications to an iOS app. The only way it works for me is if my current directory is /var/www/html/folder/ and the script is also in this
directory. So if I cd .. and try to do
x#x:./folder/script.sh
I get an error from the php file saying "unable to connect to ssl://gateway.sandbox.push.apple.com:2195(Unknown error)". I've also changed the permissions for both the php and bash file to rwx for all. Any ideas why it fails if everything isn't in the same directory?(Using Ubuntu)
As Etan Reisner pointed out, my php file included credentials/keys referenced locally. Referencing them with absolute paths fixed the problem.
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
I have a dreamhost web panel and I am setting a cronjob to run my php script, but it shows weird errors, it looks like that it's not executing the file , but it's reading the file. The error is shown below, it comes as email to me
?php: No such file or directory [somesessionvariable]=1573: command
not found line 3: syntax error near unexpected token
"include/mydb.php"' line 3:require_once("include/mydb.php");
This is the subject of the mail:
Cron /usr/local/bin/setlock -n
/tmp/cronlock.3782446772.128284 sh -c $'/path /to/file/cases_cron.php'
the permission of the file is 755 and i have copied the path from other perfectly running cronjobs, so no issue to the file path as it's getting the file contents. I am not getting where the problem is
I should try to use an absolute path to your include file.
I'm sure the problem is about the include path not being set the same in file call and cron call.
You can do something like this :
require_once dirname(__FILE__)."/include/mydb.php";
__FILE__ is the path of the file where this line is executed, dirname() return the absolute path where the file is located.
Of course, you can use, as you want require, require_once, include, include_one
Your paths are probably off. Try to either avoid relative paths (include/file.php instead of /var/www/project/include/file.php) or change your working directory with chdir().
You can also put an 'cd' command into your cronjob ie:
cd /path/to/phpfile && php -f file.php
Ok, Guys, Thanks for all of your support, I am finally able to sort out the matter.
I was missing the following line on the top of the file
#!/usr/local/bin/php
Adding this line at the top of the php file, sorted my problem. But i don't know that what this line means to the script. But i copied it from other working crons.
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.
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.