1

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

5
  • 1
    Why do you need exec() just include the file?! Commented Feb 6, 2015 at 8:40
  • 1
    But... why do you want to run it with exec()? I can't think of a reason to ever do this. Commented Feb 6, 2015 at 8:41
  • 2
    exec('php ' . $path, $output, $return); You may need to change php to the full path of the executable. Commented Feb 6, 2015 at 8:41
  • Take a look at output buffering php.net/manual/en/book.outcontrol.php Commented Feb 6, 2015 at 8:49
  • Hello i want to run the exec function as i need to put a function in the background until i complete a particular action on my form Commented Feb 6, 2015 at 9:38

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

It may also be that you'd need to use 'C:/xampp/bin/php-cli.exe'
@mondjunge : i tried your code but still i see that after my other called file is executed it is executing the main file. i need to call the called file and put it to background and make the main file work till then.. please help
I never worked with subprocesses in PHP. This is possible, but you need to figure it out on your own or ask a new question. Here is the PHP function you possibly need to do what you want: php.net/manual/en/function.pcntl-fork.php Also the Observer/Obserable Pattern should help, but I am not sure if this is possible in PHP.
Oh, yes, Observer/Obserable is possible with PHP: php.net/manual/en/class.splsubject.php
0

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

Comments

0

Adding

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

solved my problem..Thanks all

2 Comments

this is for linux systems, your question was on code for windows systems. You are a silly dev Rebecca. :P ;)
@mondjunge: i used the same answer which was suggested by u but the only difference made is the exec statement as i need the other file to be executed in the background, for your code i had to wait for the other file to get executed.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.