geekshubh's avatar

File upload error

Hi guys here is my controller

 // Handle File Upload
        if($request->hasFile('file_url')){
            // Get filename with the extension
            $filenameWithExt = $request->file('file_url')->getClientOriginalName();
            // Get just filename
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            // Get just ext
            $extension = $request->file('file_url')->getClientOriginalExtension();
            // Filename to store
            $fileNameToStore= $filename.'_'.time().'.'.$extension;
            // Upload Image
            $path = $request->file('file_url')->storeAs('public/assignments', $fileNameToStore);
        }         
    // Create Book
        $book = new Library;
        $book->title = $request->input('title');
        $book->author = $request->input('author');
        $book->type = $request->input('type');
        $book->availability = $request->input('availability');
        $book->file_url = $fileNameToStore;
        $book->save();
        return redirect('/library')->with('success', 'Book Created');

Here is my form:-

@extends('layouts.app')
@if(Auth::user()->isAdmin() || Auth::user()->isLibrarian())
@section('content')
    <h3 class="page-title">Add Book</h3>
    {!! Form::open(['method' => 'POST','enctype'=> 'multipart/data', 'route' => ['library.store']]) !!}

    <div class="panel panel-default">
        <div class="panel-heading">
            @lang('quickadmin.create')
        </div>

        <div class="panel-body">
            <div class="row">
                <div class="col-xs-12 form-group">
                    {!! Form::label('title', 'Title*', ['class' => 'control-label']) !!}
                    {!! Form::text('title', old('title'), ['class' => 'form-control','placeholder' => 'Title']) !!}

                </div>
            </div>

            <div class="row">
                <div class="col-md-12 form-group">
                    {!! Form::label('author', 'Author *', ['class' => 'col-sm-2 control-label']) !!}
                    {!! Form::text('author', old('author'), ['class' => 'col-sm-10 form-control ', 'placeholder' => 'Author']) !!}

                </div>
            </div>
            <div class="row">
                <div class="col-md-12 form-group">
                    {!! Form::label('type', 'Type *:', ['class' => 'col-sm-2 control-label']) !!}
                    {!! Form::select('type', array('E-book' => 'E-book', 'Hardcover' => 'Hardcover'), ['class' => 'col-sm-10 form-control']) !!}
                </div>
            </div>
            <div class="row">
                <div class="col-xs-12 form-group">
                    {!! Form::label('availability', 'Availability *', ['class' => 'col-sm-2 control-label']) !!}
                    {!! Form::select('availability', array('Available' => 'Available', 'Unavailable' => 'Unavailable'), ['class' => 'col-sm-10 form-control']) !!}
                </div>
            </div>
            <div class="row">
                <div class="col-xs-12 form-group">
                    {!! Form::label('Upload E-Book', 'E-book Upload *', ['class' => 'col-sm-2 control-label']) !!}
                    {!! Form::file('file_url') !!}
                </div>
            </div>
        </div>
    </div>

    {!! Form::submit(trans('quickadmin.save'), ['class' => 'btn btn-success']) !!}
    {!! Form::close() !!}
@endif
@stop

The issue is that when I click on submit to save it to DB it says variable fileNameToStore not found

I have created the linking between the folder storage and public

0 likes
6 replies
Snapey's avatar

what is the exact error, and what line is it on?

geekshubh's avatar

The error is on the controller On the following line:-

 $book->file_url = $fileNameToStore;
Snapey's avatar

but what is the exact error message

Snapey's avatar

ok, so all the stuff dealing with the file upload is inside an if() . If there is no file then the filename variable is not initialised

Snapey's avatar

Well thats the problem with your code. You have one path through the code where the variable is initialised and one where it is not

Anyhow as you don't seem capable of answering a simple question, I'm not sure I can help any further.

Please or to participate in this conversation.