简体   繁体   English

限制特定用户上传的文件数量

[英]restrict number of files to upload for particular users

In my property section I have two property types:在我的财产部分,我有两种财产类型:

  1. Freemium免费增值
  2. Premium优质的

I want to restrict users to upload only 5 images for Freemium property type while for Premium properties a user can upload infinitive number images and videos.我想限制用户只能上传 5 张免费增值物业类型的图片,而对于高级物业,用户可以上传不定数量的图片和视频。

Must needed some suggestions.必须需要一些建议。

Here is my image upload part :这是我的图片上传部分:

public function postProperty(PropertyRequest $request)
{

    $user = User::where('id', $request->user->user_id)->first();
    if(!empty($user))
    {
        $data['user_id']        = $user->id;
        $data['type']           = $request->type;
        $data['category']       = $request->category;
        $data['area']           = $request->area;
        $data['price']          = $request->price;
        $data['description']    = $request->description;

        //dd($data);
        $property = Property::create($data);

        //$property['flag'] = false;          // if (flag = false, property = freemium) else (flag = true, property = premium ))

        $urls = new PropertyImage();

        if ($request->hasFile('url'))
        {
            $files = $request->file('url');
            foreach($files as $file)
            {
                $mime = $file->getMimeType();

                //$property['flag'] = $property->account == 1 ? false : true;

                if($mime == 'image/jpeg')
                {
                    $fileName = $file->getClientOriginalName();
                    $destinationPath = public_path() . '/images/';
                    $file->move($destinationPath, $fileName);
                    $urls->url = '/public/images/' . $fileName;
                    $url_data = [
                        'property_id' => $property->id,
                        'url_type' => 1,
                        'url' => $urls->url,
                    ];


                    $urls->create($url_data);
                }

                elseif($mime == 'video/mp4')
                {
                    $fileName = $file->getClientOriginalName();
                    $destinationPath = public_path() . '/videos/';
                    $file->move($destinationPath, $fileName);
                    $urls->url = '/public/videos/' . $fileName;
                    $url_data = [
                        'property_id' => $property->id,
                        'url_type' => 2,
                        'url' => $urls->url,
                    ];
                    $urls->create($url_data);
                }
            }
        }
        return Utility::renderJson(trans('api.success'), true, $property, $urls );
    }
}
You can use laravel validation to restrict user to some number of files as shown below
//If user is Freemium then restrict him
if (!$property['flag']) {
$messages = [
    "url.max" => "files can't be more than 3."
 ];

 $this->validate($request, [
        'url' => 'max:3',
    ],$messages);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM