So I've installed 'translate-toolkit' to use 'po2txt', everything works fine when I run it in terminal. But when I try run it in a php file,
<?php
exec('po2txt --version');
I get sh: 1: po2txt: not found
I'm really unsure about what I've missed, if I run for example
<?php
exec('git --version');
It returns normally. So I guess I'm unsure as to why it works when I use it in terminal but if I try execute it through a php file it isn't found..
Where is the installation path of po2txt?
You can simply find it by running this command in terminal: which po2txt
check the path of po2txt is exists in your PATH variable?
Related
I am running PHP and Apache. My program is as follows:
<?php
echo shell_exec("ssh");
?>
It works from the command line as php script.php but when visiting the web browser 127.0.0.1/script.php it returns an empty result.
Any ideas why this may be happening?
Edit 1:
I tried running ssh as the www-data user through command line. This worked fine.
Edit 2:
I tried running sshpass instead of ssh (same location, same permissions) and it works but ssh does not.
Mentioned in kamermans's note, the help message of ssh is output to the stderr stream, so try
echo shell_exec('ssh 2>&1');
I have a C program that I wrote called convert3to5, originally written for CentOS / Fedora 32bit system in early 2010. I am moving it to new CentOS 6.x 64bit system host.
From a CentOS Putty console I can run the convert3to5 command just fine; here is a sample of it running from my console:
[root#cloud convert3to5]# ls
CircleStar convert3to5 Convert3To5.txt test.tif
[root#cloud convert3to5]# ./convert3to5 /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif
TIFFReadDirectory: Warning, /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif: wrong data type 7 for "RichTIFFIPTC"; tag ignored. Image has an undefined fillorder - using default: MSB2LSB
The above is a normal completion of convert3to5 and I get a SV-DIS160217B.bmp that is placed in /var/www/webadmin/data/www/mydomain.com/uploads/ So running it from console works fine.
Question - I am attempting to run the same exact command from PHP using the exec(command, output, return) command as follows:
chdir($sv_path.$c3to5_path); //change our working directory to "/convert3to5" directory
$command = "./convert3to5 $targetFile 2>&1";
$result = exec($command, $output, $return);
// the output of the above command - is a .bmp file it will be placed in the same path as the input .tif file
I get the following $result:
ERROR: Unable to convert
/var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif to 5
color BMP file: Open file Error: Tiff_3_to_BMP_5_.lut!
My convert3to5 does need to open Tiff_3_to_BMP_5_.lut
Why does it find Tiff_3_to_BMP_5_.lut when I run convert3to5 from a console prompt but not from PHP exec(...) in both cases my pwd shows that I am in
[root#cloud convert3to5]# pwd
/var/www/webadmin/data/www/mydomain.com/myView/convert3to5
I have also verified pwd is correct from my PHP script after the
chdir($sv_path.$c3to5_path);
Tiff_3_to_BMP_5_.lut is in CircleStar directory - the path to CircleStar is /var/www/webadmin/data/www/mydomain.com/myView/convert3to5/CircleStar
Summary: ./convert3to5 works while PHP exec('convert3to5 ..) does not appear to work.
Can anyone suggest the difference and how to fix and/or debug?
Thanks
You're running the console from the convert3to5 directory, and I suspect your old C program used a relative path to the .lut file, possible relative to the .tif?
What if in the console example you did
cd ../..
./path/to/convert3to5/convert3to5 /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif
Might be related to $targetFile. Print that and see if it's the full path.
Finally, run
/full/path/to/convert3to5 fullTargetPath
If that works, then as a workaround, if you just do exec('/full/path/to/convert3to5 $fullTargetPath, ..) it should behave like the console.
Per my above comment to wonton:
From the console I was running as root (so fully privileged). I supposed my PHP script will run as the "apache" user on the server?
Here was the problem I believe: I looked at the CircleStar directory privileges where the Tiff_3_to_BMP_5_.lut file exists. CircleStar had rw-r--r-- (0644) when running as root from console this allowed my convert3to5 program to find and open Tiff_3_to_BMP_5_.lut file just fine. However not the PHP exec(...) once I changed the privilege on CircleStar to rwxr-xr-x (0755) PHP exec(...) ran fine!
So ultimately it was a permission issue.
I've got the following script:
#!/bin/sh
export DISPLAY=:0
phantomjs --version
It try to run it from the following PHP script:
<?php
$result = shell_exec('sh test.sh');
echo $result;
?>
This script return the following error:
[Thu Jun 19 10:31:31 2014] [error] [client] test.sh: line 3: phantomjs: command not found
I tried to run phantomjs -v by hand in a console, and it runs fine. I checked the PATH, and phantomjs is correctly defined and found.
The execution environment is a virtual Server with LiveConfig.
Can someone help me understand what I'm doing wrong ?
It could be an issue with shell_exec() and line breaks,
try adding "2>&1" to the string you are passing:
$result = shell_exec('sh test.sh 2>&1');
this worked for me, found it in the top comment here, naturally ;)
Your PATH probably lacks the location for the phantomjs executable. PhantomJS is probably installed in /usr/local/bin so you need to add this to your PATH variable:
#!/bin/sh
export DISPLAY=:0
PATH=$PATH:/usr/local/bin
phantomjs --version
To check what the current PATH is, you could begin the shell script with:
#!/bin/sh
echo $PATH
<?php
exec('/usr/local/bin/phantomjs path/somescript.js');
?>
Yes. Sometimes phantomjs don't need full path in some environment without generate any error. However, sometimes it does.
Always use the full path for all argument in the php command.
Did you use the fullpath for hello.js?
Do not use exec(). Never. It's a bad way.
Use the php-phantomjs and PhantomJS Runner instead.
I was following the guide written here Run Bash Command from PHP
and I have /var/www/test.php
<?php $old_path = getcwd();
chdir('/home/');
$output = shell_exec('./test.sh');
echo "<pre>$output</pre>";
chdir($old_path); ?>
and located by using sudo nano /home/test.sh
#/bin/bash
mystring="Hello World"
echo "$mystring"
yet running locally http://localhost/test.php I get a blank page.
I can run the file from the terminal, and I can run shell_exec('ls') successfully as php script, can anyone see what I am doing wrong please?
Ok problem was resolved after a few typos and permissions not being set, but main problem it would seem is the getcwd() does not run on my OS locally or on the server... both Ubuntu 12.04
So for anyone else stuck with problems be sure to:
Check the File can be executed via the terminal, if not run chmod +x /file/location/test.sh
next check what directory you are in and moving to using the echo shell_exec('pwd')
If you are not moving directory then try replacing the getcwd() with dirname(__FILE__);
This resolved the issues for me :) now to ty passing variables into that script from the php page!! Many thanks for all your help everyone.
`
I'm using Unison to sync files between two servers. I'm trying to get PHP to call it after a file is uploaded.
I'm using exec, but it's returning an error code of 2.
exec("/usr/bin/unison /var/www/html/files ssh://a2//var/www/html/files -batch -prefer newer -times -path uploads", $out, $r);
$out is a blank array, and $r is 2. What does an error code of 2 mean?
P.S. I ran php -a on the command line, and copied and pasted that line, and it worked. Also, exec('whoami') works (and is the same user I was logged in as on the command line).
I fixed it! Using popen (thanks #sberry2A) I saw an error.
Fatal error: Error in creating unison directory /.unison: Permission denied [mkdir(/.unison)]
I ran chdir('/home/user');, before running the command, and then saw an error about HOME not being set.
So, I added HOME=/home/user before the command. Now it works, and I don't need the chdir command either!
exec("HOME=/home/user /usr/bin/unison /var/www/html/files ssh://a2//var/www/html/files -batch -prefer newer -times -path uploads", $out, $r);