【问题标题】:Only first image uploaded, Upload multiple images individually in Laravel仅上传第一张图片,在 Laravel 中单独上传多张图片
【发布时间】:2020-03-23 16:01:33
【问题描述】:

这里是Laravel新手,想请教如何一张一张上传多张图片。多张图片可在一个选择中使用,但将图片一张一张上传不起作用,并且第一张图片仅在提交后出现。请帮忙。

控制器

    if (\Input::file('photos')) {
                $base_path =
                    dirname(__FILE__) . "/../../../../uploads/img/gallery/" . $add->id . "/";
                ini_set('gd.jpeg_ignore_warning', 1);
                //get last uploaded file name
                $files = (\Input::file('photos'));
                $list_image = $temp = explode(",", $request->list_image);
                foreach ($files as $key => $value) {
                        if (!is_null($value)) {
                        $temp = explode(".", $value->getClientOriginalName());
                        $newfilename = $value->getClientOriginalName();
                        if (!file_exists($base_path)) {
                            mkdir($base_path);
                            chmod($base_path, 0777);
                        }
                        $key = array_search($newfilename, $list_image);
                        if ($key == 0) {
                            $newfilename = $add->id . '_primary' . '.' . end($temp);
                        } else {
                                $newfilename = chr(64 + $key) . '_' . $newfilename;
                        }
                        $image = \Image::make($value);
                        // perform orientation using intervention
                        $image->orientate();
                        $new_width = $image->width();
                        $new_height = $image->height();
                        if ($new_width > 800) {
                            $percent = 0.7;
                            $new_width = $new_width * $percent;
                            $new_height = $new_height * $percent;
                        }

                        // resize image to fixed size
                        $image->resize($new_width, $new_height);

                        // create a new Image instance for inserting
                        $watermark = \Image::make(
                            public_path() . '/assets/img/icons/watermark.png',

                        );
                        // insert watermark at bottom-right corner with 10px offset
                        $image->insert(
                            public_path() . '/assets/img/icons/watermark.png',
                            'bottom-right',
                            10,
                            10,
                        );
                        $image->save($base_path . $newfilename);
                    }
                }

        }

【问题讨论】:

  • 你用的是什么版本的 laravel?
  • 使用 Laravel 5.1
  • 你不必为了上传多个文件而写这么多代码,如果你更新你的 laravel 版本会变得容易得多。

标签: php arrays laravel


【解决方案1】:

你可以这样做。

    $list_image = $temp = explode(",", $request->list_image);

if($request->hasfile('photos'))
        {
         $destinationPath = public_path('uploads/img/gallery/'. $add->id.'/'); 
            foreach($request->file('photos') as $k => $file)
            {

            $temp = explode(".", $file->getClientOriginalName());
            $newfilename = $file->getClientOriginalName();

            $key = array_search($newfilename, $list_image);
            if ($key == 0) {
                  $newfilename = $add->id . '_primary' . '.' . end($temp);
            } else {
                  $newfilename = chr(64 + $key) . '_' . $newfilename;
            }

                $name = $newfilename.'.'.$file->getClientOriginalExtension();           

                File::makeDirectory($destinationPath, $mode = 0777, true, true);
            $file->move($destinationPath, $name);
            $tdestinationPath = public_path('uploads/img/gallery/thumb'. $add->id.'/');
            try {
                    $img = Image::make($file->getRealPath());
                    $img->resize(50, 50, function ($constraint) {
                        $constraint->aspectRatio();
                    })->save($tdestinationPath.'/'.$name);      

                    $img->destroy(); 

                } catch (Exception $e) {
                    $response_data=array('images'=>"",'success'=>0,'message'=>$e->getMessage());
            }

                $image_arr[] = $name;
            }
            $uploaded_img = implode(',',$image_arr);    
            if(!empty($hidden_property_img)){
                $uploaded_img = $hidden_property_img.','.$uploaded_img;
            }   
            $data['images'] = $uploaded_img;    

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 2019-09-05
    • 2018-04-28
    • 2021-12-22
    相关资源
    最近更新 更多