On the terminal, I run this successfully within the web application's directory:
pdftohtml -c -noframes "documents/document1.pdf"
Now I want to do this through PHP, so I wrote a shell.sh file that looks like this:
sudo pdftohtml -c -noframes "documents/$file"
exit 0
Then I wrote this in php:
$output = shell_exec("file='document1.pdf' shell.sh");
It doesn't work, I expect to see html files generated, but I get some empty html files..since the command worked fine through the terminal, then I think the problem is in the way I execute it from php
echoing $output doesn't show anything.. what do I do wrong?
You need specify path to the script (or ./ if it is the current directory):
shell_exec("file='document1.pdf' ./shell.sh")
Related
I had create a cmd file:
phpunit Test.php > myTest.txt
It work when i run is directly. But when exec with PHP code:
exec("cmd.cmd");
a file myTest.txt created, but it blank
Most likely because phpUnit is not run when you call it via php (this could be due to security privileges).
Try
$output = '';
exec("cmd.cmd", $output);
echo $output;
And see what the execution actually returned, also you might need to specify a path since exec might run from the PHP execution path, not your PHP webroot where the file exists.
I'm developing a code which uses ldap_search Shell Script Command for extracting user information from Active Directory using user id and by proper LDAP Server Authentication. I am getting accurate result from ldap_search script.
But, whenever I put the shell script inside exec or shell_exec PHP command, I'm not getting anything.
All the other shell scripts are working fine with the help of PHP exec command except ldap_search.
Is there some additional task left for me to do?
Is ldap_search and exec/shell_exec not compatible with each other?
You must use echo exec('your command or script');
Make sure to have permissions to run it. I mean, the web user must have permissions to execute that.
May seem obvious, but I think your failure is in something basic like this. You must put echo to show the result of the command.
EDIT After reading your new comments about it and using that new info... I saw you are trying to redirect the output to a file... but maybe you have 2 different problems.
Have the user which is executing php (usually www-data) permission to write on the folder where the php is?
Your code has quotes inside quotes that must be escaped using . Try this:
<?php exec("ldapsearch -x -v -h 'LDAP://server' -p '389' -D 'uid=\"domain_user_id\",ou=users,ou=internal,o=\"organization\"' -w 'domain_password' -b 'ou=users,ou=internal,o=organization' 'uid=person's_user_id' >> result.txt"); ?>
So you don't need echo if you want the output in a file. And the redirection >> can be inside the command executed, not in php.
Remember that > replaces de file and what you have >> add at the end of the file.
I'm experiencing a weird problem in trying to execute python in a php server (LAMP). (safe_mode off)
if I type:
$output = shell_exec("ls -lah");
echo "<pre>$Output</pre>";
I got the result of the ls command. Same for$output = shell_exec("tar --version"); and other applications, such as gzip.
However, if I switch for any of these lines:
$output = shell_exec("python --version");
$output = shell_exec("python2.7 --version");
$output = shell_exec("/usr/bin/python --version");
$output = shell_exec("python my_script.py");
And other variants of this kind, I get no results. The command is not being executed, the python bitecode not made and the echo remains silent.
I have also tried with the exec() command with no more success.
I think this may help...
looks like the output for the python call needs to be routed properly.
I was able to make this work within my index.php file for returning the python version...
shell_exec("python -V 2>&1");
Here is where I found the answer.
If you are trying to run the python script using the following code
$output = shell_exec("python my_script.py");
you will need to use absolute path for my_script.py and give all permissions (I am not sure which ones are sufficient) for the python file.
I think you need to refer to the full path for your python.
for example use this instead:
$output = shell_exec("/usr/bin/python full_path/my_script.py")
instead of:
$output = shell_exec("python my_script.py");
I think kernel not able to find the path for python where it is installed..if you can do echo $PATH..it will show all the paths where to be search a command if given
add your python part there and then it may work or you can give absolute path(other than /usr/bin/) see if it works..I need to test it too.
What does
which python
tell you, both from the command line and from shell_exec()? It should tell you which (if any) Python interpreter it's finding (from $PATH). Don't forget that it's quite possible that the $PATH used from the Linux command line might not be the same as the $PATH used by shell_exec()! Once you find the Python interpreter you want to use, you might have to hard code it in the shell_exec().
Most likely the web server doesn't have appropriate rights to execute shell commands. To fix this, run the 'sudo visudo' command and add the following line to the sudoers file:
www-data ALL=NOPASSWD: ALL
Also, make sure that the /var/www directory belongs to the www-data user and group (use sudo chown -R www-data:www-data /var/www to set the correct owner). The details are here http://www.raspberry-pi-geek.com/Archive/2014/07/PHP-on-Raspberry-Pi
Also refer
Can't execute python script from php
In addition to my previous question, another problem appeared and I decided to make a new question for it:
I am currently calling a php script that than runs a bash script. The php script looks like:
chdir('/home/');
$output = shell_exec('./do.sh');
echo "<pre>$output</pre>";
The do.sh contains:
#! /bin/bash
echo 12;
dd if=/dev/urandom of=test.test bs=1048576 count=2
The problem is following:
When I call ./do.sh from the terminal everything works fine: test.test is created and the ouput is 12
However, when I call it from my php file, the output is 12 aswell, but no file is being created. Since I know almost nothing about bash scripting, I have no idea why this is happening...
Check if PHP safe_mode is enabled. You have to turn it off in your /etc/php.ini file, and obviously check filesystem permissions.
I would like to change the directory in Linux terminal from cli script, not the PHP current working directory -- hopefully with shell_exec().
Ex: from user#host:~$ to user#host:/the/other/directory$
system() and exec() are not allowed.
This isn't working in my case:
$dir = '/the/other/directory';
shell_exec('cd '.$dir);
nor these
shell_exec('cd '.escapeshellarg($dir));
shell_exec(escapeshellcmd('cd '.$dir));
pclose(popen('cd '.$dir));
But shell_exec('ls '.$dir) gives me the list in that directory. Any trickery?
When you're executing several commands like you are doing:
shell_exec('cd '.escapeshellarg($dir));
shell_exec(escapeshellcmd('cd '.$dir));
It won't work. The first command has nothing to do with the second one, so you can't use the results of the 1st command to execute the 2nd command.
If you want to execute a chain of commands, use the pipe symbol | like:
echo shell_exec("ls /etc/apache2 | grep fileName");
Local changes made by shell_exec only apply to that php process and therefore ls would fetch from that other directory. But when you exit the php, you are back to bash who never really cares what process does.
I doubt you can change bash's current directory from outside.
If you need to run bash in other directory, you could try this inside your php:
system('gnome-terminal --working-directtory=/home/doom');
this will start another terminal in new dir but script will wait till you quit this shell.
If you mean you want to change the parent shell's directory by calling a php cli script, then you can't do that. What you could do is at best:
shell> eval `your/cli/script`
and in your script do something like
<?php
...
echo "cd $dir"
?>