tags:

views:

41

answers:

3

I try to call a php file that starts like

<?php

//Connection
function connection () {
...

I call from a php like:

<?php
exec ('/ /opt/lampp/htdocs/.../name.php)')
?>

I get:

line1-> cannot open ?: No such file
line 3 //Connection: not found
line 4 Syntax errror: "("

What happens? Why I can't execute it?

+2  A: 

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');
Rob Kennedy
+2  A: 

It's trying to run it as a shell script, which doesn't work. Just use include() or one of its friends.

Ignacio Vazquez-Abrams
Ok, thanks. I use include() and it works!
skiria
@skiria If it works, then this is the accepted answer?
tylermac
sorry, I'm new on this and I don't know that i have the accept the answer....
skiria
+1  A: 

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 ... ?>
Mark Baker
Thanks. I also tried exec('/usr/... -f /oopt.../name.php)' and it either worked. finally I use include() and it works. Thanks for your answer.
skiria
for punct read shebang
Mark Baker