1

It seems that on my server the popen() doesn't work at all. I access test.php via browser and the files postt1.php and postt2.php do not get executed. The article1.txt and article2.txt files are not written. All the files are in the same directory.

What could be the problem?

In file test.php I have:

<?php
$pipe1 = popen('postt1.php', 'w');
$pipe2 = popen('postt2.php', 'w');

pclose($pipe1);
pclose($pipe2);
echo "end"; 
?>

Inside files postt1.php I have:

<?php
 $tosave =  "Hello there";

 $file = fopen("article1.txt","w");
 fwrite($file,$tosave);
 fclose($file);
 echo $tosave;
 echo "<br>done";
 ?>

and in the postt2.php I have the same thing except that I write to file article2.txt.

HPierce
  • 7,249
  • 7
  • 33
  • 49
Brana
  • 1,197
  • 3
  • 17
  • 38
  • 1
    popen requires a command as first parameter, you are just passing `postt1.php`, i would say something like `php postt1.php`. OR use `include` if you want to execute those(postt1 & postt2) file. – Jigar Oct 18 '15 at 05:15
  • Are you sure - there is no another argument in popen on this page - http://stackoverflow.com/questions/70855/how-can-one-use-multi-threading-in-php-applications – Brana Oct 18 '15 at 05:48

1 Answers1

0

postt1.php end postt2.php need shebang tags (#!) and execute permissions.

Add the shebang tag by telling the file where the PHP interpreter is on the first line in the file (before the <?php!). Best practices dictates that you should access the interpreter via the env command:

 #!/usr/bin/env php

<?php
 $tosave =  "Hello there";

 $file = fopen("article1.txt","w");
 fwrite($file,$tosave);
 fclose($file);
 echo $tosave;
 echo "<br>done";
 ?>

You can also use a shebang that points directly to php: #!/usr/bin/php. But this is less portable accross systems.


You must also ensure that both files have executable premissions:

$ chmod +x posttl.php

HPierce
  • 7,249
  • 7
  • 33
  • 49