简体   繁体   English

我实际上是否需要2张上传表格,一张用于单张图像,一张用于多张图像,还是可以只使用1张多张图像表格?

[英]Do I actually need 2 upload forms, 1 for single images and 1 for many images or can I just use 1 multi image form?

Currently, I have 2 upload forms and 2 functions, uploadImage(); 目前,我有2个上传表单和2个函数uploadImage(); and uploadAlbum(); 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. 如果在多图像形式中仅选择1张图像,则将上载一张图像,如果上载1张以上,则将上载相册。

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. 这将使上传视图看起来更好,因为它将没有2个相同的上传表单,并且只需要后端的1个功能即可根据上传的图像数量确定是单张图像还是相册。

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: 我的uploadImage()uploadeAlbum()函数:

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

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. 提交表单时,您可以检查$_POST['files']数组是否仅包含一个文件。 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 . 一个人会被称为foreach

暂无
暂无

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

相关问题 当我选择许多图像时,php多图像上传失败 - Php multiple image upload fails when i select many images 如何使用Codeigniter中的不同按钮以单一形式上传多个图像和文本文件? - How do I upload multiple images and text file in a single form with the different button in Codeigniter? 我需要让我的网站用户上传图片 - 我可以在不将文件夹权限设置为777的情况下执行此操作 - I need to enable users of my site to upload images - can i do it without setting the folder permissions to 777 我实际上可以在上传表单中使用此代码段吗? - Can I actually use this snippet of code in my upload form? 我需要一个mysql数据库来上传临时使用的图像吗? - Do I need a mysql database to upload images used temporarily? 如何使用PHP表单将图像上传到邮件 - How can i upload images to mail using PHP form 如何设置图像轮播中可以显示的图像数量限制? (PHP) - How do I set a limit on how many images can be shown in an image carousel? (PHP) 我如何将这个新生成的图像上传到我的本地主机/图像/ - how can i upload this new generated image to my localhost/images/ Codeigniter 3-我可以从3张图片中获取图片上传的网址吗? - Codeigniter 3 - can I get image upload's url from 3 images? 如何以正常的插入形式(MySql)上传图像? 上传后,图像应该有三个不同大小和不同名称的版本 - How can I upload images in a normal insert form (MySql)? after upload the image should have three versions of different sizes and different names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM