检查上传是否花费太多时间,以防通知用户


Check if the upload takes too much time and in case notify the user?

当用户上传大量互联网连接速度较慢的文件时,我尝试检测PHP脚本执行超时:我应该只警告用户超时。

我在upload.php中使用register_shutdown_function()connection_status()on_shutdown()我创建了一个$timeout标志,用于complete.php检测超时。

问题是结果是完全错误的:我有一个超时致命错误(没关系(,一个需要致命错误(在函数require($require),好像$require是空的(,并且没有显示complete.php

太糟糕了,我不知道我错在哪里。这是脚本upload.php(简短版本(:

<?php
   register_shutdown_function('on_shutdown');
   $require = 'complete.php'; // Complete page
   sleep(50); // fake timeout for testing
   // Do stuff, save files, insert into database...
   // This always invoked
   function on_shutdown()
   {
      global $require;
      $timeout = connection_status() == 2;
      require($require);
   }
?>

。这是complete.php

<!DOCTYPE html>
<html>
    <head>
        <title>Upload complete</title>
        <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    </head>
    <body>
        <?php if ($timeout) { ?><!-- is timeout? -->
            <p>Upload process took too much time, results are unpredictable.</p>
        <?php } ?>
    </body>
</html>

register_shutdown_function('on_shutdown'(; 仅在执行脚本后运行。

"注册要在脚本执行完成或调用 exit(( 后执行的回调 php.net。"

您可以检查最大执行时间的值并将其分配给 0 以进行长时间执行,而不是尝试超时,但不会重新注释安全性方面的 0

$maxExeTime = ini_get('max_execution_time'); //this will get maxumum excetion time for PHP script in seconds
echo $maxExeTime;

您也可以检查下面的全局变量

$postMs = ini_get('post_max_size'); //this will get maxumum size of data to be accepted in $_POST
$upMaxFiles = ini_get('upload_max_filesize'); //this will get maxumum allowed file size for file upload
$noFilesUp = ini_get('max_file_uploads'); //this will get number of files to be load on    single request