Rebecca
Rebecca

Reputation: 413

execute external php file using exec()

i am new to exec function and need help with executing an external php file. My code of the files are as follows

Script.php(main file) :

<?php 
$path = 'C:/xampp/htdocs/user/execute.php';
exec($path, $output,$return);
var_dump($return);
echo "hi"."<br>";
echo "end";?>

execute.php(calling file) :

for($i=0;$i<10;$i++){
echo "hello"."<br>";
}

trying to execute the calling file

Upvotes: 1

Views: 10252

Answers (3)

mondjunge
mondjunge

Reputation: 1239

exec is for executing system functions, not for running scripts. (Take a look at the manual, it's helping: http://php.net/manual/de/function.exec.php)

To achieve what you want, you could pass the path to php executable and add the script as parameter, like this:

<?php
$phpExecutable = 'C:/xampp/bin/php.exe' 
$path = 'C:/xampp/htdocs/user/execute.php';
exec($phpExecutable." ".$path, $output,$return);
var_dump($return);
echo "hi"."<br>";
echo "end";?>

Should work. I do not know where your php executable is located, so please adapt it to your location.

Happy coding.

Upvotes: 2

Rebecca
Rebecca

Reputation: 413

Adding

exec("php execute.php > /dev/null &");

solved my problem..Thanks all

Upvotes: 0

Jakub Matczak
Jakub Matczak

Reputation: 15656

First of all, as guys said in comments, you don't need exec() here at all. You can just include that file.

Anyway, exec() function executes external program. A .php file is not a program, it's just a script that is executed by a program called php.

So you can run it like:

exec('php ' . $path, $output,$return);

php also can require you to give full path to its executable if it's not available globally.

http://php.net/manual/en/function.exec.php

Upvotes: 0

Related Questions