Running a bash script in a webpage using php - php

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'.

Related

execute php files without typing php in front

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.

Running a bash script using php

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');

php script to run bash script which runs scrapy crawler

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.

Running a .php file from the command line?

I want run a .php file in the command line, but I can’t.
When I get to the file and want to run was opened with IDE:
>cd c:\wamp\www\GXC-CMS-2-master\GXC-CMS-2-master
core\cms\cms.php
Should certain system settings to do?
You should do it like this:
C:\path\to\php.exe file.php

running php script in filezilla

I am using fileZilla ftp server and trying to run my php scipt. Below code to call b.php is not working. I am new to filezilla so let me know if any configuration required to run this code. I am calling b.php from a.php and both of them are in same folder.
<?php
header("Location: b.php");
?>
It will not work like that. You can run PHP file via terminal:
$ php -f file.php
Or you can make it executable with $ chmod +x file.php, add #!/usr/bin/env php on top of file and, finally, you run like that:
$ ./file.php
Or just access it in browser if you have Web Server configured and running.
This code may help you...
Try to write like this
<?php
header('Location: b.php');
exit();
?>

Categories