我试图通过网址导入大约130k图像。我认为写一个cli artisan命令会很方便。
我正在使用laravel和#34; Spatie / MediaLibrary"。
这是我的剧本:
public function handle()
{
Article::where('index_block_position',0)->each(function($article){
if($article->featured_image != ''){
try{
$featuredImage = $this->mediaLibrary->addMediaFromUrl($article, 'http://myoldwebsite.com' . $article->featured_image, 'featured_image');
$libraryImage = $this->mediaLibrary->addMediaFromUrl(MediaLibrary::first(), 'http://myoldwebsite.com' . $article->featured_image);
$article->index_block_position = 1;
$article->save();
$this->info("Article {$article->id} image: {$featuredImage->id} - Library image: {$libraryImage->id} - added");
Log::useDailyFiles(storage_path().'/logs/featured-image-migration.log');
Log::info("Article {$article->id} image: {$featuredImage->id} - Library image: {$libraryImage->id} - added");
}catch (\Exception $e){
$this->info("image: {$article->featured_image} - exception: {$e->getMessage()}");
Log::useDailyFiles(storage_path().'/logs/featured-image-migration.log');
Log::info($e->getMessage());
}
}
});
$this->info('All done!');
}
不幸的是,在添加10张图片后,我收到以下错误:
Symfony\Component\Debug\Exception\FatalErrorException: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24576 bytes)
我不知道如何才能完成这项工作,因为我要导入130k图像,并且在10张图像后内存限制为128M已经耗尽......
我很感激建议。
答案 0 :(得分:0)
如果您使用的是Laravel,请考虑将上传内容分解为jobs
,然后可以对其进行排队和处理。
https://laravel.com/docs/5.4/queues
您还可能遇到/etc/php/x.x/fpm/php.ini
中设置的文件上传限制。更具体地说,这些设置:
upload_max_filesize
post_max_size