laravel 5.7 中的多个文件上传问题

Posted

技术标签:

【中文标题】laravel 5.7 中的多个文件上传问题【英文标题】:Multiple file uploads issue in laravel 5.7 【发布时间】:2020-04-28 00:18:11 【问题描述】:

我在 laravel 5.7 版中遇到与多个文件上传有关的问题。*

这就是我在 web.php 中的内容

Route::get('add/news',[
            'uses' => 'Admin\NewsController@create',
            'as'     => 'add.news'
        ]);
        Route::post('add/news',[
            'uses' => 'Admin\NewsController@store',
            'as'     => 'add.newsProcess'
        ]);

刀片文件格式代码

<form  id="addNewsForm" action = " route('add.newsProcess') " method="POST" enctype="multipart/form-data" autocomplete="off">
     csrf_field() 
    <fieldset>
     <legend>News Management Area</legend>
     <div class="form-group">
      <label class="col-form-label" for="news_title">Title</label>
      <input type="text" name="news_title" class="form-control news_title placeholder-shown" placeholder="News Title" id="news_title"> 
      </div>
      <div class="form-group">
        <label class="col-form-label" for="newsBody">Body</label>
        <textarea  name="newsBody" class="form-control newsBody placeholder-shown" id="newsBody"></textarea>
      </div>
     <div class="form-group">
        <label>Attachments <span class="optional">(Optional)</span></label>
        <div class="custom-file">
        <input type="file" name="newsAttachments[]" class="custom-file-input newsAttachments" id="tf3" multiple>
        <label class="custom-file-label persist" for="tf3">Choose Attachment</label>
       </div>
    </div>
    <div class="form-group">
      <div class="list-group-item d-flex justify-content-between align-items-center">
        <span style="margin-left: -15px;">Status</span>
        <label class="switcher-control" style="margin-right: -12px;">
          <input type="checkbox" name="is_active" class="switcher-input" checked="checked">
          <span class="switcher-indicator"></span>
        </label>
      </div>
    </div>
    <div class="form-group btn-toggle">
      <button type="submit" id="BtnSubmit" class="btn btn-primary">Submit</button>
    </div>   
</fieldset>
<div id="loader"></div>
</form>

型号代码

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class NewsTb extends Model

    protected $primaryKey  = 'news_id';
    protected $fillable    = ['title','body','attachment','published_on','is_active','user_id'];

    public function user()
        return $this->belongsTo('App\User');
    

在我的控制器中我有这段代码

public function store(Request $request)
   
    dd($request->all());

    $user_id = Auth::id();
    $news_title    = $request->news_title;
    $newsBody      = $request->newsBody;
    $is_active     = $request->is_active;
    $is_active     = $request->is_active;

    if ($is_active === "on") 
        $is_active = 1;
      
      else
          $is_active = 0;
    

    if($request->hasFile('newsAttachments'))
    
      foreach($request->file('newsAttachments') as $key => $image)
        
            print_r($image);
           $name=$image->getClientOriginalName();
           $image_encod = base64_encode(file_get_contents($request->file('newsAttachments')[$key]));
           $destinationPath = public_path('/images');
           $image->move($destinationPath, $name);

           /*$data = new Image();
           $data->image_name = $image_encod;
           $data->save();*/

           
    

我在上传多个文件时遇到此问题,但收到此错误。

ErrorException (E_WARNING)
trim() expects parameter 1 to be string, array given

请帮助我提前谢谢

【问题讨论】:

你能把你的foreach($request-&gt;file('newsAttachments') as $key =&gt; $image)转换成foreach($request-&gt;file('newsAttachments') as $image)这个试试 我也已经尝试过了,但仍然给出相同的错误。trim() 期望参数 1 是字符串,给定数组。当我 dd 请求时也会发生同样的情况。 【参考方案1】:

请更换这个

foreach($request->file('newsAttachments') as $key => $image)

使用此代码

foreach($request->file('newsAttachments') as $image)

【讨论】:

以上是关于laravel 5.7 中的多个文件上传问题的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5.7 - 上传到公共文件夹

Laravel 5.7中的电子邮件验证语言文件

Laravel 5.7 中的“无法清除缓存。请确保您具有适当的权限”

输入文件中的多个文件上传问题

在 Laravel 4 中上传多个文件

Laravel 为单个项目上传多个文件并进行验证