I am trying to run a bash script from an html file using php. I've read this question (how to run a .sh file from php?) and followed it exactly but cannot seem to get it to work.
I have two files in my downloads folder on my mac. These are the file names and their content:
hello.sh
#! /bin/bash
echo hello world
test.php
<?php
echo shell_exec('sh /Users/steve/downloads/hello.sh');
?>
I ran this command in command line while in the downloads folder:
php test.php
This resulted in a blank page being opened in chrome while I was expecting to get a page with "hello word" in it.
I am also wondering how to get my php code to open a window in chrome like html files do. For example, if I enter this into command line:
open test.html
a new window is opened in chrome running the content of test.html.
Thanks for the help.
Here is an example of how you can create a .php file named "test.php" that runs a shell script named "hello.txt".
This is the code that goes inside "test.php". You can put this code in any directory and it should work fine.
<?php
echo shell_exec('./hello.sh');
?>
This is the code that goes inside "hello.sh". You can put this code in any directory so long as it is in the same directory as "test.php".
#! /bin/bash
echo hello world
Once you have made both these files, go into their directory using command line and enter the following commands:
chmod +x hello.sh
php test.php
The first command (I think) makes "hello.sh" accessible from "test.php". You only need to do this once. The second command runs test.php and should return "hello world".
This can also be used with programs other than shell scripts. For example you could compile a cpp file into a.out (the default name of a compiled c++ file) and run it from the php file by replacing echo shell_exec('./hello.sh'); with echo shell_exec('./a.out');
Related
Anyone can assist me on how to execute my php files without preceding php front
Examples instead of typing 'php filename.php' in cli or cloud9
I want to execute them by just typing the file name
command: 'filename'
then the script runs
Well i was thinking of using the htaccess file but i'm not sure if that also works on vps or cli. I know of browser only
Edit your PHP script and add this as the new first line, by itself, before your opening <?php tag:
#!/path/to/php
(Replace that with your actual path.)
Alternatively, if you have the env command, you can use that to figure it out for you:
#!/usr/bin/env php
So your script looks like this:
#!/usr/bin/env php
<?php
echo "Hello, world!\n";
Then make the file exectuable:
chmod u+x filename
Now you can just "run" it directly in the shell:
% filename
Hello, world!
Note, you only want to do this if the script is CLI-only. If it's ever run via the web server, that first line will be output to the browser.
I am trying a really simple thing in PHP. I want to execute an external bat file from PHP script, which is invoked from web server (Apache with WAMP). Following is the content of bat file named savereport.bat
echo %PATH% > C:\project\mypath.txt
cp D:\books\Ant\1.pdf D:\books\Ant\2.pdf
The bat file when executed manually works fine, it creates mypath.txt in C:\project directory and 1.pdf is copied to 2.pdf correctly.
However, when I run the same bat file from PHP with system(), only the first statement completes, which means it does create C:\project\mypath.txt, but strangely it does not copy 1.pdf to 2.pdf.
This is the PHP code I am using
<?php
system('C:\wamp\www\savereport.bat');
?>
I have also tried exec(), passthru, same result in all the case. Please help.
cp is not a windows command, it's probably a Powershell alias, replace the cp with copy.
I am trying to run a bash script from an html file using php. I've read this question (how to run a .sh file from php?) and followed it exactly but cannot seem to get it to work.
I have two files in my downloads folder on my mac. These are the file names and their content:
hello.sh
echo hello world
test.html
<?php
echo shell_exec('sh /Users/fred/downloads/thing.sh');
?>
I ran this command in command line while in the downloads folder:
open test.html
This resulted in a blank page being opened in chrome while I was expecting to get a page with "hello word" in it.
Thanks for the help.
rewrite your bash file in nano as
#! /bin/bash
echo "hello world"
In the command line type "bash hello.sh" to execute, this will test if it is working or not.
test.html rewrite to test.php
php code only work with *.php extension.
rename 'test.html' to 'test.php'.
also check the file name here
echo shell_exec('sh /Users/fred/downloads/thing.sh');
i think this should be
echo shell_exec('sh /Users/fred/downloads/hello.sh');
If again you face the same issue then may be your apache server doesn't have permission to access 'hello.sh'.
I have a form in html page that has action to run php file. The php file has to run a bash function command which in turn runs a scrapy spider. Since I have the scrapy spider located outside var/www/, I have added a function in .bashrc to run the bash command ($ startscript) from anywhere. When I run it on the terminal from var/www folder or anywhere it works as expected but when I do it in php file it does not work. I am not sure if its because of php file permission, scrapy proprieties or something else.
Any suggestions?
.php file:
<?php
$output = shell_exec('startscript');
echo $output;
?>
.bashrc file:
function startscript
{
cd /home/pi/IndeedCoUkCrawl
./BotScrapy.sh
}
So what you can try:
put the absolute path of the startscript in shell_exec and see if it works.
try running with error_reporting(E_ALL) and see if this give any error/notice
check if your webserver has permissions to run the file. Try putting the file into the same group as the webserver runs in.
I have 2 php files. One which stores various xml files in a database and other php files takes those xml files as input, parse them and produces a another table having various information about the xml files.
I want this work to be scheduled for everynight.
I want to do this via batch files but I have no idea how to use .bat files, and how to incoorporate the php script in it.
Need help/guidance.
Thanks.
Yogesh
I assume you are doing this on a windows machine as you mention a .bat file
PHP can be executed from a command line e.g. a dos shell
http://www.php.net/manual/en/features.commandline.introduction.php
Once you get it working manually just put what you are typing in a text file. (.bat)
You can execute the .bat file (a program file to windows) when you want using a scheduler, e.g. http://windows.microsoft.com/en-GB/windows7/schedule-a-task
create start.sh add this code:
#!/bin/bash
php /path/to/folder/script.php
chmod 644 script.php
OR
create start.sh add this code:
#!/bin/bash
/path/to/folder/script.php
chmod 755 script.php
the first line of your script.php has to be:
#!/usr/bin/php