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.
Related
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.
How can I create a scheduled task to run a PHP file?
Yes, I filled out everything in the scheduled task, but it still doesn't work.
Run: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website\save.php"
Start in: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website"
It just opens the PHP file in Notepad.
I gave the correct user name and pwd.
Please help me..
The Run command should be
C:\Path\to\php.exe -f "C:\Path\to\file.php"
From the command line help of php.exe:
-f Parse and execute <file>.
I just wrote a .bat file that does the work file.bat
#ECHO OFF
php.exe -f "C:\code\cust.php"
And put it in the Schedule Tasks like this:
Run: C:\code\file.bat
Start in: C:\code\
If you running php scripts, in most of cases script awaiting to be runned in current folder.
That mean you must set up folder each your action, use field "Start In".
Example:
Run: C:\php\php.exe
Arguments: -f C:\web\myscript.php
Do not forget:
Start in: C:\web\
Sounds like you don't have PHP files associated with the EXE.
You can do this My Computer > Tools > Folder Options > File Types. If nothing else, this can also help you verify your settings for them.
Otherwise, you can specify "C:\path\to\php.exe [file]" in the task.
This is what I did:
PHP file
<?php my code goes here ?>
*Note if you are using HTTP API/CURL in CLI use dl("php_curl.dll");
this loads curl into cli
Now I added PHP to windows path variable, this can be done from My computer, properties, advanced settings, environment variables, new
Next I created a .bat file, simply open a notepad & type code below and save as myfile.bat
#ECHO OFF
php -f d:\wamp\www\V3\task.php
This site might help you on .bat file syntax.
Now create a new scheduled task on windows & call the above .bat file as source,
For those people that can't able to make #Tomalak's answer work:
Check if there are spaces () in your directory. If it does, you need to enclose them with ".
"C:\Path\to php file\php.exe" -f "C:\Path\to php file\file.php"
And if it still did not work, check your file.php if there are included files in it.
include("file2.php"); /* OR */
include_once("file2.php");
Remove it. And if the included file in your file.php is really needed, try to move the code/script from that included file to your main file.
I think, you must execute your PHP script via URL.
you can write batch script for execute URL.
Why you don't write backend script in other language such as batch script, vbscript or etc.
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 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.
So I'm working with a designer on a website in PHP/MySQL and there are a few scripts that he would like to have to make life easier for him. He is pretty comfortable using the command line for stuff like git, SASS, node, etc. I would like him to be able to run my scripts like he would run a program, instead of running it through PHP.
So instead of this:
php /path/to/file/create_module.php module_name
I would like to do this:
myscript create_module module_name
Is it possible to do this with PHP on an Apache server? I know I will most likely have to modify the server to interpret it properly, which is fine. I just don't even know where to begin, and couldn't find what I needed on Google.
Your best bet would to be to create an alias.
So an alias of myscript would actually point to the command: php /path/to/file/create_module.php and then any extra arguments will be passed as typed.
In command line, do the following:
cd /etc/
nano bash.bashrc
At the very bottom of the file, add this line of text:
alias "myscript=php /path/to/file/create_module.php"
BASHRC is a script that is run on user login, so the alias will be recreated every time the user logs into the system.
I am not sure what you are looking for myscript to do, but to run a php script via the command line without specifying the php binary, just add a a shebang, like
#!/bin/env php
<?php
// The above expects env is in /bin
$foo = "bar";
Or the full path if you like
#!/usr/bin/php
Another option: Shell script wrapper
create a new text file in /bin or another directory in your PATH, name it how you would like to invoke your script and give it this content
#!/bin/bash
cd /path/to/your/php/scripts/folder
php script.php $*
don't forget to
chmod a+x /path/to/bash/script
The advantage of this is that your PHP script is run in the right directory where it may expect other resources to be that it depends on.
On A Linux Solution:
/usr/bin/php /path/to/file.php
On a Windows Solution:
C:\Path\To\PHPExe C:\Path\To\phpfile.php