Running exec command php - php

I have just installed pdf2htmlEX without any issues. If I run the command on the server it works fine. So I know the library itself is doing what it is supposed to do. If I run the command in php via the exec function nothing happens. I assume exec is used in this instance??
In PHP
// doesn't work
$output = exec('pdf2htmlEX --zoom 1.3 pdf/test.pdf');
// doesn't work
$output = shell_exec('pdf2htmlEX --zoom 1.3 pdf/test.pdf');
If I run a general command in the exec function it works fine, so I know exec is enabled and working as expected.
// works fine
$exec = exec('pwd', $output);
print_r($output);
Direct on Command Line not in PHP
// works and generates the file as expected.
pdf2htmlEX --zoom 1.3 pdf/test.pdf
Any help would be greatly appreciated.
Command line reference for library
https://github.com/coolwanglu/pdf2htmlEX/wiki/Quick-Start
EDIT:
After some further digging, it could be that the php script runs as a different user to the command line. My question would then be how do I check/fix this?

You just have to add the output html path and file name in your command :
Most of pdf_to_something packages usage with exec() is package_name [options] <input-filename> <output-filename>
$path = 'path/to/your/folder/';
$command = 'pdf2htmlEX '.$path.'test.pdf '.$path.'test.html';
exec($command);

Try following and check if your file is created in working directory.
exec('pdf2htmlEX --zoom 1.3 pdf/test.pdf',$output);

pdf2htmlEX doesn't output anything.
$cmd= "pdf2htmlEX --zoom 1.3 test/test.pdf";
exec($cmd, $output);
Will create a test.html file in the current directory.
Are you expecting something else?
Note: i've tested this on ubuntu by installing pdf2htmlEX with
sudo apt-get install pdf2htmlex
To get the output,
$a = file_get_contents("test.html");
echo $a;

Related

Not able to run wkhtmltopdf commad through shell_exec() function in php but same command works on command line

I'm in trouble and that much confused about a php shell_exec command.
When the command is execute by PHP I have no error but the execution fails. If I use exactly the same command from a terminal it works.
Here's the command :
/usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyxa9otq.html" "/tmp/knplabs_snappyv3pD7h.pdf"
When I lauch this from a terminal :
$ /usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyWG9XTd.html" "/tmp/knplabs_snappyv3pD7h.pdf"
Loading page (1/2)
Printing pages (2/2)
Done
But from my php script :
// Construct the previous command
$command = $this->buildCommand($url, $path);
../..
shell_exec($command);
../..
$content = file_get_contents($path);
../..
I've test the output of shell_exec, it's empty.
The log :
Warning: file_get_contents(/tmp/knplabs_snappyv3pD7h.pdf): failed to open stream: No such file or directory in /*****/lib/snappy/SnappyMedia.class.php on line 64
No permission pb in the /tmp directory :
$ ls -la /tmp
total 448
drwxrwxrwt 16 root root 4096 mars 12 21:51 .
../..
I've tried avec the PHP exec() function to get error informations, I just get an "1" error code in return_var and nothing in output.
For information this issue appear on my test server, my desktop computer but not on my notebook. All the 3 are with sames PHP, Apache, Mysql versions.
I don't understand anything ...
Thanks for any help, I'm loosing my mind.
David.
I've found the solution here : Executing wkhtmltopdf from PHP fails
Thanks to Krzychu.
First to get information from the shell_exec command add " 2>&1" at the end of the command. In that way you will get information in return of the command :
$no_output = shell_exec($command);
echo $no_output; // nothing
$output = shell_exec($command . ' 2>&1');
echo $output; // in my case : "cannot connect to X server"
The solution :
Not use the wkhtmltopdf ubuntu package (0.9.9-4)
Use the official package from the Wkhtmltopdf download page
So no need to install xvfb ! (I've seen this advice many times)
Looks like a user's permissions issue.
When you run the command from the terminal, it is the user account, currently used, which does have the right permissions, to run a command in /usr/bin, and execute the specific file.
When you run it from the php script, it is the http server account on your system, which needs the permission to execute the file in /usr/bin. Usually this is the apache user.
How you should setup permissions depends on your system. Just remember that what is allowed for apache, is allowed for anyone accessing your http server.
I have had this problem for ages and adding . ' 2>&1' after the $command has somehow solved the problem.
this:
$output = shell_exec($command . ' 2>&1');
instead of:
$output = shell_exec($command);
No idea why but it works and I'm grateful.
Is it a shared hosting? It seems like shell_exec is a restricted function. Try running error_reporting(E_ALL); ini_set('display_errors', 1); before calling shell_exec.
I stumbled upon the same Problem, in my case an absolut Path in the exec Command like /var/www did not work, I had to use relative Paths from the point where I executed the php File.
I also wanted to notice, that it did not work using shell_exec, however it worked using normal exec command, not sure wheres the difference here.

PHP + winexe -> loads forever

I'm trying to list running services on a windows server via php. Therefore I'm using shell_exec with winexe.
My script:
$cmd = "winexe --interactive=0 --user='***' --password='***' //192.168.***.** \"net start\"";
$output = shell_exec($cmd);
echo $output;
Unfortunately on execution the page loads forever with no result. The command works on the command-line (Debian).
Anyone an idea?
Thanks in advance.
Save $cmd with correct format into a new bash file. Set cmd value for call this file. Remember set execution perms to this file.
Check if your apache user has perms for exec winexe
===
Try to launch
cat </dev/null | winexe --interactive=0 --ostype=1 --user=...

FFMPEG command doesn't work in PHP. (Incompatibilty with MAMP)

I'm a bit of a beginner when it comes to PHP, and I'm trying to create a simple(ish) system where files are input, and then converted to html5 video in various resolutions.
I've sorted out how to handle multiple file uploads etc, but now I'm having a problem.
I can't seem to get exec to execute FFMPEG in PHP.
For example, if I type this into my command line (Terminal on Mac OSX 10.8), It converts the video correctly:
ffmpeg -i /Users/charlieryan/Desktop/MOV01785.MPG websample.mov
This correctly outputs the converted video file into my home directory.
However if I run this in PHP as follows:
exec('ffmpeg -i /Users/charlieryan/Desktop/MOV01785.MPG websample.mov');
Absolutely nothing happens ... my stat monitor doesn't register any change in processor use, and I can't find the output file anywhere on my system.
Since I'm a bit of a noob at this, I'm assuming I'm doing something wrong, but what is it?
Thanks,
Charlie
After alot of help from birgire and a lot of fiddling around I've sorted it.
This problem comes from an incompatibility with the MAMP sandbox. Which can be solved as follows:
Go to Terminal and type:
sudo nano /Applications/MAMP/Library/bin/envvars
Then comment out the following lines with a hash (#)
# DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
# export DYLD_LIBRARY_PATH
And then add the following line to the file
export PATH="$PATH:/opt/local/bin"
Then, go back to MAMP and restart your servers, navigate back to the page, and you'll be good to go.
You should first try to see if exec() is allowed:
<?php echo exec('echo "exec() is working"');?>
if it's working you should get
exec() is working
If it works you should try
exec('/full/path/to/ffmpeg -i /Users/charlieryan/Desktop/MOV01785.MPG websample.mov');
I had the same problem, If you use MAMP, the problem is because mamp's php can't find the correct library, I don't know why!, so.. here's the trick.
- You should use system's php to execute the php which will call to ffmpeg
In your php code (ex: lib.php | index.php):
function callToSysPHP ($videoName) {
// $cmd = '/path to php/php <your php script> args';
// In my case
$cmd = '/usr/bin/php myffmpeg.php ' . $videoName;
shell_exec($cmd);
}
In myffmpeg.php:
$videoName = $argv[1];
//$cmd = 'path to your ffmpeg/your ffmpeg command';
// In my case my ffmpeg cmd looks like
$cmd = '/usr/sbin/' . 'ffmpeg -f image2 -framerate 25 -i ./files/pngs/%1d.png -vf scale=480:640 -vcodec libx264 -refs 16 -preset ultrafast ./files/pngs/'. $videoName .'.mp4 2>&1';
echo '<pre>'; print_r(shell_exec($cmd)); echo '</pre>';
Basically from your mamp php, call a system php to execute a php file wich calls a ffmpeg throught shell_exec();
I hope this can help you.
have you ffmpeg installed on windows machine? what happens if you run the same command from command line without php, does it work? If it doesn't, it hasn't to do anything with PHP.
If '/usr/local/bin/' is the directory where you can find the ffmepg executable try this one:
<?php
$cmd = 'PATH="/usr/local/bin/"; ffmpeg -i /your/file/destination/batman.mp4 2>&1';
echo "<pre>".shell_exec($cmd)."</pre>";
?>

PHP and Apache : shell_exec(wkhtmltopdf with xvfb) command doesn't work

I try to run the wkhtmltopdf (0.11.0 rc1) with php (5.4.2) on apache (2.4.2).
When I try to launch wkhtmltopdf-i386 --use-xserver http://www.google.com google.pdf 2>&1, I can find my pdf. Here my php code
<?php
$cmd= '/usr/bin/wkhtmltopdf-i386 http://www.google.com google.pdf 2>&1';
$ret = shell_exec($cmd);
echo $ret;
?>
It works with apache and as command line php test.php.
Because my target page contains many images and some "heavy" js charts. I have got a Segmentation Fault with the wkhtmltopdf command when I try to turn it into pdf.
The only way to make it work is to use xvfb as X11 emulator. The code looks like this :
<?php
$cmd= '/usr/bin/xvfb-run /usr/bin/wkhtmltopdf-i386 --use-xserver http://www.google.com google.pdf 2>&1';
$ret = shell_exec($cmd);
echo $ret;
?>
This script works with the command line php test.php but it doesn't work with apache. If I take a look into the apache's process with htop, I can see that there are two process (with php test.php) :
xvfb
wkhtmltopdf
When I launch with apache I have only xvfb process. It finish by a timeout from apache because it's waiting the wkhtmltopdf process.
I can make it works with apache (2.2.21) and php (5.3.10).
Is there something that I'm missing ? Maybe something in the apache's config-files ?
I was having the same problem. I was using the exec function, but the same applies to shell_exec. The function execution was disabled in php.ini.
SOLUTION: Remove the shell_exec string from the disable_functions at php.ini file.
I am not sure why your second version is not callable from Apache (must not be using the same shell, since shell_exec uses a shell?), but as a work-around could you (from Apache PHP) shell_exec("php test.php"); and get your intended result?
Perhaps also try one of the other process execution functions such as pcntl_exec.
it's mostly because of ownership and permissions, try
su www-data (for debian)
php test.php
you'll probably see the error.

running a jar file in PHP

I have a command to be run like this
$command="java -jar ".dirname(__FILE__)."\gmksplit.jar"." ".$input_path." ".$output_path;
I have echoed the $command variable and I get the output as
java -jar X:\wamp\www\moodle\gmksplit.jar X:\wamp\www\moodle/upload/maze_4.gmk X:\wamp\www\moodle/outputs/maze_4;
which is exactly I want to run..
I am trying to run it as
echo $exec($command);
it is not running. I have tried all the functions like shell_exec() and system()
It gives the output as
Java Version: 10700 (1.7.0_01)
when i run the same line in command prompt I get the output as
Java Version: 10700 (1.7.0_01)
Loading lib files in X:\wamp\www\moodle\gmksplit.jar
01_move.lgl 02_main1.lgl 03_main2.lgl 04_control.lgl
05_score.lgl 06_extra.lgl 07_draw.lgl
time taken to load file: 254 ms
so, as you see my php code is giving only the first line as output. The command is not running properly and I am not getting the intended output.
please help me
I am using the wampp server
Can you try this:
<?php
$command="java -jar ".dirname(__FILE__)."\gmksplit.jar"." ".$input_path." ".$output_path;
$out = array();
exec = ($command, $out);
print_r($out);
?>

Categories