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
Related
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...
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
I have a command in cmd which looks like below
C:\wamp\www\editor\DocTo-master\exe\docto -f C:\wamp\www\editor\uploaded\uploaded_files_21_original\AffidavitinDIR-4.docx -O "C:\wamp\www\editor\uploaded\uploaded_files_21_original\pdf\21.pdf" -T wdFormatPDF
When I run this in cmd, it works absolutely fine and gives output(doc to pdf) as expected.
However when I put the same command in php shell_exec like
shell_exec('C:\wamp\www\editor\DocTo-master\exe\docto -f C:\wamp\www\editor\uploaded\uploaded_files_21_original\AffidavitinDIR-4.docx -O "C:\wamp\www\editor\uploaded\uploaded_files_21_original\pdf\21.pdf" -T wdFormatPDF');
I dont get the desired output with the above code in php.
Any help will be appreciated
Seems like most of the problems have to do with permissions or redirection.
Your current user credentials allow you to create files in that directory but does the webserver username?
Redirect stderr to stdout to get error information.
Is the quoting right?
Look in the webserver error logs.
I am the author of docto.
This is most probably a permissions issue in that php is not necessarily given the correct permissions to run word on your server.
I am running this myself on a Windows 2012 server running Word 2010, it works fine. DocTo is installed on a subdirectory above the Webserver root.
PHP is running as NTSystem Authority
Also I am using exec rather than shell_exec not sure if that makes any differnce
For fix this problem you must create Desktop folder in these paths:
C:\Windows\SysWOW64\config\systemprofile\Desktop
C:\Windows\System32\config\systemprofile\Desktop
Then go to (Control Panel\Administrative Tools\Services) and right click on your apache service. go to "Log On" tab and enable "Allow service to interact with desktop" option.
Then reset your apache service.
http://pmlearning.info
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.
I am having difficulty with the PHP exec() function. It seems to not be calling certain functions. For instance, the code echo exec('ls'); produces no output whatsoever (it should, there are files in the directory). That main reason this is a problem for me is that I'm trying execute a .jar from a PHP exec() call.
As far as I know I'm calling the java program properly, but I'm not getting any of the output. The .jar can be executed from the command line on the server. (For the record, it's an apache server).
My php for the .jar execute looks like this:
$output = array();
exec('java -jar testJava.jar', $output);
print_r($output);
All I get for output from this exec() call is Array().
I have had success with exec() executing 'whoami' and 'pwd'. I can't figure out why some functions are working and some aren't. I'm not the most experienced person with PHP either, so I'm not too sure how to diagnose the issue. Any and all help would be appreciated.
The reason why you are not able to execute ls is because of permissions.
If you are running the web server as user A , then you can only ls only those directories which have permissions for user A.
You can either change the permission of the directory or you can change the user under which the server is running by changing the httpd.conf file(i am assuming that you are using apache).
If you are changing the permissions of the directory, then make sure that you change permissions of parent directories also.
To change the web server user, follow following steps:
Open the following file:
vi /etc/httpd/conf/httpd.conf
Search for
User apache
Group apache
Change the user and group name. After changing the user and group, restart the server using following command.
/sbin/service httpd restart
Then you will be able to execute all commands which can be run by that user.
EDIT:
The 'User' should be a non-root user in httpd.conf. Apache by default doesnot serve pages when run as root. You have to set user as a non-root user or else you will get error.
If you want to force apache to run as root, then you have to set a environment variable as below:
env CFLAGS=-DBIG_SECURITY_HOLE
Then you have to rebuild apache before you can run it as root.
I have found the issue - SELinux was blocking PHP from accessing certain functions. Putting SELinux into permissive mode has fixed the issues (although, I'd rather not have to leave SELinux in permissive mode; I'd rather find a way of allowing certain functions if I can).
I have a solution:
command runs from console, but not from php via exec/system/passthru.
The issue is the path to command. It works with the absolute path to command
So that:
wkhtmltopdf "htm1Eufn7.htm" "pdfIZrNcb.pdf"
becomes:
/usr/local/bin/wkhtmltopdf "htm1Eufn7.htm" "pdfIZrNcb.pdf"
And now, it's works from php via exec
Where command binary you can see via whereis wkhtmltopdf
Tore my hair out trying to work out why PHP exec works from command line but not from Apache. At the end, I found the following permissions:
***getsebool -a | grep httpd*** ---->
**httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off**
USE: setsebool -P httpd_ssi_exec 1
SEE: https://linux.die.net/man/8/httpd_selinux
Your problem is not an execution issue but the syntax of the exec command. The second argument is always returned as an array and contains a single line of the output in each index. The return value of the exec function will contain the final line of the commands output. To show the output you can use:
foreach($output as $line) echo "$line\n";
See http://php.net/manual/en/function.exec.php for details. You can also get the command's exit value with a third argument.