首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保存多个csv的内容而不超过时间限制

问保存多个csv的内容而不超过时间限制
EN

Stack Overflow用户
提问于 2019-02-20 06:05:24
回答 1查看 37关注 0票数 0

我的网站托管在我从Hostgator租用的共享主机上。但是set_time_limit只是30,你不能改变限制,因为它是共享主机。他们的规则。

因此,我将我的csv文件拆分为8个csv文件,其中包含大约8个5.500记录。

我的问题是,有没有办法在不超过服务器time_limit的情况下,在单个function中按顺序运行这8个文件?

示例:

代码语言:javascript
复制
$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
}
EN

回答 1

Stack Overflow用户

发布于 2019-02-20 07:28:53

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

就其基本要素而言,该脚本可能如下所示:

代码语言:javascript
复制
// 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()函数进行重定向,您不能先输出任何内容。header()设置一个HTTP响应头,必须在响应体之前发送给浏览器(如HTML、Javascript等)。

如果你不能保证这一点,另一种解决方案是使用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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54775661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档