Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute php file from another php

Tags:

php

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?

like image 876
nabrugir Avatar asked May 15 '10 17:05

nabrugir


People also ask

How do I call a PHP page from another PHP page?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

How do I run a PHP file from another server?

Use file_get_contents , to open up the file, append it to the second file like so: $secondFile = file_get_contents('http://www.sample.com/includeThis.php'); file_put_contents('your_file', $secondFile, FILE_APPEND); This will work if you want to put it at the end of your file.

How do you call another PHP script?

<? php include ('Scripts/Php/connection. txt'); //The connection. txt script is executed inside the current file ?>


2 Answers

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 
like image 171
Ignacio Vazquez-Abrams Avatar answered Sep 19 '22 11:09

Ignacio Vazquez-Abrams


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 ... ?> 
like image 23
Mark Baker Avatar answered Sep 17 '22 11:09

Mark Baker