【问题标题】:restrict number of files to upload for particular users限制为特定用户上传的文件数量
【发布时间】:2020-04-03 04:53:32
【问题描述】:

在我的属性部分,我有两种属性类型:

  1. 免费增值
  2. 高级版

我想限制用户对于 Freemium 属性类型只能上传 5 张图片,而对于 Premium 属性,用户可以上传不定式图片和视频。

一定需要一些建议。

这是我的图片上传部分:

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 );
    }
}

【问题讨论】:

  • 如果你只使用PHP端上传图片,那么你可以通过计算文件数来限制上传吗?比如,如果文件大于 5,抛出验证错误?
  • 一次或全部限制为 5 张图片?
  • @KiprasT 全面限制免费增值类型的属性。

标签: php laravel laravel-5 laravel-5.8 laravel-validation


【解决方案1】:
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);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    相关资源
    最近更新 更多