laravel - 使用干预在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
解决方案
您将 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
参数来获取您的请求。
推荐阅读
- matlab - 如何在 Matlab 中使用默认或动态生成的文件名进行保存?
- aws-mobilehub - 使用 AWS Mobile Hub + AWS Amplify 会带来或增加 HIPAA 审计流程的问题吗?
- r - R-从数据框中该组的每个元素中减去该组的平均值
- python - 在python中更改x标签
- excel - 关于通过VBA导入google sheet到excel的问题
- android - genymotion + virtualbox错误
- python-3.x - Networkx - 使用边缘标签、高度和宽度属性、自定义图像导出 graphml
- r - 如何使用 sub 在 R 中提取带有首字母的名称?
- angular - Angular 5 静态注入错误
- amazon-web-services - 如何在 request_spot_instances 中附加 VPC 和子网?