Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.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/10.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 我真的需要2个上传表单,1个用于单个图像,1个用于多个图像,还是我可以只使用1个多图像表单?_Php_Laravel - Fatal编程技术网

Php 我真的需要2个上传表单,1个用于单个图像,1个用于多个图像,还是我可以只使用1个多图像表单?

Php 我真的需要2个上传表单,1个用于单个图像,1个用于多个图像,还是我可以只使用1个多图像表单?,php,laravel,Php,Laravel,目前,我有2个上传表单和2个函数,uploadImage()和上传相册()。我一直在想,我是否可以删除单一图像形式和使用两种情况下的多图像形式。如果在多图像窗体中仅选择了一个图像,则将上载一个图像,如果上载了多个图像,则将上载一个相册 这将使上传视图看起来更好,因为它不会有两个相同的上传表单,而且它只需要后端的一个功能,根据上传的图像数量来确定它是单个图像还是相册 我真的没有看到它的任何缺点,但我想在重新编写代码之前确认一下 我的上传视图: <form class='uploadForm'

目前,我有2个上传表单和2个函数,
uploadImage()
上传相册()。我一直在想,我是否可以删除单一图像形式和使用两种情况下的多图像形式。如果在多图像窗体中仅选择了一个图像,则将上载一个图像,如果上载了多个图像,则将上载一个相册

这将使上传视图看起来更好,因为它不会有两个相同的上传表单,而且它只需要后端的一个功能,根据上传的图像数量来确定它是单个图像还是相册

我真的没有看到它的任何缺点,但我想在重新编写代码之前确认一下

我的上传视图:

<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>


这当然是可能的。您必须使用允许多个字段的字段

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

提交表单时,您可以检查
$\u POST['files']
数组是否只包含一个文件。如果是,则可以使用单个文件(图像)的逻辑,如果包含更多文件,则可以使用多个文件(相册)的逻辑


当您完成此工作时,您还可以合并大部分逻辑并将其拆分为多个函数。一个会被调用为
foreach

你能给我一个例子,说明我的逻辑应该分成哪些部分吗?使用单一形式。如果只有一次上传图像,它将是多个图像,它将被视为相册。你是对的。这是另一个问题,所以如果你也需要答案,请打开另一个问题。一般来说,您可以创建一个处理和保存文件的函数,以及另一个处理多个文件的函数。后者称之为前者。
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');
    }
<input type="file" name="files[]" multiple />