简体   繁体   中英

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

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.

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

Adding

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

solved my problem..Thanks all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM