laravel多次上传只在db中插入20个

fzsnzjdm  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(315)

我已经创建了一个允许多个图像上传的api,但是当我尝试上传20个以上的图像时,它只会插入20个记录,而不会一次插入更多的记录。它将在服务器上保存全部金额。没有抛出错误,日志中也没有指示问题的内容。
我要保存到的表(events\u images)如下所示:

id, event_id, title, path, size, extension, description, created_at, updated_at

这是我的剧本:

public function store(Request $request)
{
    $id = $request->id;
    $images = $request->file('image');
    $count = 0;

    // confirm that files exists in the request
    if(empty($images)):
        return response()->json(['error' => 'no images exist'], 500);
    endif;

    // validate image(s) before we store to db to make sure there are no failures
    // TODO : add to validation
    $validator = Validator::make($request->all(), [
        'image.*' => 'image|mimes:jpeg,jpg,png'
    ]);

    if($validator->fails()):
        return response()->json(['error' => 'upload failed, please make sure you are uploading a jpeg, jpg, or png'], 500);
    endif;

    // store images in DB and upload
    foreach($images as $image):
        $title = Uuid::uuid1();
        $extension = $image->getClientOriginalExtension();
        $path = $title . '.' . $extension;
        $size = $image->getClientSize();
        $description = NULL;           

        // store in db
        EventsImage::Create([
            'title' => $title,
            'path' => $path,
            'extension' => $extension,
            'size' => $size,
            'description' => $description,
            'event_id' => $id
        ]);

        // save to filesytem
        $raw = Storage::disk('media')->put('events' . '/' . $id . '/' . $path, file_get_contents($image));

        // create and store thumb
        $thumb = Image::make($image)->resize(150, null, function ($constraint) {
             $constraint->aspectRatio();
        });
        Storage::disk('media')->put('events' . '/' . $id . '/t_' . $path, (string) $thumb->encode());

        // create and store medium
        $medium = Image::make($image)->resize(300, null, function ($constraint) {
             $constraint->aspectRatio();
         });
        Storage::disk('media')->put('events' . '/' . $id . '/m_' . $path, (string) $medium->encode());

        // create and store full size
        // TODO : if smaller than 1920 then don't do anything
        $full = Image::make($image)->resize(1920, null, function ($constraint) {
             $constraint->aspectRatio();
        });
        Storage::disk('media')->put('events' . '/' . $id . '/' . $path, (string) $full->encode());

        $count++;

    endforeach;

    $event = Event::with('EventImages')->find($id);

    return response()->json(['success' => true, 'message' => $count . ' images have been uploaded successfully', 'data' => $event], 200);
}

我已经查看了发送到api的数据,所有的东西都在那里,所有的文件都保存到光盘上。我还注解了文件保存逻辑,看看它是否是一个时间的东西,它仍然只会插入20条记录。
有什么限制设置吗?是因为我使用循环一次保存一条记录吗?

编辑

我使用了postman和angular4,得到了相同的结果。我上传了20多张图片,拉威尔只看到20张。

piwo6bdm

piwo6bdm1#

您可能需要更改php设置以允许更大的上载。php对文件上传大小、文章大小等有限制
去: /etc/php5/fpm/php.ini 或者 /etc/php/7.0/fpm/php.ini 或者如果您使用的是apache /etc/php/7.0/apache2/php.ini 并更改这些值。

post_max_size = 125M 

upload_max_filesize = 100M 

max_file_uploads = 20

更高的值。哪里 post_max_size 是整个帖子的最大大小 upload_max_filesize 是单个尺寸的最大尺寸 max_file_uploads 是一次可以上载的文件的最大限制。

相关问题