In my web site I have some feature to upload data feeds by users. These data feeds normally having more than 30,000 records. And each records need to be go through complex validation process. So simply this feeds processing will take more than 1 hour to complete the task. This long running process unavoidable.
Here this feeds are uploaded by public user. And feed size is less than 5MB text file. So we don't want to give FTP access as well as we don't want to give command line access to those users. Only we prefer to give web access to upload the feeds. And we have to process the feed immediately after they upload. And once processed we have to send them a report of mail also.
Now the problem is the long running webpages throwing some proxy time out issue on many net work (as it take 1 hour to respond). This is the problem of most client net work configuration.
I need a way to trigger a PHP script once the feed uploaded. And that script should run in background at the same time properly complete the page load without delaying the client.
I will make the background to process the data and send mail to the client once the process completed. Cron tab not feasible here as client may use these feature only few times per month and in unexpected time range.
If there any way to trigger a PHP script in background but through web access please let me know.
A sample script to simplify the requirement
Assume this example and help me on modify this script to achieve the result. I have two PHP scripts "thread.php and createfile.php". The file with name "thread.php" will be opened by client through web browser. This script need to execute the script "createfile.php". But it will take 20 seconds to respond. But we want allow it run in background. And show an out put immediately in the browser.
Following are the code on the example scripts.
createfile.php (http://sugunan.net/demo/createfile.php)
<?php
sleep(20);
@unlink("phpfile.txt");
$myfile = fopen("phpfile.txt", "w") or die("Unable to open file!");
$txt = date("h:i:s")."\n";
fwrite($myfile, $txt);
fclose($myfile);
mail("someone@example.com","Background process","Process completed");
?>
created file visible at web url: http://sugunan.net/demo/phpfile.txt
thread.php (http://sugunan.net/demo/thread.php)
<pre>
<?php
echo "Script start at: " . date('h:i:s') . "\n";
include("createfile.php"); //give a method to execute this script but in background without delay
echo "Script end at: " . date('h:i:s');
?>
This will give following out put with the sleep()
function delay.
Script start at: 02:53:27
Script end at: 02:53:47
Is there any way to execute the "createfile.php" without include()
method. And avoid the 20 second sleep
delay while process the "createfile.php" in background?
Please consider I want to avoid the waiting time on client side and want to give the out put on browser immediately.