I want to call a PHP file that starts like
<?php
function connection () {
//Statements
}
I call from the PHP like this:
<?php
exec ('/opt/lampp/htdocs/stuff/name.php');
?>
I get:
line1-> cannot open ?: No such file
line 3 //Connection: not found
line 4 Syntax errror: "("
Why doesn't this correctly execute the name.php file?
It's trying to run it as a shell script, which interprets your <?php token as bash, which is a syntax error. Just use include() or one of its friends:
For example, in a.php put:
<?php
print "one";
include 'b.php';
print "three";
?>
In b.php put:
<?php
print "two";
?>
Prints:
eric#dev ~ $ php a.php
onetwothree
exec is shelling to the operating system, and unless the OS has some special way of knowing how to execute a file, then it's going to default to treating it as a shell script or similar.
In this case, it has no idea how to run your php file.
If this script absolutely has to be executed from a shell, then either execute php passing the filename as a parameter, e.g
exec ('/usr/local/bin/php -f /opt/lampp/htdocs/.../name.php)') ;
or use the punct at the top of your php script
#!/usr/local/bin/php
<?php ... ?>
Sounds like you're trying to execute the PHP code directly in your shell. Your shell doesn't speak PHP, so it interprets your PHP code as though it's in your shell's native language, as though you had literally run <?php at the command line.
Shell scripts usually start with a "shebang" line that tells the shell what program to use to interpret the file. Begin your file like this:
#!/usr/bin/env php
<?php
//Connection
function connection () {
Besides that, the string you're passing to exec doesn't make any sense. It starts with a slash all by itself, it uses too many periods in the path, and it has a stray right parenthesis.
Copy the contents of the command string and paste them at your command line. If it doesn't run there, then exec probably won't be able to run it, either.
Another option is to change the command you execute. Instead of running the script directly, run php and pass your script as an argument. Then you shouldn't need the shebang line.
exec('php name.php');
This came across while working on a project on linux platform.
exec('wget http://<url to the php script>)
This runs as if you run the script from browser.
Related
Okay, I'm at a bit of a loss here.
I'm testing out running PHP scripts from within powershell and it just keeps opening NotePad ++ rather than executing the script. I cannot figure out why this won't work...
I'm using a pretty basic PHP script to test:
<?php
echo 'Hello, World!';
?>
And I'm calling it using the standard way I run .ps1 files:
PS C:\php> c:\phpfiles\test25.php
The execution policy is set to unrestricted... what am I doing wrong?
You should pass the path of the file as an argument to the PHP executable. If (lets say) PHP is installed in c:\php, then you must do:
PS c:\php\php.exe -f c:\phpfiles\test25.php
I am calling a shellscript from php using shell_exec() command. Following is the simple version of the shell script:
args[0] = "tom"
echo "hello"
echo "${args[0]}"
When I run this script from terminal, it gives the following output in terminal:
hello
tom
Whereas when I call this from php using shell_exec() only "hello" is printed and not "tom" . ie; variable assignment is not working when the script is called from php. Why this happens and how can I resolve this.
Any help is appreciated.
Probably PHP executes the script with sh, not Bash; thus arrays (which are a Bash feature) are not supported by the shell.
Workarounds: don't use arrays, or explicitly inboke Bash on the script. (If PHP understands shebangs, having a correct shebang line as the first line of the script may well be sufficient.)
First, args[0] = "tom" should not have space. It should be args[0]="tom"
(I'm not sure is this the problem, but worth to try).
Try this at the end of your code, to see which shell is running your script.
echo "`ps -p $$`"
Try both on terminal and PHP scripts to see if it same shell or not.
Is there a correct way to use php from the command line...or rather...is one way more correct than another ?
If you create a file, say test.php with the following code:
#!/usr/bin/php
<?php
print "This is a test".PHP_EOL;
print "This is another test!!";
?>
then chmod +x text.php (make it executable on linux).
You can then run in the following ways.....
./test.php
or
php test.php
I prefer just using ./test.php, but often see php test.php in examples.
ALSO
is the following correct syntax for the shebang line
#!/usr/bin/php
or is this more correct
#!/usr/bin/php -q
I've seen both, and see that the -q flag is to quiet the html stuff, but was wondering if
php compiled with cli compatibility really needs the -q flag ???
Thanks for your help :)
#!/usr/bin/php
On the first line of an interpreter script, the "#!", is the name of a program which should be used to interpret the contents of the file. For instance, if the first line contains
"#! /bin/sh"
, then the contents of the file are executed as a shell script.
In your case it means excute the script using the php file in /usr/bin location.
Using php test.php means that you are running your test.php with php so u don't need the first line. where as for ./test.php your file needs to be executable and first line is required.
And about the -q flag look at this
specifically on
rob 23-Mar-2007 11:48
There are better expanations if you see for -q. here's another one
If you are writing little php scripts for your own purposes, #! is fine.
If they are to be on some kind of world visible box (e.g. web server), then I'd say not so fine - since you have made them executable they are now a security risk.
I tend to use #! for perl, sh (which are always little private, non production things) and php somefile.php for PHP (which may or may not end up on a server).
I just began learning PHP. I've installed php5 on Linux and wrote very simple code just to get going.
How can I run scripts? I tried using the -f option, but it works as a cat command and just spits out the code to standard output.
The interactive interpreter option works fine. Is a web browser the only way to execute a PHP script?
A simple:
php myScript.php
… should do the job.
If it is acting like cat, then you probably forgot to switch out of template mode and into script mode with <?php
Shorter way for command line:
php -r 'echo "Hello "; echo "Jay";'
OR
php -r 'echo dirname("parent/child/reply") . "\n";'
As already mentioned, you can execute your PHP with the following.
php myScript.php
If you wish to pass an argument(s), you can simply do so like this:
php myScript.php Apples
In your PHP file you can use this argument by accessing the $argv array like this:
<?php
echo 'I like ' . $argv[1];
?>
The above would print our "I like Apples".
Note the array index is 1 and not 0. 0 is used for script name. In this case $argv would be "myScript.php"
For more information, check out my blog post Running PHP from the Command Line - Basics.
Actually, PHP's main purpose is to generate web pages, but there are at least two other options:
command line (CLI) script execution,
interactive shell - which is actually the variant of the previous option,
The first one can be achieved in many ways (eg. by giving proper permissions to the file and calling script by providing its URI, eg. ./index.php), the second one can be invoked by php -a command (as stated in the documentation mentioned above).
I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. After much struggling, I reduced this to the classic hello world example. The calling script looks like:
exec('/usr/bin/php /home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &');
When I run this, the output execoutput.txt contains a copy of the invoking script page, not hello world as I expected.
Why can't I get this PHP script to execute using exec? Note that when I change the command to something like ls -l, the output is a directory listing as expected. btw, in case it matters, I did chmod the called script to 755...
Update - I moved the exec call to the end of the calling script and at least now I don't see the calling script executed in the output. Thx to posters and I will try some of these ideas.
Help!
Thanks
Steve
I had this issue also and it turns out this is a bug in php (#11430). The fix is to use php-cli when calling another php script within a php script. So you can still use exec but rather than use php use php-cli when calling it in the browser:
exec("php-cli somescript.php");
This worked for me.
What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.
if you just want the script's output, try:
exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &')
however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:
exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')
the above should only output the "Hello World" to the execoutput.
Edit:
Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:
alias pexec='php /home/quote2bi/tmp/helloworld.php'
then
exec('pexec > /tmp/execoutput.txt 2>&1 &')
it seems to be a problem with the way exec handles input as opposed to the shell itself.
-John
The problem is with PHP itself, it treats everything as $argv in the script. It doesn´t redirect the output to a file ou to /dev/null.
I faced the same problem some time ago. What I did is to create a runscript.php in /opt/php-bin and then inside this script run what It should be running. Something like this:
$script = $argv[1]
$params = implode(' ', array_slice($argv, 2));
$cmd = "{$script} {$params} > /dev/null &";
$output = array();
$return = 0;
exec("php {$cmd}", $output, $return);
exit((int)$return);
And then you call it using:
exec('/opt/php-bin/runscript.php /path/to/your/script.php arg1 arg2')
It´s the only way I managed to get this working.
To avoid the stated problems of PHP in this area, why not put this in inside a shell script? PHP can then execute the shell script which has all the redirections handled internally.
If you need to dynamically change things, then why not write the shell script and then execute it (and of course, clean up afterwards)?
if you are just simply running a php script one possible way to execute the entire code is to use the include() that will run the php file and output any results. You cannot direct the output to a text file but it should appear in the browser window if you're Hello World php script looks like
<?php echo "Hello World!"; ?>
then it will spit that out in the browser. So your second code would look like
<?php include("helloWorld.php"); echo " PHP ROCKS";?>
resulting in a page that would look like,
Hello world! PHP ROCKS
This runs as if you run the script from browser.
This came across while working on a project on linux platform.
exec('wget http://<url to the php script>)
Hope this helps!!