I cannot seem to get this to work:
shell_exec("/anaconda/bin/scrapy crawl script_v5 -a calln=D5 -o output_D5.csv");
I suspect commands manually installed don't work.
In addition, I was trying to dump the $PATH to know where it searches, but
shell_exec("echo $PATH");
returns NULL.
Why is this so, and how do I solve it?
By the way, I ran the script from a browser calling to localhost where MAMP is running.
Most of the server disabled this function because of high-risk security issue
shell_exec();
so you should read the file and then get your output.
using
fopen()
Related
I am trying to have a PHP file call a Windows batch script using
exec("runPy.bat");
I have also tried
exec("cmd /c \\server\somePath\runPy.bat");
It doesn't matter what I try, the batch script doesn't work. And I don't get any errors. After much research I found a post saying that the problem may be due to privaleges: https://stackoverflow.com/a/11613662
Can anyone show me safe/proper way to set up permissions so that I can call batch scripts in IIS? Or is it unsafe in general to do that? Using IIS8 if that helps.
To run batch, you need to use cmd, which you can call via:
system("cmd /c C:[path to file]");
I am trying to download the contents of an html file to my Ubuntu Linux 16.04 computer using php's get_file_contents() function. However, when I do this, I get this Warning: "failed to open stream: the , aborting"
Yet when I use wget on the terminal command line, it quickly downloads the file contents.
So why does file_get_contents not work for this? Here is my php code, which produces the Warning:
$testDownload = file_get_contents("https://ebird.org/region/US-AL-001?yr=all");
echo $testDownload;
On my Ubuntu terminal command line, here is my bash code, which works quickly and flawlessly:
wget https://ebird.org/region/US-AL-001?yr=all
I want to use php because I want to automate the downloading of a number of files and need a fair bit of code to do it, and I feel much more comfortable using php than bash.
P.S. I tried various "context" solutions for the file_get_contents function that were suggested on Stack Overflow, but they did not solve the problem.
P.P.S. I earlier tried cURL and got the same redirects Warning, though I admit to not knowing much about cURL.
I found a solution: the shell_exec() function in php allows me to use bash's wget command line function within my php script. I tried it and it worked. (I will have to change the ownership of the downloaded files to get access to them.) Here is the code that worked:
$output = shell_exec('wget https://ebird.org/region/US-AL-005?yr=all');
I still don't understand why wget can get the file contents but file_get_contents cannot. But with shell_exec() I have found a php solution to complete my task, so I am happy.
I'm currently using the Pygments for PHP plugin that is located here: http://derek.simkowiak.net/pygments-for-php/.
The line that actually calls Pygments from that code is an exec() passed: pygmentize -f html $extra_opts -l $language $temp_name as the command. This all works fine, and I get back the output and it is formatted by the plugin.
What I would like to happen at the same time is for Pygments to create an image of it, so I pass exec() a similar command: pygmentize -f png $extra_opts -l $language -o $full_image_path/$output_file.png $temp_name This is where I run into a problem. The image never shows up in the expected folder.
However, if I var_dump() that command string before I exec() it and take it and run it straight from the command line, it works fine.
I have tried echoing exec('whoami') which tells me that the PHP user is www-data. I've tried giving permissions to www-data and changing ownership to www-data on the folder where I store the images. I've also tried changing permissions to 777 just to see what would happen, and the answer is nothing.
Is there something I'm missing? I'm running out of ideas to try. Thank you!
Edit: Another thing that I've checked is the output from the exec command, and the return value. It outputs an empty array, and it returns 1 as the return value.
Edit 2: After seeing that that directory should be writeable/readable for the PHP user, is it possible that pygments doesn't have permission to write it as a specific user? I'm not sure this makes sense, as when I run it myself it works fine, and in fact, when PHP runs it with the HTML lexer, it is able to run. I'm not very experienced in Python, so I don't know if this is a potential issue.
I guess you cannot do it like this.
$output_file.png
Try
$file = $output_file.".png"
and substitute in the exec
Ended up being an issue with the font that was installed for use by the www-root user. Apparently the one that is used by default for Pygments was installed only for the user that I was running as when I use the command line.
The way I was able to figure this out, was running
exec("$command 2>&1", $out, $code);.
The extra 2>&1 redirects stderr into the output for me to see the issue.
The $out parameter showed the FontNotFound error that pygments was throwing.
I changed the font that Pygments used via the command line using: full,style=manni,cssclass=pygmentize_kbOKBd,font_name='DejaVu Sans Mono' -l php -o /srv/www/path/to/images/uploads/2513732976ad4b7.02729290.png /tmp/pygmentize_kbOKBd after finding which fonts I had available to me.
To see which fonts I had available to me as the user running the script, I just ran fc-list in an exec() command for Ubuntu, and checked the output of that for the list of available fonts.
I have a problem trying to run passthru function in my php code (Joomla module). the code is following (this is only a snippet)
ob_start();
passthru("/usr/bin/whois 85.70.231.130 | /usr/bin/grep 'address:'",$code);
$whoisData = ob_get_contents();
ob_end_clean();
$whoisData = str_replace("address:", "", $whoisData);
$whoisArray = split("\n",$whoisData);
echo trim($whoisArray[1]);
when I run this on my localhost, it echoes what it should, but when I run this code on the production server, it echoes nothing and the $code variable contains 127 (command not found). I tryied add absolute paths to these commands into the passthru function, but it didn't helped. Interesting is, that when I run the code right from terminal via ssh and php command, it runs well, but when it's called from application context it doesn't. Does anybody know what I should to do?thanks
SOME EDITS..
safe_mode is on
webserver does not see into /usr/bin and /bin/ folders so what is the best way how to run these commands from php?
usr/bin/grep doesn't look like a valid path to a command.
The missing / at the beginning of the path to the second command might explain the command not found error... even if the first whois command is found.
Have you looked to see if your webserver / php is running chrooted?
print_r(glob('/*'));
if (file_exists('/usr/bin/grep') && file_exists('/usr/bin/whois')) {
print "maybe its a permissions thing?\n";
} else {
print "can't see executables required\n";
}
should give you a clue.
So I have already solved my problem with phpwhois library. I seems like with my server configuration is it unlikely that these functions will be working well. So thanks for your help:)
I tried googling for this issue and found many people with the same problem but no solution.
$result = exec("C:\\Ruby191\\bin\\lessc.bat less\\$file", $output);
Here result is an empty string and output an empty array. Same thing with:
$result = exec("cmd /c C:\\Ruby191\\bin\\lessc.bat less\\$file", $output);
I am sure the path is correct; I am sure exec() is enabled.
I tried exec, shell_exec, system and none work.
lessc is less CSS.
EDIT
The apache error log says:
'"ruby.exe"' is not recognized as an
internal or external command, operable
program or batch file.
I found out the problem; I installed ruby just for this and added it to my PATH environment variable, and it started working immediately for command prompts.
However it didn't work for PHP, so I decided to restart my computer. After that it worked.
First chdir("C:\Ruby191\bin\"), then run lessc.bat.