首页 > 解决方案 > 使用干预在laravel中上传多个文件

问题描述

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Image;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;

class TestController extends Controller
  {
 /**
  * Show the application dashboard.
  *
 * @return \Illuminate\Http\Response
  */
  public function index(Request $req)
 {

 if(isset($_POST['upload'])){
    $filename = $_FILES['imagefile']['name'];
   foreach($filename as $file){

   $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
    $location = public_path('/images/test/' . $withoutExt);
   $img = Image::make($file)->resize('720', '404')->save($location.'.jpg');
  }
  }}}

我已经使用了$img = Image::make(Input::file('imagefile'))->resize('720', '404')->save($location.'.jpg');单个文件并且它工作正常但是对于多个文件上传我使用$file而不是使用Input然后它显示错误Image source not readable

标签: laraveluploadintervention

解决方案


您将 var 定义为$filename = $_FILES['imagefile']['name'];这样不会只获得一个字符串值吗?并且字符串不可读。让我们尝试一下$filename = $_FILES['imagefile']

更新

您从中循环字符串值filename,它不是可读的图像。您需要一些其他的东西来处理图像上传。

if($req->hasfile('imagefile'))
{
    foreach($req->file('imagefile') as $file)
    {
        $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file->getClientOriginalName());
        $location = public_path('/images/test/' . $withoutExt);
        $img = Image::make($file)->resize('720', '404')->save($location.'.jpg');
    }
}

而且因为您使用 Laravel,我建议您只使用$req参数来获取您的请求。


推荐阅读