Php 在不超过时间限制的情况下保存多个csv的内容
我的网站是在我从Hostgator租用的共享主机上托管的。但是Php 在不超过时间限制的情况下保存多个csv的内容,php,csv,Php,Csv,我的网站是在我从Hostgator租用的共享主机上托管的。但是set\u time\u limit仅为30,您不能更改限制,因为它是共享主机。他们的规则 因此,我将我的csv文件拆分为8个csv文件,其中包含大约5.500条记录 我的问题是,有没有办法在一个函数中按顺序运行8个文件而不超过服务器的时间限制 例如: $lines = file(''.get_template_directory_uri() . '/lines1.csv', FILE_IGNORE_NEW_LINES); fore
set\u time\u limit
仅为30
,您不能更改限制,因为它是共享主机。他们的规则
因此,我将我的csv
文件拆分为8个csv
文件,其中包含大约5.500
条记录
我的问题是,有没有办法在一个函数中按顺序运行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
}
为了扩展我关于设置参数和重定向的评论,下面是一个简单的示例
在其基本要素中,脚本可以如下所示:
// 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();
//构建下一个文件的URL
$next\u script=$\u SERVER['PHP\u SELF'.'?文件='。($file+1);
//使用javascript重定向
echo'window.location.href=“”.$next_script.“;”;
退出();
在后台调用它命令行脚本没有时间限制exec(“php csv\u import.php>/dev/null&”)真正的问题是:你在用这些行做什么?单是读取5000行不会以任何方式达到时间限制-也许在处理过程中还有改进的余地。同意@ccKep,从一个文件读取5500条记录最多不应超过1-2秒。不过,如果您从浏览器访问脚本,可以让它接受文件号的参数,并在完成后重定向到下一个。例如,script.php?file=1
读取lines1.csv
然后重定向到script.php?file=2
等等,直到所有文件都被读取。谢谢@tim,但这确实是我第一次从后台命令中看到一些东西,我对code@rickdenhaan实际上跑步的时间很多,由于函数中有各种数据操作,谢谢,但是文件是/public\u html/example.com/wp content/themes/thema/inc/theme options.php
管理页面上的url是:https://www.example.com/wp-admin/admin.php?page=options_theme
我对在url中插入变量有点困惑,因为提交按钮在wordpress主题中options@Gislef啊。恐怕我帮不了你,我总是设法远离Wordpress。如果你编辑你的问题并添加标签,那可能会引起别人的注意。