Laravel local disk append() taking up a lot of memory

穿精又带淫゛_ 提交于 2020-12-13 04:56:14

问题


Storage::disk('local')
         ->append('/newsletter_lists/'.$contact_file_name.'.csv',
                $contact->email.','.$contact->salutation.','.$contact->lastname.','.$contact->unsubscribelink.','.$contact->bonus
          );

This is the code in Laravel that is writing to csv file inside a foreach loop (chunk loading from database). But as far as I know the append() method, is opening the file everytime, loads the content in memory, adds the new line and saves the file again, which with a large number of records (65 000) takes a long time. I am trying to find a way to append to the end of the file without loading the contents in memory if possible or am I am taking the wrong approach here?


回答1:


I took the @d3L advice and used plain php to append to files and not to use the append() method of laravel, which is loading the content everytime it has to append a line.

$file = fopen(Storage::disk('local')->path('test.csv'), "a+b");


来源:https://stackoverflow.com/questions/54659226/laravel-local-disk-append-taking-up-a-lot-of-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!