代码之家  ›  专栏  ›  技术社区  ›  Gislef

在不超过时间限制的情况下保存多个csv的内容

  •  0
  • Gislef  · 技术社区  · 7 年前

    我的网站由我从Hostgator雇佣的共享主机托管。但是 set_time_limit 只是 30 您不能更改限制,因为它是共享主机。他们的统治。

    所以我分开了我的 csv 有关于 5.500 8年的记录 csv 文件夹。

    我的问题是,有没有办法一次按顺序运行8个文件 function 不超过 time_limit 服务器的名称?

    例子:

    $lines = file(''.get_template_directory_uri() . '/lines1.csv', FILE_IGNORE_NEW_LINES);
    
    foreach ($lines as $line_num => $line){
        //here is some code for save you content line
    }
    
    0 回复  |  直到 7 年前
        1
  •  1
  •   rickdenhaan    7 年前

    为了扩展我关于设置参数和重定向的评论,下面是一个简单的例子。

    在其基本要素中,脚本可以如下所示:

    // require the ?file= parameter to exist
    if (empty($_GET['file'])) {
        echo 'No file-parameter provided.';
        exit();
    }
    
    $file = $_GET['file'];
    
    // build the full path to the file
    $filename = get_template_directory_uri() . '/lines' . $file . '.csv';
    
    // make sure the file exists, this will make sure the script stops
    // when the last file has been processed
    if (!file_exists($filename)) {
        echo 'File ' . $file . ' does not exist. Processing may be complete.';
        exit();
    }
    
    // read and process the file
    $lines = file($filename, FILE_IGNORE_NEW_LINES);
    foreach ($lines as $line_num => $line){
        // process this line
    }
    
    // build the URL to the next file
    $next_script = $_SERVER['PHP_SELF'] . '?file=' . ($file + 1);
    
    // set a HTTP/1.1 307 Temporary Redirect header and tell the browser
    // where to go
    http_response_code(307);
    header('Location: ' . $next_script);
    exit();
    

    现在,您可以通过转到开始导入过程 http://example.com/script.php?file=1 .

    请注意,对于使用 header() 函数工作时,不能先输出任何内容。 标题() 设置HTTP响应头,该头必须在响应体(如HTML、Javascript等)之前发送到浏览器。

    如果不能保证这一点,另一种解决方案是使用javascript重定向:

    // build the URL to the next file
    $next_script = $_SERVER['PHP_SELF'] . '?file=' . ($file + 1);
    
    // redirect using javascript
    echo '<script>window.location.href = "' . $next_script . '";</script>';
    exit();