【问题标题】:Check if the upload takes too much time and in case notify the user?检查上传是否需要太多时间并通知用户?
【发布时间】:2012-01-22 19:21:00
【问题描述】:

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

我在我的upload.phpon_shutdown() 中使用register_shutdown_function()connection_status(),我创建了一个$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>

【问题讨论】:

  • 为什么不在上传页面上使用set_time_limit(0) 让他们上传文件?我只是问...
  • 顺便说一句,当你第一次声明$require时,除非我弄错了,否则你应该使用global $require = 'complete.php';

标签: php file-upload upload timeout


【解决方案1】:

register_shutdown_function('on_shutdown'); 只有在脚本执行后才会运行。

“注册一个回调,在脚本执行完成或调用 exit() 后执行。” - php.net

【讨论】:

    【解决方案2】:

    您可以检查最大执行时间的值并将其分配为 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多