我试图从拥有4000条记录的数据库创建备份文件(实际上备份将超过20000 rec),为此我打开文件对象,然后填写文件头,然后尝试追加行 - adif格式的行列记录。在不创建文件的情况下进行测试时,它会返回格式化文本,但如果我将fwrite
添加到循环中,则会抛出错误"undefined variable $file."
这是整个函数
public function processBackup() {
set_time_limit(300);
DB::disableQueryLog();
$fileName = 'backup'.date('YmdHis').'.adi';
$destinationPath = public_path().'/uploads/backups/'.Sentry::getUser()->username.'/';
$file = fopen($destinationPath.$fileName, 'w');
fwrite($file, "ADIF 2 Export from SWARLOG\n");
fwrite($file, "Generated on ".date('Y-m-d')."\n");
fwrite($file, "<PROGRAMID:7>SWARLOG\n");
fwrite($file, "<ADIF_Ver:1>2\n");
fwrite($file, "<EOH>\n");
Qso::withUser(Sentry::getUser()->id)->chunk(50,function($qsos){
foreach ($qsos as $qso) {
$row = AdifHelper::getAdif($qso);
fwrite($file, $row);
}
});
fclose($file);
}
有什么问题?
答案 0 :(得分:1)
您应该为您的功能添加use
。
变化:
function($qsos)
成:
function($qsos) use ($file)