1

I have a problem when executing the PHP files as background process. I have two PHP file as follow :

index.php

<?php   
    $cmd = "php cmdReadReport.php";
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
?>

cmdReadExcel.php

<?php
    $reportname = "./report/lw321.xls";
    $thereport = new Report();
    $thereport->readReport($reportname)
?>

My goal is to run Report.readReport as background process. readReport function is function which reads Excel file and saves its rows to database.

I have followed the tutorial but there is no result/no rows inserted. Is there anything wrong in the code?

Regards,

  • 1
    What happen if you run it in "foreground"? – Passerby May 14 '13 at 03:48
  • @Passerby there might be some reasons, like "file is too large", or script timeout, because there is a lot of rows in there. – BlitZ May 14 '13 at 03:52
  • @CORRUPT I only meant to see if `cmdReadExcel.php` actually functions properly. I myself use background technique to do things too, and OP's handling seems fine at a first glance. – Passerby May 14 '13 at 03:52
  • It will take long time (data is big). I have tried run it in "foreground" with smaller data and it worked fine. – user2020692 May 14 '13 at 03:54
  • i have tested to run the command in Windows command prompt too and it worked fine – user2020692 May 14 '13 at 03:56
  • @passerby is there any security setting? – user2020692 May 14 '13 at 04:02
  • @user2020692 Yes, what priviledge your server/PHP have? There might be chance that THAT user don't have enough permission on `cmd.exe`. In an XP + IIS + normal user account machine, I had to copy `cmd.exe` to PHP installation directory for this technique to work. – Passerby May 14 '13 at 04:06

1 Answers1

0

You could initiate script execution from client instead of server. Like this:

www.example.com/scripts/cmdReadReport.php?keypass=Giw3m4d9BaJO0ua0WFtG

Then just request this page with CURL or something. This approach is more portable.

Heavy
  • 1,861
  • 14
  • 25