Laravel Multiple Uploading仅将20插入DB

时间:2018-04-17 17:54:53

标签: php mysql database laravel

我创建了一个允许多个图片上传的API,但是当我尝试上传超过20个时,它只会插入20个记录,而不是一次只能。它将在服务器上保存全部金额。没有抛出任何错误,日志中没有任何内容表明存在问题。

我保存到(events_images)的表格如下:

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

这是我的Laravel脚本:

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和Angular 4并获得相同的结果。我上传了20多张图片,而Laravel只看到20张。

1 个答案:

答案 0 :(得分:1)

您可能需要更改PHP设置以允许更大的上传。 PHP对文件上载大小,POST大小等有限制

转到:/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是整个POST的最大大小upload_max_filesize是单个大小的最大大小,max_file_uploads是您一次可以上传的文件的最大大小。

相关问题