Allow me to post as a novice.
Would it not be less memory consuming to put the store-method outside of the foreach like so:
foreach($files as $f) {
// ...
}
$res = Storage::disk('s3')->put('in/' . $new_document->id, $contents);
Surely I'm doing something wrong, but creating new records inside a loop takes up more and more memory for each loop, until it runs out of memory all together and breaks.
I first figured I could fix it with just creating an array and do a bulk insert with DB::table()->insert() but then I realised that I need the id for every created record.
foreach($files as $f) {
unset($contents);
echo $f." : ".memory_get_usage()."\n";
$contents = file_get_contents($f);
unset($new_document);
$new_document = new \App\document();
$new_document->email_id = $document->email_id;
$new_document->directory = 'in';
$new_document->filename = $document->classified_as.time().'.pdf';
$new_document->ocr_task_status = 'Unknown';
$new_document->key = str_random(32);
$new_document->authority_id = $document->authority_id;
$new_document->pages = 1;
$new_document->step = 'email';
$new_document->classified_as = $document->classified_as;
$new_document->classified_score = $document->classified_score;
$new_document->identified = Carbon::now()->toDateTimeString();
$new_document->classified = Carbon::now()->toDateTimeString();
$new_document->save();
$res = Storage::disk('s3')->put('in/' . $new_document->id, $contents);
unlink($f);
}
Please or to participate in this conversation.