Exec Linux command as user in PHP - php

I'm converting a document file to html. I've installed LibreOffice on Linux server.
Here's the command,
$cmd = "libreoffice4.1 --headless -convert-to html /var/www/html/CV.doc -outdir /var/www/html/";
This command is working fine when I execute directly on the Linux server but when I tried to execute it via PHP it will not convert.
exec($cmd,$out,$err);
The $err is printing 127.
Kindly, let me know what I'm doing wrong here.

You say this:
This command is working fine when I execute directly on the Linux
server but when I tried to execute it via PHP it will not convert.
And your command is this:
$cmd = "libreoffice4.1 --headless -convert-to html /var/www/html/CV.doc -outdir /var/www/html/";
It could be that as a user logged into the system the path to libreoffice4.1 is part of your default user path. But when Apache (I assume) is running PHP it does not know where the libreoffice4.1 binary is.
So my solution would be to include the full path to the file in your command like this:
$cmd = "/full/path/to/this/binary/libreoffice4.1 --headless -convert-to html /var/www/html/CV.doc -outdir /var/www/html/";
Of course replacing /full/path/to/this/binary/ with the actual path to the file.
You can easily determine the full path to the binary by logging into your sever & typing in the following command:
which libreoffice4.1
The returned output should be the full path your libreoffice4.1 binary file. The path should be something like this:
/usr/bin/libreoffice4.1
Or this:
/usr/local/bin/libreoffice4.1
But the which command on your system will return the full path to the file, so use that output as your guide.

The most common reason for this is that the $PATH environment variable hasn't been populated to your PHP application. In this case use the full path to libreoffice:
$cmd = "/usr/bin/libreoffice4.1 --headless ... ";
The path /usr/bin may differ on your system. Type which libreoffice4.1 in terminal to find it out.
Another reason for the problem might be insufficient permissions to write files into the target directory or read the original documents. In this case make sure that the php application has proper permissions to do so. !root permissions must not being required for that! This would be a security flaw in your application.

Related

I can't find the format file `pdflatex.fmt'!

I want to run pdflatex from php on user request.
Running from terminal works fine [php file.php] and creates pdf file. But when I try to run from browser by sending request to php, It gives this error "I can't find the format file `pdflatex.fmt'!"
I am working with php5.2.6, OS is Fedora 9, and safe_mode is turned off.
Code -
$cmd = "/usr/bin/pdflatex /home/deep/public_html/resume/Folder/deependra.tex 2>&1";
$output = exec($cmd);
echo $output;
the pdflatex.fmt must be readable by the web browser in order to be accessible for the exec command, also the server might be chrooted and if so, the chroot folder might not have access to all the pdflatex parts. check it...

wkhtmltopdf not working in php script

I would like to use the wkhtmltopdf for HTML to PDF conversion.
When I have tried to convert it via linux terminal, it works fine.
But when I have tried with the php script it does not work.
I am trying execute the binary directly.
here is the code I am trying with PHP.
exec('/home/binary_loc/wkhtmltopdf http://www.google.com /home/user/output.pdf');
My binary is at the same folder where "index.php" exist.
I have tried to fetch the version of wkhtmltopdf binary with PHP, then it return the version.
But i don't able to understand why not it work to execute with php for pdf.
Here is code for version check using php.
error_reporting(E_ALL);
ini_set('display_errors', '1');
$cmd = "./wkhtmltopdf --version";
$t = shell_exec($cmd);
echo $t;
exit()
Do anyone has solution regarding it??
I want this because this will work in the shared hosting too. No need to install the wkhtmltopdf in the server.
Exec probably doesn't have permission to execute the file. PHP usually runs as either apache or nobody, rather than your user account. You'll have to make sure the execute bit is set for whatever user it runs under. You can use chmod 755 wkhtmltopdf from the directory it's under, to grant read and execute to all users.
Note that on some shared hosting, exec() is disabled. Check with your host to make sure you have access to it.
Define the complete path to the wkhtmltopdf executable and the complete path to the output folder.
To debug try something like this:
shell_exec("/home/binary_loc/wkhtmltopdf http://www.google.com /home/user/output.pdf > /home/user/debug.log 2>&1");
pipe the error in a file

Cannot execute command using exec() in php

Ive tried searching for the answer but cant find anything proper in relation to my case.
What i want to execute is very simple...its a call to the SPMF framework jar file.
here is the code -
<?php
exec('java -jar spmf.jar run Apriori /opt/lampp/htdocs/rrugd/ip.txt /opt/lampp/htdocs/rrugd/output.txt %20');
?>
why is this not executing?
it obviously runs perfectly using cli.
the folder having all the files (jar file,ip.txt,output.txt) have permissions set to r/w to all users
I am on Linux - Ubuntu 13.10
I found the solution.
The problem was in the path to the spmf zip file.
Either it needs a full path or should be in the same folder as the script.
I solved it by placing it in the folder same as the script.( under htdocs as i am using lampp)
Also, the folder needs to have permissions for other to be set to "Create and Delete" for php to be able to access and write output to the folder.
Basic debugging: If exec() doesn't work, then you need to check return values:
exec('some command here', $output, $return_var);
var_dump($output, $return_var);
NEVER assume that an external resource succeeded. ALWAYS check return values and output for error messages.
Most likely java isn't in the path of whatever shell PHP is using to handle the exec() call, meaning you'll need exec('/full/path/to/java ...') instead.

Exec command doesn't work as expected

I'm trying to launch a CLI command from a PHP script:
in particular I wanna use this command convert a.png a.tif to convert an image to tiff.
When I launch this command from CLI it works as expected but if I launch from a PHP script with the following code it doesn't create any tiff image in my folder:
$exec = "convert a.png a.tif";
exec($exec,$yaks,$err);
echo "<pre>";
print_r($yaks);
echo "$err";
echo "</pre>";
Moreover $yaks is empty and $err is set to 127.
I'm not an expert, why this doesn't work as expected?
Best regards
UPDATE
I used this command instead $exec = "convert 4.png 4.tif 2>&1"; and I got in return [0] => sh: convert: command not found
This seems to me strange since I can use it from CLI!
FINAL UPDATE
Thanks a lot guys!
$exec = "/usr/local/bin/convert a.png a.tif";
This command solved the problem!
You're great.
you should enter fullpath to "convert" and may be files.
err 127 - file not found
It looks like the 'convert' binary isn't in any of the directories on the PATH PHP is using. You could try using the full path, e.g. /opt/local/bin/convert or whatever the path is.
You could also modify the PATH used by PHP (but I don't know how).
The PHP script probably doesn't know where to find these things you're referring to in the exec command. When you run this from the command line, the shell will look for them in the directory you are in at that point in time; but when you run it from PHP, it probably defaults to the PHP dir and not the specific dir in which your files are. So write out the full path.

Can't get shell_exec to download a file

I have a multithreaded cli downloader for ubuntu called Aria2c.
I've been trying to get a php script to run aria2c and download a file using shell_exec, but i can't seem to get it to work. Ultimately I plan to have an input box on a page where I can enter a link and aria would download it.
Here's the code I've come up with (for now im inputting the link manually):
<?php $dl = shell_exec('aria2c -d /home/user/ www.downloadlink.com'); ?>
Note that the aria2c command I specified works well in the shell; and the directory I'm attempting to download to is set to '777'.
I'm baffled as to why it's not working, any ideas?
PS: I prefer to use aria rather than the alternatives because it is multithreaded and it supports cookies.
Check if PHP is running in safe_mode. shell_exec won't work if safe_mode is on.
EDIT: aria2c was not referenced with a full path. Referencing it like this: shell_exec('/path/to/aria2c -d /home/user/ www.downloadlink.com') works.
I'll make the assumption that you are running PHP through a web server. In such case, it's very unlikely that the web server has permission to write into your user's home directory: Apache runs as daemon with the credentials of a limited user. Also, the PATH env variable in Apache is not necessarily the same as your user's PATH. Last but not least, you don't check the return value or the script output.
Try something like this:
<?php
exec('/path/to/aria2c -d /tmp www.downloadlink.com', $output, $return_var);
var_dump($output, $return_var);
?>
You can get the full path for aria2c with:
which aria2c

Categories