0

I have a table with ~500 rows, which will expand in the future.

In background.php I create some PDFs and send them email and FAXs to each record of this table: this requires a certain amounts of time, which means I can not do the operation on a browser window or with an AJAX call.

Now I'd like to create a background process (exec() or shell_exec() or proc_open() or ...?) which iterate throght the table and sends FAXs and mails.

I don't need logs because the script has an internal log system.

I've tried something like

<form method="post" action="">
  <input type="submit" name="background" value="go">
</form>

if(isset($_POST['background'])){
    exec('php /WEB/mysite/.../.../htdocs/test/background.php &') or echo 'Error';
}

where background.php is called from the user on the browser window!

I always get error but I don't know where and what error (probably errorS) I have!

I have no idea how to go forward...

EDIT Nobody on this site is able to answer my question properly? =(

Perocat
  • 1,481
  • 7
  • 25
  • 48

2 Answers2

0

For this and other similar tasks you can use cron job. It is time-based job sheduler in Unix-like operating systems. Webhosting companies usually provide this service for their clients.

You just need to write the PHP script, set up time when it should be executed and after that it will run at specified time in background.

  • It should be executed on user action not regularly – Perocat Apr 27 '14 at 14:44
  • @Perocat In that case have a look at this [stackoverflow answer](http://stackoverflow.com/questions/1532065/php-multithread). –  Apr 27 '14 at 14:48
0

Maybe you should call the function write_session_close() before the long operation. This will make the server able to respond ajax calls independently of getting responses to the previous requests. This will not delete the session, but won't let write on it again. But you can always reset the session using session_destroy() and session_start().

Andrade
  • 1,179
  • 12
  • 15