以下是一些使用它的示例。
第一个是基本示例。
<?php
$file = __DIR__ . '/text.txt';
if (is_file($file) && is_writable($file)) {
@unlink($file);
echo '<small style="color: #ccc;">' . $file . ' was deleted.</small><br>' . PHP_EOL;
}
echo '<p>Calling to <code>fastcgi_finish_request()</code>.</p>' . PHP_EOL;
echo '<p>If success, the file ' . $file . ' will be created.</p>' . PHP_EOL;
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
} else {
echo '<p style="color: red;">This server does not support <code>fastcgi_finish_request()</code> function.</p>' . PHP_EOL;
echo 'Exit now.<br>' . PHP_EOL;
exit();
}
echo 'This line will be not echo out.<br>' . PHP_EOL;
file_put_contents($file, date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);
?>
如果成功,将创建文件 text.txt。
==========================
第二个是关于执行超时。
<?php
set_time_limit(5);
$file = __DIR__ . '/text.txt';
if (is_file($file) && is_writable($file)) {
@unlink($file);
echo '<small style="color: #ccc;">' . $file . ' was deleted.</small><br>' . PHP_EOL;
}
echo '<p>Testing timeout and <code>fastcgi_finish_request()</code> function.</p>' . PHP_EOL;
echo '<p>Set timeout to ' . ini_get('max_execution_time') . ' seconds.</p>' . PHP_EOL;
echo '<p>Calling to <code>fastcgi_finish_request()</code>.</p>' . PHP_EOL;
echo '<p>If success, the file ' . $file . ' will be created but error will be shown in the log.</p>' . PHP_EOL;
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
} else {
echo '<p style="color: red;">This server does not support <code>fastcgi_finish_request()</code> function.</p>' . PHP_EOL;
echo 'Exit now.<br>' . PHP_EOL;
exit();
}
$i = 1;
while(true){
if ($i <= 10) {
file_put_contents($file, date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);
$i++;
}
}
?>
我发现,只要没有达到 php.ini 或 set_time_limit() 函数中设置的超时时间,代码就会继续运行。