微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

foreach 语句在多文件上传中导致未定义的变量 fileNameToStore

如何解决foreach 语句在多文件上传中导致未定义的变量 fileNameToStore

我正在尝试上传多个文件,因此我尝试稍微修改代码。但每次我添加 foreach($request->file('image') as $file)。我总是收到有关未定义变量的错误。我在下面附上了我的控制器代码,知道如何解决这个问题并上传多个文件吗?

    if($request->hasFile('image'))
    {
        foreach($request->file('image') as $file)
        {
        $filenameWithExt =$request->file('image')->getClientOriginalName();
        $filename = pathinfo($filenameWithExt,PATHINFO_FILENAME);
        $extension = $request->file('image')->getClientOriginalExtension();
        $fileNametoStore = $filename. '_'.time().'.'.$extension;
        $path = $request->file('image')->storeAs('public/images',$fileNametoStore);
        }
    }
    $post = new Post;
    $post->image = $fileNametoStore;
    $post->save();
    return redirect('/dashboard')->with('success','Post Created!');

这里是 create.blade.PHP

@extends('layouts.app')

@section('content')
    <h1>Upload Images</h1>
    {!! Form::open(['action' => 'PostsController@store','method'=> 'POST','enctype'=>'multipart/form-data'])!!}
    <div class="form-group">
        {{Form::file('image',['multiple'=>'true','files'=>'true'])}}
    </div>
    
   {{Form::submit('Submit',['class' => 'btn btn-success'])}}
   {!!Form::close()!!}

解决方法

如果我正确理解你。尝试在 fileNameToStore 中添加 foreach 变量:

$errors = [];
$post = new Post;
if ($request->hasFile('cover_image')) {
    foreach ($request->file('cover_image') as $file) {
        $filenameWithExt =$request->file('image')->getClientOriginalName();
        $filename = pathinfo($filenameWithExt,PATHINFO_FILENAME);
        $extension = $request->file('image')->getClientOriginalExtension();
        $fileNameToStore = $filename. '_'.time().'.'.$extension;
        $path = $request->file('image')->move('public/images',$fileNameToStore);
        $post->cover_image = $fileNameToStore;
    }
    if (! fileNameToStore) {
        $errors[] = false;
    }
    if (count($errors) > 0) {
        return redirect('/dashboard')->with('error','Somthing went wrong!');
    }
}
$post->save()
return redirect('/dashboard')->with('success','Post Created!');

希望能帮到你。 PS 将 image 替换为 cover_image

,

你得到 Undefined variable: fileNameToStore 这意味着你没有得到文件并且仍然尝试访问 $fileNameToStore 所以你需要把它添加到里面 if 像这样的条件

if ($request->hasFile('image')) {
    foreach ($request->file('image') as $file) {

        $filenameWithExt = $request->file('image')->getClientOriginalName();
        $filename = pathinfo($filenameWithExt,PATHINFO_FILENAME);
        $extension = $request->file('image')->getClientOriginalExtension();
        $fileNameToStore = $filename . '_' . time() . '.' . $extension;
        $path = $request->file('image')->storeAs('public/images',$fileNameToStore);
    }
    $post = new Post;
    $post->cover_image = $fileNameToStore;
    $post->save();
    return redirect('/dashboard')->with('success','Post Created!');
}
return redirect('/dashboard')->with('error','Somthing went wrong!');

注意这只是Undefined variable: fileNameToStore不上传图片的解决方案

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。