2

I have a php file to create a directory. I want that php file execute in background. I have written 2 files like below.

file1.php

<?php
error_reporting(E_ALL); 
ini_set("display_errors", 1);
echo 'calling file1';
if(!is_dir('testdir')){
   mkdir('testdir');
}
?>

index.php

<?php
error_reporting(E_ALL); 
ini_set("display_errors", 1);
$i = '';
exec("php file1.php $i > test.txt &");
?>

I want a directory to be created when index.php is executed via browser. I think I am making mistake to write exec() function. Can anybody tell me how it will be?

user1841408
  • 133
  • 1
  • 1
  • 8
  • 1
    You can use curl_execute to open php files, on your server. It's much more secure than exec. (thing about injection to $i... – Aminadav Glickshtein Jan 08 '13 at 11:04
  • @Amina: what a weird advice :-S – zerkms Jan 08 '13 at 11:05
  • 1
    @Amina: any specific example of how `$i` can be "injected"? – zerkms Jan 08 '13 at 11:06
  • have you tried executing the command via ssh shell? is "php" globally known by the device without any paths to it? is the path to the script (file1.php) correct? Best thing is: take a look into your error.log of the system and post these lines here – simplyray Jan 08 '13 at 11:08
  • I am using exec because I want to execute file1.php asynchronusly. And I am using PHP version 5.2.4. Can we use curl_execute to call a file asynchronusly and is it supported by PHP version 5.4.2 – user1841408 Jan 08 '13 at 11:10
  • "'php' is not recognized as an internal or external command, operable program or batch file." I am getting this error in my log file under apache/logs/ – user1841408 Jan 08 '13 at 11:13
  • Sure, you can do it asynchronusly. and it is support by 5.4.2. It is very basic extension, that you must to know. – Aminadav Glickshtein Jan 08 '13 at 11:14
  • @user1841408: okay. you have to call php with the absolute path of its executable file. Depends on the system where its located. try "/usr/bin/php" on a unix system. otherwise try "which php" from a ssh shell to locate its position – simplyray Jan 08 '13 at 11:18
  • @simplyray Thanks. I am having the file1.php in my localhost under htdocs/rnd/file1.php and index.php in htdocc/rnd/index.php – user1841408 Jan 08 '13 at 11:40
  • I changed the exec function like exec("php http://localhost/rnd/file1.php $i > test.txt &"); . but same error in log file – user1841408 Jan 08 '13 at 11:45
  • @user1841408 Can you verify you have php binary installed in the system? Often website does not use the php binary but apache php module and the php binary is not installed unless otherwise requested. – Antti Rytsölä Jan 08 '13 at 12:01
  • If you do not have the php binary in the system you need to install the php binary or use the curl as suggested by @Amina . – Antti Rytsölä Jan 08 '13 at 12:03
  • You don't have to use exec, take a look at http://php.net/manual/en/function.pcntl-fork.php and http://php.net/manual/en/function.ignore-user-abort.php – Alin Roman Jan 08 '13 at 12:04
  • Now I am trying with curl.event the file1.php is calling and it is doing its work. I am using a for loop and calling file1.php using curl. But I can not able to understand how to call curl asynchronously. Can you help please? – user1841408 Jan 08 '13 at 12:28

1 Answers1

0

So, the answer is to use CURL, because you don't need to use any PHP binary for that.

If you don't want file1.php to wait until file2.php finish add timeout to the cUrl request on file1:

$curlcontext,CURLOPT_CONNECTTIMEOUT_MS,100;

Now, it's important to add this to file2.php:

ignore_user_abort (true);

It's mean that file2.php will not terminate after the timeout or file1.php

http://php.net/manual/en/function.ignore-user-abort.php http://php.net/manual/en/function.curl-setopt.php

Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117