我的网站托管在我从Hostgator租用的共享主机上。但是set_time_limit只是30,你不能改变限制,因为它是共享主机。他们的规则。
因此,我将我的csv文件拆分为8个csv文件,其中包含大约8个5.500记录。
我的问题是,有没有办法在不超过服务器time_limit的情况下,在单个function中按顺序运行这8个文件?
示例:
$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
}发布于 2019-02-20 07:28:53
为了扩展我关于设置参数和重定向的评论,这里有一个简单的例子。
就其基本要素而言,该脚本可能如下所示:
// 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重定向:
// 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();https://stackoverflow.com/questions/54775661
复制相似问题