brack11
brack11

Reputation: 181

Laravel file object lifetime

I'm trying to create backup file from database with 4000 records (in reality backups will be way over 20000 rec) and for that I open file object and then fill up file header and after that attempt to append line-by-line records in adif format. While testing without creating file, it returns formatted text but if I add fwrite to the loop it throws error
"undefined variable $file."
Here is whole function

 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);
    }

What is wrong?

Upvotes: 0

Views: 298

Answers (1)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111869

You should add use to your function.

Change:

function($qsos)

into:

function($qsos) use ($file)

Upvotes: 1

Related Questions