what is the exact error, and what line is it on?
Jul 4, 2018
6
Level 1
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
Please or to participate in this conversation.