PDF2SWF: php exec() only works in terminal - php

Here is my code:
$pdf = '/Users/macbookpro/Desktop/q.pdf';
$swf = '/Users/macbookpro/Desktop/q.swf';
$command2 = 'pdf2swf -o '.$swf.' -T -z -t -f '.$pdf.' -s flashversion=9';
exec($command2,$out,$status);
var_dump($output);
The output is NULL and no SWF is generated. However, if I output the command and copy it to terminal, it works. How do I solve this?

exec runs as the user running the script. Apache user likely doesn't have the PATH variable telling it where to look for programs, so instead of
$command2 = 'pdf2swf -o '.$swf.' -T -z -t -f '.$pdf.' -s flashversion=9';
Try adding the location of pdf2swf, something like:
$command2 = '/bin/pdf2swf -o '.$swf.' -T -z -t -f '.$pdf.' -s flashversion=9';
And make sure that the apache user has permission to get to the executable, and permission to execute it.
chmod a+x /bin/pdf2swf
Of course replace /bin/ with where ever pdf2swf really lives for all the example code in this answer.

Related

use "wine" with php's shell_exec

I am currently trying to use a "wine" command from my PHP-Script.
If i execute this:
$shell = shell_exec("/usr/bin/wine --version");
All is working fine and WINE version is displayed in $shell.
But, if i try to make it like this:
$run = shell_exec("/usr/bin/wine ".$workdir."/bin/tool.exe -m ".$workdir."/bin/std.maps -a ".$workdir."/bin/alias.file -n ".$workdir."/files/".$project_name."/upload/dump.bin -o ".$workdir."/files/".$project_name."/maps/definitions.list");
Which results in:
$run = shell_exec("/usr/bin/wine /var/www/html/bin/tool.exe -m /var/www/html/bin/std.maps -a /var/www/html/bin/alias.file -n /var/www/html/files/1-59374-94700/upload/dump.bin -o /var/www/html/files/1-59374-94700/maps/definitions.list 2>&1");
I get the following output:
wine: chdir to /.wine : No such file or directory
What i am doing wrong? If i enter the command above directly to the shell, all is working fine. If i do it without /usr/bin/ in front of wine, the output's are the same.
Br, Chris
I haven't try it myself but I think it is because wine is run as different user with different environment settings. Try create .wine directory inside /var/www and make www-data user as owner of this directory and make /var/www/.wine HOME directory.
$run = shell_exec("HOME=/var/www/.wine /usr/bin/wine ".$workdir."/bin/tool.exe -m ".$workdir."/bin/std.maps -a ".$workdir."/bin/alias.file -n ".$workdir."/files/".$project_name."/upload/dump.bin -o ".$workdir."/files/".$project_name."/maps/definitions.list");

Run php server through shell_exec and save its output to variable

When I try to run shell_exec("php -S localhost:8000"), it runs the server but it freezes the terminal.
I tried running $result = shell_exec('php -S localhost:8000 -t public/ &> /dev/null 2>&1') but it doesn't store the output to the variable.
The idea is so that I can customize the output message once server successfully boots up.

Tail -f | Grep <regex> | php script.php <grep result>

Ok, so I have a ssh connection open to a remote server. I'm running a tail on the logs and if an ID shows up in the logs I need to do an insert into the database.
So I have my ssh tail working and I have it piping into my grep function which is giving me the IDs I need. The next step is that as those IDs are found it needs to immediately kick off a php script.
What I thought it would look like is:
ssh -t <user>#<host> "tail -f /APP/logs/foo.log" | grep -oh "'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'" | php myscript.php <grep result>
And yes my regex is horrible, wanted to use [0-9]{8}, but what I have gets the job done.
Any suggestions? Tried looking at -exec, awk, or any other tool. I could write the result to its own file and then read the new file, but that doesn't catch the streaming ids.
-=-=-=-=-EDIT-=-=-=-=-=-
So here is what I'm using:
ssh -t <user>#<host> "tail -f /APP/logs/foo.log" |grep "^javax.ejb.ObjectNotFoundException" |awk '/[0-9]/ { system("php myscript.php "$6) }'
And if I use tail -l #lines it works, or if after a while I ctrl-c, it then works. The behavior I wanted though was to as the tail got a bad ID to kick off the script to fix the bad ID. Not wait until an EOF or some tail buffer...
I'm having similar problem. There's something funny with tail -f and grep -o combination when ssh.
So on local server, if you do
tail -f myfile.log |grep -o keyword
It grep just fine.
But if you do it from remote server....
ssh user#server 'tail -f myfile.log |grep -o keyword'
doesn't work
But if you remove -f from tail or -o from grep, work just fine... weird :-/

PHP exec not executing command

I have used php's exec to execute FFmpeg command but its not woking when I open it in browser. But when i run this php file script in terminal it works fine.And my php safe mode is off. please help me to get it solved. my php code is
<?php
$output=exec("ffmpeg -f image2 -i /home/phedra/imgs/image/img%03d.png -r 12 -s 610x489 /home/phedra/imgs/video/out.avi", $out);
echo $out;
echo $output;
?>
try giving full path where the ffmpeg application is located.
e.g.
/usr/bin/ffmpeg
So your function might look like:
$output=exec("/usr/bin/ffmpeg -f image2 -i /home/phedra/imgs/image/img%03d.png -r 12 -s 610x489 /home/phedra/imgs/video/out.avi", $out);
You must check what is the location of "ffmpeg".
I had this problem and it turned out it was Apache's permission on the public directory.
Note: I am running Ubuntu 14 on AWS
After installing FFmpeg I had to change the /var/www/* ownership to www-data.
sudo chown -R www-data:root /var/www
(the www-data is the important part here)
Then I had the following code running, and it works when I access it via URL (Apache)
// test.php
$run = system("/opt/ffmpeg/bin/ffmpeg -i /var/www/html/input.mp4 -vf scale=640:480 /var/www/html/output.mp4 &");
if($run) {
echo "success";
} else {
echo "failed";
}
The /opt/ffmpeg/bin/ffmpeg is where my FFmpeg is running from. Yours might be /usr/bin/ffmpeg or something else. You can locate it by typing locate ffmpeg in the command line and looking through the list it gives you.
The input file was a public .mp4 file and the output.mp4 file was going to the same location.
Run this in your command line: php test.php - works
Run this from your browser: yourwebsite.com/test.php - works
Note that if you are on windows you must use COMMAS. I.E:
$output=exec('"/usr/bin/ffmpeg" -f image2 -i /home/phedra/imgs/image/img%03d.png -r 12 -s 610x489 /home/phedra/imgs/video/out.avi', $out);
Like #Arfeen mentioned in his answer, you should execute the command with the path of ffmpeg, but, the given path in the answer "/usr/bin/ffmpeg" is not always the same.
First locate your ffmpeg by using the command :
which ffmpeg
The result in my case is :
/usr/local/bin/ffmpeg
Then go back to your php code and replace "ffmpeg" in the command by the path of ffmpeg (which is /usr/local/bin/ffmpeg in my case).

How to launch unoconv from php file

I want to launch the command "unoconv" from a script php.
$command = '/usr/bin/unoconv --server localhost --port 2002 --format=pdf file.rtf >/dev/null 2>/dev/null';
$rc = system( $command );
echo $rc;
The command return no result and the file is not created.
I think is a problem from access with www-data and unoconv.
When I'm launching the command in shell, the file is created.
Any idea?
You can add command unoconv to sudoers.
I do this in this way:
I create wrapper bash script in for example /usr/local/bin where I have command unoconv.
#!/bin/bash
if [ -z "$1" ]; then
echo "Must pass file";
exit 10;
fi
/usr/bin/unoconv -f pdf $1.rtf
after this I adding entry in /etc/sudoers.d:
www-data ALL=NOPASSWD: /usr/local/bin/unoconv.sh
And now you can call script in php:
exec('sudo /usr/local/bin/unoconv.sh '.$fileName);
Try to run
$output = `/usr/bin/unoconv --server localhost --port 2002 --format=pdf file.rtf`;
instead and see error messages.
For me works like this:
$cmd = "/usr/bin/unoconv -f docx files/thefile";
shell_exec($cmd);
of course you have to do this previously (if you lounch your php script from the web):
chown -R www-data:www-data files/
I have found a solution to this problem when running Apache. You have to create the home folder for the www-data user
sudo mkdir /home/www-data
sudo chown www-data /home/www-data
Lastly we will have to edit the home directory and default shell for the www-data user
sudo vim /etc/passwd
For the entry of www-data the last two strings have to be replaced respectively with
/home/www-data
/bin/bash
Simple as this
$output = shell_exec('/opt/libreoffice5.0/program/python unoconv -f rtf test.html');
Edit the path to suite your configuration.
It just works!
You may be running into an issue with LibreOffice, OpenOffice or soffice not being able to write to the current user's $HOME directory.
By running the command below I was able to identify the correct $HOME directory and see the error that was being generated.
$cmd = 'echo $HOME & unoconv -vvvv --format %s --output %s %s 2>/tmp/unoconv.debug.txt';
exec($cmd);
The verbose output of $cmd will be generated written to the file: /tmp/unoconv.debug.txt.
In my case the output was:
Verbosity set to level 5
DEBUG: Connection type: socket,host=127.0.0.1,port=2002,tcpNoDelay=1;urp;StarOffice.ComponentContext
DEBUG: Existing listener not found.
DEBUG: Launching our own listener using /usr/lib64/libreoffice/program/soffice.bin.
Failed to connect to /usr/lib64/libreoffice/program/soffice.bin (pid=32012) in 6 seconds.
Connector : couldn't connect to socket (Success)
Error: Unable to connect or start own listener. Aborting.
The command ran seemed to fine as root, and as sudo -u nobody. On seeing this output I realized there was an issue with the home directory.
Kudos to Dag Wieers for his help - I'm hoping this helps other unoconv devs with their debugging.

Categories