foreach statement causes undefined variable fileNameToStore in multiple file upload

Viewed 95

Im trying to upload more than a single file so I try to modify the code a little bit. But everytime I'm adding foreach($request->file('image') as $file). I always get an error about having undefined variable. I attached my controller code below, Any idea how can I fix this and make the multiple file upload?

    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!');

Here is the 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()!!}
2 Answers

If I correct understand you. Try to add fileNameToStore variable in 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!');

Hope help you. PS replace image with cover_image.

your getting Undefined variable: fileNameToStore it means you not getting file and still try to access $fileNameToStore so you need to add this to inside if condition like this

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!');

NOTE this is only solution for Undefined variable: fileNameToStore not upload image thing

Related