This works in the command prompt(in one line) :
cd C:\Program Files (x86)\LibreOffice 4\program &&
"C:\Program Files (x86)\LibreOffice 4\program\python.exe" unoconv -f pdf d:\class1.doc
but when it comes to do the same in PHP's exec() nothing happens - neither message nor any file, probably due to a syntax error :
echo exec('cd C:\\Program Files (x86)\\LibreOffice 4\\program &&
"C:\\Program Files (x86)\\LibreOffice 4\\program\\python.exe" unoconv -f pdf d:\\class1.doc');
Once you have cd into a dir, no need to call the executable within it from the full path again. you can call it directly.
Try this:
<?php
// Test
$ret = exec('cd "C:\Program Files (x86)\LibreOffice 4\program\" && python.exe unoconv -f pdf d:\\class1.doc', $output, $error);
// Debug
var_dump($ret);
var_dump($output);
var_dump($error);
?>
Take a look at the documentation here: http://us1.php.net/function.exec
Related
When I run this phpscript in my mac xampp it return successful but not pdf file is generated. But when I run from mac terminal it able to create the pdf file from the docx file.
<?php
$source_file = 'c++ documentation.docx';
$cmd = "unoconv/0.7/bin/unoconv";
$command = sprintf("/Applications/XAMPP/xamppfiles/htdocs/phpdocx/unoconv/0.7/bin/unoconv -h -v -f pdf -o /phpdocx/docimages/testing.pdf %s 2>&1",
escapeshellarg($source_file));
echo shell_exec($command);
echo "yoyo";
if(shell_exec($command)){
echo "successful";
}else{
echo "failed";
}
?>
This is my edited code and it said sprintf(): Too few argument!
You need to escape the shell arguments, otherwise the shell may interpret its special characters:
$source_file = 'c++ documentation.docx';
$command = sprintf("unoconv/0.7/bin/unoconv -h -v -f pdf -o /phpdocx/docimages/testing.pdf %s 2>&1",
escapeshellarg($source_file));
Provide full (absolute) path to your unoconv/0.7/bin/unoconv executable, as it seems unavailable from your $PATH environment variable:
$command = sprintf("/path/to/unoconv/0.7/bin/unoconv -h -v -f pdf -o /phpdocx/docimages/testing.pdf %s 2>&1",
escapeshellarg($source_file));
Alternatively, include the absolute path to unoconv/0.7/bin/ directory into the $PATH environment variable before calling the shell. Refer to this post, for instance.
Obviously, the $source_file should be in the current directory. Otherwise, the command will fail to access it.
I am triggering an executable using php
$commstr = '/abs/path/to/binary > status.txt &';
echo "Running command: ".$commstr."\n";
shell_exec($commstr);
PHP exec() or for that matter system() or shell_exec() works fine because status.txt gets written to, but cat status.txt says error while loading shared libraries: libQtCore.so.4: cannot open shared object file: No such file or directory. This means the binary doesn't get executed
I tried changing my php code to the following without any luck
$commstr = 'export LD_LIBRARY_PATH=/path/to/lib ; /abs/path/to/binary > status.txt &';
The binary is readable by user apache and so are the libraries.
Try running the command as sudo.
for example
$commstr = 'sudo export LD_LIBRARY_PATH=/path/to/lib ; /abs/path/to/binary > status.txt &';
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).
Is there any way to execute a makefile in a php file? I have tried:
exec('cmd /c "C:\\Program Files\\Microsoft Visual Studio\\VC98\\Bin\\nmake.exe" -f E:\\dev\\temp.mak > process.out 2> process.err < /dev/null &');
But I donot think this way makefile gets to run.
Why not doing :
$make = escapeshellarg("C:\Program Files\Microsoft Visual Studio\VC98\Bin\nmake.exe");
$path = escapeshellarg("E:\dev\temp.mak");
exec("start /B {$make} -f {$path} > process.out 2> process.err");
start /B will execute your program in background
> process.out will redirect standard output to "process.out" file
2> process.err will redirect error output to "process.err" file
In this example, process.out and process.err will be erased each time make is run. To avoid this behaviour, just replace > symbols by >>, and files will be appended.
Try different methods of doing this, Create a windows batch file in the same place as your PHP directory;
cd C:\"Program Files"\"Microsoft Visual Studio"\VC98\Bin
nmake.exe -f E:\dev\temp.mak > process.out 2> process.err
*Incorporating Zids Comment into this: *
How would you execute it from the command line, if you were not using PHP? – rid
If the above method doesn't work. Search online for usage of that exe by running it from windows command prompt, then change the .batch file accordingly.*
Save this as a .batch file, then from your PHP try running
exec ("filename.bat", $output);
then
View the output performed by the exec command in a simple foreach loop
foreach ($output AS $OutputStr)
{
echo $OutputStr."<br>";
}
There should be some output, from the output I would work with that.
I'm trying to use LibreOffice for converting a spreadsheet to another format, when I execute the command from the console it works fine but when I do it from PHP using exec() or system() it doesn't work.
It doesn't show any error or anything, it simply silently fails, if I try executing some simple command like "ls" it works just fine.
This is the command I'm using:
<?php
system("libreoffice --headless -convert-to ooxml '/home/www/path_to_app/file.xlsx' -outdir /home/www/path_to_app/");
I already tried changing the apache User and Group on /opt/lampp/etc/httpd.conf to the same logged in user I am.
I'm wondering if the problem is that the www folder is /home instead of inside my user and that causes permissions problems, but so far couldn't make it work.
Any help will be appreciated.
I solved this problem with the command below :
system('
export HOME=/tmp
libreoffice --headless --invisible --norestore --convert-to pdf --outdir /my/folder /my/file/calc.xls');
I solved this problem with the command below :
exec('libreoffice --headless --invisible --norestore --convert-to pdf --outdir /my/folder /my/file/calc.xls');
Just check rights, libreoffice uses user home folder, If you use default settings for Ubuntu then PHP runs oenter code heren www-data rights and it's home is /var/www/, but default this directory is owned by root.enter code here
In PHP - you can see rigths:
exec('whoami', $arr);
print_r($arr);
In console - You can see your HOME and give correct rights:
su wwww-data
echo $HOME
exit;
chown www-data:www-data /var/www -R
Notwithstanding your $PATH, which drew010 alluded to, I wouldn't do it this way.
LibreOffice is a pretty big program, has lots of code you don't know about, it generates and updates files in a directory in your $HOME, and is certainly not something you're going to be able to run more than one copy of at a time.
So instead of having LibreOffice launched by your web server, and subverting Apache's security by running it as a more privileged user than "www-data" or "nobody", you should make a handler.
First off, verify that you can run your libreoffice ... command line from a terminal. To be extra sure that you don't have any X11 dependencies, run unset DISPLAY (for bash) or unsetenv DISPLAY (for tcsh) in your xterm before you test your command line. Does it break? Fix that problem first. Does it work? Great, then proceed with a handler.
Your handler, in its simplest form, can be a script that loops forever, checking for "files to handle" in a spool directory, and if it finds them, converts them using libreoffice and puts the resultant file where it can be found by your web app.
#!/bin/sh
while sleep 10; do
if [ `ls /var/tmp/myspool/ | grep -c '\.xlsx$'` -gt 0 ]; then
ls /var/tmp/myspool/*.xlsx | while read file; do
/usr/local/bin/libreoffice --headless -convert-to ooxml "$file" yadda yadda
if [ $? = 0 ]; then
mv "$file" "/var/tmp/myspool/done/
fi
done
fi
done
If you don't want the overhead of something "polling" (checking the spool directory every 10 seconds), then you could have your PHP script add a line to a log that gets watched by your handler. For example:
<?php
// process form, save file to spool dir
syslog(LOG_NOTICE, "Saved: " . $filename);
?>
Make sure you've configured syslog to store these messages in, say, /var/log/filelog, then your handler can just tail the log.
#!/bin/sh
tail -F /var/log/filelog | while read line; do
filename="`echo \"$line\" | sed 's/.*Saved: //'`"
/usr/local/bin/libreoffice --headless -convert-to ooxml "$file" yadda yadda
# etc ... error handling and mv as in the other script
done
Get the idea?
I also faced same problem before.....
This solution seems worked for me...
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
$mainFile = "YOUR FILE";
$cmd2 = "export HOME=/tmp
libreoffice --headless --invisible --norestore --convert-to pdf " .
"'$mainFile'";
$saved = getenv("LD_LIBRARY_PATH"); // save old value
$newld = "/usr/lib"; // extra paths to add
if ($saved) {
putenv("LD_LIBRARY_PATH=" . $newld);
}
// set new value
//
// command is loaded using
// libs in the new path list
execInBackground($cmd2);
putenv("LD_LIBRARY_PATH=$saved"); // restore old value