如何使用 Laravel 正确上传一组图像

Posted

技术标签:

【中文标题】如何使用 Laravel 正确上传一组图像【英文标题】:How to upload an array of images with Laravel properly 【发布时间】:2021-12-20 12:56:33 【问题描述】:

我有这样的表格:

<form action=" route('popups.store') " method="POST" enctype="multipart/form-data">
    @csrf
    <div id="dynamic_field">
        <label>Date of showing</label>
        <input type="text" id="date" name="datep" class="form-control datepicker" value="" autofocus>
        
        <label for="title" class="control-label">Title</label>
        <input type="text" id="title" name="title" class="form-control" value="" autofocus>

        <label for="link" class="control-label">Link</label>
        <input type="text" id="link" name="linkp[]" class="form-control" value="" autofocus>

        <label for="bio" class="control-label">Text</label>
        <textarea class="form-control" name="bio[]" rows="3"></textarea>

        <label for="filep" class="control-label">Image</label>
        <input type="file" class="form-control-file" id="filep" name="filep[]">

        <button class="btn btn-success" type="submit">Submit</button>

        <a id="add" class="btn btn-info" style="color:white">Add new form</a>
    </div>                                      
</form>

如您所见,我添加了一个带有 id="add" 的链接,用于使用 javascript 动态添加其他表单字段。

这意味着用户可以添加多个图片文本链接。这就是为什么我使用filep[]bio[]linkp[] 作为这些表单字段的名称(以便将它们保存为数组)。

然后在控制器处,我添加了这个:

  public function store(Request $request)
    
        try
            $data = $request->validate([
                'datep' => 'nullable',
                'title' => 'nullable',
                'linkp' => 'nullable',
                'bio' => 'nullable',
                'filep' => 'nullable',
            ]);
            
            $newPop = Popup::create([
                'datep' => $data['datep'],
                'title' => $data['title']
            ]);
            
            $files = $request->file('filep');

            if($request->hasFile('filep'))
            
                foreach ($files as $file) 
                    $newImageName = time() . '-' . $request->name . '.' . $request->filep->extension();
                    $request->filep->move(public_path('popups'), $newImageName);
                    Popup::where('id',$newPop->id)->update(['image_path'=>",".$newImageName]);
                
            

        catch (\Exception $e) 
            dd($e);
        
        
        return redirect()->back();
    

因此,对于每张图片,我尝试将其移动到 public/popups 文件夹中,然后它应该更新 ID 为 $newPop-&gt;id 的表的现有记录。

但它显示了这个错误:

调用数组上的成员函数 extension()

指的是这一行:

$newImageName = time() . '-' . $request-&gt;name . '.' . $request-&gt;filep-&gt;extension();

那么这里出了什么问题?如何使用数组上传多张图片?

【问题讨论】:

$file 不是 $filep $newImageName = time() 。 '-' 。 $请求->名称。 '。' . $file->extension(); @ZrelliMajdi 我刚刚将其更改为 $newImageName = time() . '-' . $request-&gt;name . '.' . $request-&gt;file-&gt;extension(); 但再次显示错误! $file->extension() 不是 $request->file 。 $file 是您数组中的项目文件 @ZrelliMajdi 你能把这个写成答案吗 你的数据库结构是什么? Popup::where('id',$newPop-&gt;id)-&gt;update(['image_path'=&gt;",".$newImageName]);你只会得到一张图片。 【参考方案1】:

你可以试试这个-

    if($files=$request->file('filep'))

    foreach ($files as $key=>$file) 
        $extension = $file->getClientOriginalName();
        $fileName = time().'-' .$request->name.'.'.$extension; // I don't know where did you get this $request->name, I didn't find it on your code.
       
        $created = Popup::create([
            'datep' => $request->datep[$key],
            'title' => $request->title[$key],
            'image_path' => $fileName
        ]);

        if($created)

            // $file->move('popups',$fileName); to store in public folder

            //  If you want to keep files in storage folder, you can use following : -

            Storage::disk('public')->put('popups/'.$location,File::get($file));

            // Dont't forget to run 'php artisan storage:link'
            // It will store into your storage folder and you can access it by Storage::url('file_path)
            

        else
            // Your error message.
        
    

【讨论】:

在此处使用文件名 -Storage::disk('public')-&gt;put('popups/'.$fileName,File::get($file));

以上是关于如何使用 Laravel 正确上传一组图像的主要内容,如果未能解决你的问题,请参考以下文章

如何从 PowerPoint 文件文件创建一组图像文件?

将一组图像上传到 coredata

如何使用基于一组图像的 opencv 训练 SVM?

Android:如何从一组图像制作视频

如何将一组图像拼接成 MxN 图像拼贴

如何在 HTML/CSS 中居中一组图像?