Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 限制特定用户要上载的文件数_Php_Laravel_Laravel 5_Laravel 5.8_Laravel Validation - Fatal编程技术网

Php 限制特定用户要上载的文件数

Php 限制特定用户要上载的文件数,php,laravel,laravel-5,laravel-5.8,laravel-validation,Php,Laravel,Laravel 5,Laravel 5.8,Laravel Validation,在“我的属性”部分,我有两种属性类型: Freemium 特优 我想限制用户仅为Freemium属性类型上传5张图像,而对于Premium属性,用户可以上传无限数量的图像和视频 我需要一些建议 这是我的图片上传部分: public function postProperty(PropertyRequest $request) { $user = User::where('id', $request->user->user_id)->first(); if(!e

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

  • Freemium
  • 特优
  • 我想限制用户仅为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个图像,或者对于Freemium类型的属性,抛出整体?@KiprasT Restrict total。
    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);
    
    }