PHP command line fails with no error on full path - php

Basically it's this
>php /full/path/to/php/file.php
Fails with no output, warning, error anywhere in any log. Seems like it runs fine, but it actually does not run. However, changing working directory to /full/path/to/file/php
> php file.php
Works as expected.
What is special about this php script is that it:
reads files from an NFS mount
parses contents
loads file contents into a local MySQL table
renames files it has successfully loaded.
Operating system is CentOS 6.

Based on your edit, in your script, you should change to the correct directory. You can do that using using chdir:
chdir('/full/path/to/php/');
Now php is probably looking for the symlink in the wrong place.
You can use getcwd() to see in which directory you are.

Related

Issue with calling php script with cron

I have set up a cron to call a php script to run some download tasks at a regular interval.
The website is hosted at Bluehost. I have followed the instructions on how to set up a cron and that basically works fine, but the behaviour is different from when calling the script manually which i suspect has to do with directory settings. When using the cron i get errors:
Warning: copy(wp-content/uploads/feeds/full/1.csv): failed to open stream: No such file or directory in /home1/user1/public_html/import/custom-downloader.php on line 86
my php code is copy( $row["externalURL"] , 'wp-content/uploads/feeds/full/'. $localfilename );
I have also tried below with same result.
copy( $row["externalURL"] , $_SERVER['DOCUMENT_ROOT'].'wp-content/uploads/feeds/full/'. $localfilename );
I think think that i need to construct the path to /home1/user1/public_html/import/wp-content/uploads/feeds/full/ but i don't see how to do that.
First thing's first, make sure that 1.csv exists and that it can be read (there's read permissions on the file)
Second thing, double check if 'wp-content/uploads/feeds/full/1.csv' really is what you want to copy. Your error message indicates that the php script calling the copy method is /home1/user1/public_html/import/custom-downloader.php. However, the path you specified is relative. So your PHP script (barring any changes to the PATH to look for other directories) will look for /home1/user1/public_html/import/wp-content/uploads/feeds/full/1.csv -- which may not be what you intended!
This can be solved by using an absolute path to your wp-content folder, or by moving this script to the parent directory of wp-content.

Running jar file from php gives different output as from the terminal

I have a jar file that needs to access a credentials file from (/home/user) location.
Now while running from the terminal, it works properly. However, when I execute
exec("java -jar Main.jar");
from php, it gives me an error, asking me to check if my file is at the location, and is formatted (catch was activated after it couldn't get the credentials).
Now I assumed this was a permissions issue, and so I used
is_readable("/home/user/credentials")
It said that the file was readable. So where am I going wrong?
Make sure that the jar file resides in the same location of php file which you are trying to read and also the java environmental variable is set properly to run java from anywhere.

PHP external program call

My setup is as follows: Windows 7, XAMPP with Apache and PHP enabled I have a PHP script in which I call an external program to do run a conversion. This external program is an EXE file, which requires 3 attributes:
The source file
The destination file
Additional flags (conversion type etc)
When I use the command line tool built into XAMPP to execute my script, everything works fine. But when I use the exec() function in my PHP script, no output file is created. I'm pretty sure the conversion is actually happening (it takes about 5 seconds, about the same time it takes to run the PHP script).
I think it's a permissions thing, so I already moved the EXE file to the same folder as my PHP file and adjusted the permissions of the entire folder (I granted all permissions to all users). I also disabled the Windows UAC and tried to put the command in a BAT file. The file just is not created.
Any help or tips would be greatly appreciated!
My PHP code is as follows:
exec('c:\converter.exe c:\src.txt c:\dst.txt -f', $output);
print_r($output);
When I print out $output, the array turns out to be empty. When I put the exact same command in Command Prompt, the code works like a charm (no syntax errors). I use absolute paths as well.
Try to copy your executable file in same folder as your application.
try
exec("script.exe src.txt dst.txt", &$output);
echo $output;
also, do not forget to use escapeshellcmd() to add some security to your application.
Thank you very much for your input! As it turns out, it was Windows issue caused by the 'Interactive Services Detection' feature. Apache was running as a system service, which prevented calls to external programs (with a GUI). I disabled the run-as-service feature in XAMPP, which solved the problem. A more thorough explanation can be found here: http://php.net/manual/en/book.exec.php

how to give command line to run browser which in turn runs php script

question is how to give command line (in .cmd script) to execute browser to then in turn have browser execute .php script.
Presently if from a command window I execute like ...
"c:\Program Files\Internet Explorer\iexplore.exe" file:\c:\users\win7ultsdtest\findroot.php
....OR....
C:\Program Files\Internet Explorer\iexplore c:\users\win7ultsdtest\findroot.php
This will run the Explorer browser, but then the browser the browser will download the contents of findroot.php instead of executing the php code as I need. Does anyone know how I can get the browser to instead execute the php code and not just download it as data?
Let me explain my need ... The findroot.php file contains php code to access the $_SERVER['DOCUMENT_ROOT'] variable. This variable is ONLY non-null when the localhost is running a http server and then it contains the localhost document server root path where loadable browser .html, .php etc may be stored to loaded from http:\ lines.
The findroot.php outputs the $_SERVER['DOCUMENT_ROOT'] contents to a file as rootpath.txt so that my .cmd script can then can automatically install PHP code into the active PHP servers document root area.
So understand I must find the $_SERVER['DOCUMENT_ROOT'] from a .cmd script.
Now I might search ALL the computers drives for httpd.conf and then scan that file for the value but this wouldn't work for two reasons; 1. there can be multiple httpd.conf files and I can't know which server is active and using what httpd.conf. 2. it would take a long time to search a given computers entire drive(s) on all httpd.conf files.
The browser won't execute PHP code. You either need a server to run PHP and to access it via HTTP such as C:\Program Files\Internet Explorer\iexplore http://localhost/url/for/findroot.php or you can run PHP via the command line c:\path\to\php.exe c:\users\win7ultsdtest\findroot.php. However running it via the command line won't give you $_SERVER['DOCUMENT_ROOT'] as that is only populated when running PHP within a server.
It's not possible to execute PHP by the HTTP server without the server knowing about the PHP before hand - for example, by being in the document root. Of course if you don't know the document root, your script won't be there. If you're trying to install a script into a web server for a user, it's much better to give instructions on how to do so as their server environment will likely vary from what you expect.
There are light browsers like lynx if you are in a linux machine
lynx http://whateverurl/php.php

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