douxiexie3574 2018-07-31 08:42
浏览 55

我是否真的需要2个上传表格,1个用于单张图片,1张用于多张图片,或者我可以只使用1张多图像表格吗?

Currently, I have 2 upload forms and 2 functions, uploadImage(); and uploadAlbum();. I have been wondering if I could remove the single image form and use the multi image form for both cases. If only 1 image is selected in the multi image form, a single image would be uploaded and if more than 1 images are uploaded, an album would be uploaded.

That would make the upload view look better since it won't have 2 identical upload forms and it would only require 1 function on the back-end that would determine whether it's a single image or an album based on the amount of images uploaded.

I don't really see any downsides to it but I wanted to make sure before reworking the code.

My upload view:

<form class='uploadForm' action="{{ route('imageUpload') }}" method="POST" enctype="multipart/form-data">
        <label for="name">Image Name</label>
        <input class='input' type="text" name="name" placeholder="Image Name">

        <label for="description">Image Description</label>
        <input class='input' type="text" name="description" placeholder="Description">

        <input type="file" name="image"> {{ csrf_field() }}
        <button class='Submit' type="submit" name="submit">UPLOAD</button>
    </form>

    <form class='uploadForm' action="{{ route('albumUpload') }}" method="POST" enctype="multipart/form-data">
        <label for="albumName">Album Name</label>
        <input class='input' type="text" name="albumName" placeholder="Album Name">

        <label for="albumDescription">Image Description</label>
        <input class='input' type="text" name="albumDescription" placeholder="Description">

        <input type="file" name='files[]' multiple> {{ csrf_field() }}
        <button class='Submit' type="submit" name="submit">UPLOAD</button>
    </form>

My uploadImage() and uploadeAlbum() functions:

public function uploadAlbum(Request $request){
        $name = $request['albumName'];
        $description = $request['albumDescription'];
        $tag = $request['tags'];
        $userId = auth()->user()->id;
        $files = $request->file('files');
        $path = 'storage/uploads/albums/'.$name;

        $fileOriginalName = $files[0]->getClientOriginalName();
        $fileName = pathinfo($fileOriginalName, PATHINFO_FILENAME);
        $extension = $files[0]->getClientOriginalExtension();
        $fileNameToStore = $fileName.'_'.time().'.'.$extension;
        $fileNameToStore = str_replace(' ', '', $fileNameToStore);

        $album = new Album();
        $album->name = $name;
        $album->description = $description;
        $album->user_id = $userId;
        $album->thumbnail = $fileNameToStore;

        $album->save();
        $album->tags()->attach($tag);

        if(!File::exists($path)) {
            File::makeDirectory(public_path($path));
        }

        if (is_array($files) || is_object($files)){
            foreach ($files as $file){
                $fileOriginalName = $file->getClientOriginalName();
                $fileName = pathinfo($fileOriginalName, PATHINFO_FILENAME);
                $extension = $file->getClientOriginalExtension();
                $fileNameToStore = $fileName.'_'.time().'.'.$extension;
                $fileNameToStore = str_replace(' ', '', $fileNameToStore);

                $file->storeAs('public/uploads/albums/'.$name, $fileNameToStore);
                $file->storeAs('public/uploads/albums/'.$name.'/thumbnails/', $fileNameToStore);

                $thumbnailImage = InterventionImage::make('storage/uploads/albums/'.$name.'/thumbnails/'.$fileNameToStore)->fit(400, 400, function ($constraint) {
                    $constraint->upsize();
                });

                $thumbnailImage->save();

                $albumImage = new AlbumImage();
                $albumImage->file_name = $fileNameToStore;
                $albumImage->album_id = $album->id;

                $albumImage->save();
            }
        }
        return redirect()->route('albums');
    }

public function uploadImage(Request $request){
        $this->validate($request, [
            'name' => 'required|max:120',
            'description' => 'max:120|nullable',
            'image' => 'required'
        ]);

        $name = $request['name'];
        $description = $request['description'];
        $tag = $request['tags'];
        $userId = auth()->user()->id;
        $file = $request->file('image')->getClientOriginalName();
        $fileName = pathinfo($file, PATHINFO_FILENAME);
        $extension = $request->file('image')->getClientOriginalExtension();
        $fileNameToStore = $fileName.'_'.time().'.'.$extension;
        $fileNameToStore = str_replace(' ', '', $fileNameToStore);

        $request->file('image')->storeAs('public/uploads/images/',$fileNameToStore);
        $request->file('image')->storeAs('public/uploads/images/thumbnails/',$fileNameToStore);
        $request->file('image')->storeAs('public/uploads/images/specificImages/',$fileNameToStore);
        $request->file('image')->storeAs('public/uploads/images/miniImages/',$fileNameToStore);

        $thumbnail = InterventionImage::make('storage/uploads/images/thumbnails/'.$fileNameToStore )->resize(500, null, function ($constraint) {
            $constraint->aspectRatio();
            $constraint->upsize();
        });

        $thumbnail->save();

        $specificImage = InterventionImage::make('storage/uploads/images/specificImages/'.$fileNameToStore )->resize(2000, null, function ($constraint) {
            $constraint->aspectRatio();
            $constraint->upsize();
        });

        $specificImage->save();

        $miniImage = InterventionImage::make('storage/uploads/images/miniImages/'.$fileNameToStore )->fit(200, 200, function ($constraint) {
            $constraint->upsize();
        });

        $miniImage->save();

        $image = new Image();
        $image->name = $name;
        $image->description = $description;
        $image->user_id = $userId;
        $image->file_name = $fileNameToStore;

        $image->save();
        $image->tags()->attach($tag);

        return redirect()->route('home');
    }
  • 写回答

1条回答 默认 最新

  • dousao2186 2018-07-31 08:47
    关注

    This is possible of course. You would have to use the field that allows multiple

    <input type="file" name="files[]" multiple />
    

    When submitting the form you can check for if the $_POST['files'] array contains only one file. If it does, you can use the logic of a single file (image) and if it contains more you can use the logic of multiple files (album).

    When you have this working you can also merge the majority of your logic and split it into multiple functions. One would be called with a foreach.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测