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.
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 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');
I m trying to execute batch file in php file. I m using Apache server. Itried following ways but its not working
PHP Code
echo shell_exec('download.bat');
echo exec('download.bat');
system ("cmd /c download.bat");//Also tried for exec and shell_exec
Batch file contains downloading code using ftp client
Batch file
"c:\program files\coreftp\coreftp.exe" -s -O -site mysite -d /Export/*.* -p C:\wamp\www\file\txt
If I run it in cmd or run directly then its works fine when I run its in php its just write or echo batch file's code
download.bat file is in same folder.
I also tried to call simple bat file
start "link" "https://www.google.co.in/?gfe_rd=cr&ei=NzuIVI-FG6aG8Qef44CAAw"
Its also not calling to this bat file
Are you sure that your 'download.bat' file is in the same PATH as your PHP script?
Try to use absolute path like this
exec('C:\\MY\\PATH\\TO\\download.bat');
Just use exec('download.bat'); if the file is in the same directory, however you need to ensure that Apache has correct permissions to execute the batch file i.e. it should be running on an Administrator account. If you're on Win7 or above, look at how to run programs in elevated mode.
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();
?>
HI there!
I've run into some problem while learning to combine .sh files and PHP. I've create a file test.sh and in that file I call a PHP file called test.php.
If I double click on the .sh file then it runs perfectly but when I try to run it from the terminal I get "command not found". I'm in the exact folder as my .sh file but it wont work. Here's my test.sh:
#!/bin/bash
LIB=${0/%cli/}
exec php -q ${LIB}test.php one two three
exit;
When I doubleclick on the test.sh file then it returns the argv array like it suppost to. But why can't I run it from terminal?
use ./filename.sh
no matter if your are in the same folder or not, without giving ./ the system is searching the path variable. (/bin/, /usr/bin and so on)
Is execute bit enabled?
chmod +x test.sh
Your $PATH variable may not include '.' - so the shell may not be able to find the command to run.
As others have said, sh ./file.sh will work...
It is possible that your environment is different when launching from the Terminal and when launching via a double-click. Try executing which php and echo $PATH from the Terminal to see what the deal is.
EDIT 1
As others have noted, if your "command not found" is referring to the shell script and not php, then you probably forgot to include the "./" before the name of the script (i.e. ./test.sh). Also, don't forget to make it executable by invoking chmod a+x test.sh. The reason for this is that PATH does not include the current directory (i.e. "."), because doing so would be a security risk (e.g. folders with a ton of files in them including a fake "ssh" which could then intercept your password or the like).
EDIT 2
Also, I don't know about you, but ${0/%cli/} is evaluating to -bash from within my Terminal. Are you sure that's what you wanted it to expand to? Perhaps you should specify the exact filename.
Another option is to run it with sh (or bash, if sh on your machine isn't bash and the script uses bashims)
sh filename.sh
bash filename.sh
This will work whether or not the file is executable or in $PATH