I'm trying to make an image uploader, but it always give me this error
Call to a member function getClientOriginalName() on a non-object
here is my code controller code
public function uploadImageProcess(){
$destinatonPath = '';
$filename = '';
$file = Input::file('image');
$destinationPath = public_path().'/assets/images/';
$filename = str_random(6).'_'.$file->getClientOriginalName();
$uploadSuccess = $file->move($destinationPath, $filename);
if(Input::hasFile('image')){
$images = new Images;
$images->title = Input::get('title');
$images->path = '/assets/images/' . $filename;
$image->user_id = Auth::user()->id;
Session::flash('success_insert','<strong>Upload success</strong>');
return Redirect::to('user/dashboard');
}
}
and here is the upload form
<form role="form" action="{{URL::to('user/poster/upload_process')}}" method="post">
<label>Judul Poster</label>
<input class="form-control" type="text" name="title">
<label>Poster</label>
<input class="" type="file" name="image"><br/>
<input class="btn btn-primary" type="submit" >
</form>
what's wrong with my code?
You miss enctype attribute in your form markup.
Either do this
<form role="form" action="{{URL::to('user/poster/upload_process')}}" method="post" enctype="multipart/form-data">
...
</form>
or this...
{{ Form::open(array('url' => 'user/poster/upload_process', 'files' => true, 'method' => 'post')) }}
// ...
{{ Form::close() }}
These code are right, but you didn't check values of returns of Input::file('image'). I think returns value may be is not a correct object or your class Input does not have a public function name is getClientOriginalName.
Code:
$file = Input::file('image');
var_dump($file); // if return a correct object. you will check your class Input.
Good luck.
This is just because you forget to write enctype="multipart/form-data" in <form> tag.
This error happen just when you forget this:
<form class="form form-horizontal" method="post" action="{{ route('articles.store') }}" enctype="multipart/form-data">
Please Check Your Form 'files'=> true
{!! Form::open(['route' => ['Please Type Url'], 'class' => 'form-horizontal' , 'files' => true]) !!}
{!! Form::open(array('url' => '/xyz','files' => true)) !!}
{!! Form::close() !!}
if you are using Laravel Collective than you can try this solution
{{ Form::open(array('url' => 'user/poster/upload_process', 'files' => true, 'method' => 'post')) }}
{{ Form::close() }}
else if you are using html form tag than you have to put extra markdown for storing image data
<form class="form form-horizontal" method="post" action="{{ route('user/poster/upload_process') }}" enctype="multipart/form-data">
Related
First question asked in this forum.
I have watched many tutorials on file uploading in laravel, did exactly what they did but file is not uploading. It would be great of you could help me.I am posting all the relevant codes for this.
Here is my html code for taking file input and other inputs
<div id="form" >
<div id="select" style="font-size:20px;">
{{ Form::open(['route' => 'gpa.science'])}}
<div id="youtubelink" style="font-size:20px;">
<p>শিরোনাম :</p>
<h22 > {{ Form::textarea('title', null, ['size' => '70x1']) }} </h22>
</div>
</br>
<div id="youtubelink" style="font-size:20px;">
<p>ইউটিউব ভিডিও লিঙ্ক :</p>
<h22 > {{ Form::textarea('videokey', null, ['size' => '70x1']) }} </h22>
</div>
</br>
<div>
<form action="" name="filea">
<input type="file" name="filea" enctype="multipart/form-data">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</div>
</form>
<div class="input-filed">
{{ Form::submit('Submit', ['class'=>'btn btn-primary right'])}}
{{ Form::close()}}
</div>
Here is my route
Route::post('/blog1', ['as'=>'gpa.science', 'uses' => 'PageController#blogafter']);
Now after submit button this will go to this PageController.
PageController Code:
<?php namespace App\Http\Controllers;
use DB;
use App\Quotation;
use Input;
use Illuminate\Http\Request;
use App\Filename; use Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Http\UploadedFile;
class PageController extends Controller {
public function blogafter(Request $request){
//return $request->all();
if($request->hasFile('filea'))
{
dd('Got the file');
}
dd('No file');
return view('blogafter');
} }
Now the problem is it does not get any file. Always shows no file.If I do $request->all(); it returns
videokey null
filea "working.sql"
Now can anyone tell me what is wrong in my code? Why I can not upload files. I am using laravel 5.4.36 and php version 5.6.31
You have typo in your form which is missing enctype="multipart/form-data"
<form action="" name="filea" method="post" enctype="multipart/form-data" >
Add
{{ Form::open(['route' => 'gpa.science', 'files'=> true])}}
Hope this helps.
Change
{{ Form::open(['route' => 'gpa.science']) }}
to
{{ Form::open(['route' => 'gpa.science', 'files' => true]) }}
This will add enctype="multipart/form-data" to the form, which is required to upload files to PHP.
I use Laravel HTML to create form but I have problem, so in creating form I have:
{!! Form::open(['url'=>'vocuhers','files' => 'true','enctype'=>'multipart/form-data']) !!}
#include('vouchers.form',['submitButtonText'=>'Click to Add New Vocuher'])
{!! Form::close() !!}
But when I see my HTML form in browser there is just:
<form method="POST" action="http://localhost:8888/vouchers" accept-charset="UTF-8">
<input name="_token" type="hidden" value="dfgdfgdfgdfgdf">
so where is
enctype="multipart/form-data"
which allow me to upload files from form ?
Why I don't get this HTML output:
<form method="POST" action="https://bedbids.com/chats" accept-charset="UTF-8" enctype="multipart/form-data">
<input name="_token" type="hidden" value="dsfdfgdfgdfgdfg">
What is the problem here exactly ?
Your syntax is wrong. The following works for me:
{{Form::open(array('url' => 'your_url', 'method' => 'post', 'files' => true))}}
Change url with route as below.
{{!! Form::open(['route'=>'vocuhers','class'=>'your_class','files'=>true]) !!}}
{!! Form::open(['url'=>'vocuhers','files' => 'true','enctype'=>'multipart/form-data']) !!}
change It to
{!! Form::open(['url'=>'vocuhers','files' =>true,'enctype'=>'multipart/form-data']) !!}
as Markinson said
Change it to like this:
{!! Form::attributes(['enctype'=>"multipart/form-data"])open(['url'=>'vocuhers']) !!}
I'm using Laravel 5.2 and I want to make a form which can upload a pdf file with it. I want to add that file on folder "files" in "public" folder. here is my view:
<div class="form-group">
<label for="upload_file" class="control-label col-sm-3">Upload File</label>
<div class="col-sm-9">
<input class="form-control" type="file" name="upload_file" id="upload_file">
</div>
</div>
and what should I do next? what should I add in my controller and route?
First you should add enctype="multipart/form-data" to your <form> tag. Then in your controller handle the file upload as follow:
class FileController extends Controller
{
// ...
public function upload(Request $request)
{
$uniqueFileName = uniqid() . $request->get('upload_file')->getClientOriginalName() . '.' . $request->get('upload_file')->getClientOriginalExtension());
$request->get('upload_file')->move(public_path('files') . $uniqueFileName);
return redirect()->back()->with('success', 'File uploaded successfully.');
}
// ...
}
Link to Laravel Docs for Handling File Uploads
Laravel casts the file type params in request to UploadedFile objects. You can see Symfony's UploadedFile class here for available methods and attributes.
First of all, the documentation tells you exactly what to do here.
What you want to do is adding this to your <form> tag:
enctype="multipart/form-data" (This allows you to upload data), set a method(get/post) and an action (url).
Then you want to set up your routes.
For example:
Route::post('/pdf/upload', 'FileController#upload');
This way you make sure that when you send the form it will go to your FileController with upload as function.
In your controller you want to declare the file as explained in the docs.
$file = $request->file('photo');.
From this point you can do whatever you'd like to do with the file ($file). For example uploading it to your own server.
public function store(Request $request)
{
if($request->file('file'))
{
$file = $request->file('file');
$filename = time() . '.' . $request->file('file')->extension();
$filePath = public_path() . '/files/uploads/';
$file->move($filePath, $filename);
}
}
You Could Use Simple Method It Can Save The File
$path = $request->file('avatar')->store('avatars');
For More Information Here
you can this code for upload file in Laravel:
$request->file('upload_file')->move($path,$name);
You can take a look at how i upload files, all files are accepted:
first the code for the create.blade.php form
{!! Form::open(
array(
'url' => 'uploads',
'method' => 'post',
'class' => 'form',
'novalidate' => 'novalidate',
'files' => true)) !!}
#include('uploadspanel.create_form')
{!! Form::close() !!}
Remember to set files to true
Then the uploadspanel.create_form
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('file', 'Bestand:') !!}
{!! Form::file('file',null,['class'=>'form-control']) !!}
</div>
#if(\Auth::user()->level == 2)
<div class="form-group">
{{ Form::label('approved', 'Beschikbaar voor:') }}
{{ Form::select('approved', array(1 => 'Iedereen', 2 => 'monteurs', 3 => 'concept'), null, ['class' => 'form-control']) }}
</div>
#else
{{ Form::hidden('approved', 3) }}
#endif
<div class="form-group">
{!! Form::submit('Bestanden uploaden',['class' => 'btn btn-primary form-control']) !!}
</div>
then the controller store function
public function store(UploadRequest $request){
$extension = Input::file('file')->getClientOriginalExtension();
$filename = rand(11111111, 99999999). '.' . $extension;
Input::file('file')->move(
base_path().'/public/files/uploads/', $filename
);
if(\Auth::user()->level == 2) {
$approved = $request['approved'];
} else {
$approved = 3;
}
$fullPath = '/public/files/uploads/' . $filename;
$upload = new Uploads(array(
'name' => $request['name'],
'format' => $extension,
'path' => $fullPath,
'approved' => $approved,
));
$upload->save();
$uploads = Uploads::orderBy('approved')->get();
return view('uploadspanel.index', compact('uploads'));
}
I can not handle file upload forms. Sorry if it is a dummy question, but:
If I use 'files' => 'true' or 'enctype' => 'multipart/form-data' in the Forms open tag I get an object with protected properties. How can I handle the originalName, mimeType etc.. in my app?
To handle file uploads you do:
On your view:
<form action="{{ UR::route('upload') }}" method="POST" enctype="multipart/form-data">
<input type="file" name="photo" />
<input type="submit" value="Upload!">
</form>
Or in blade:
{{ Form::open(array('url' => UR::route('upload'))) }}
{{ Form::file('photo'); }}
{{ Form::submit('Upload!'); }}
{{ Form::close() }}
Then on your controller you can:
$name = Input::get('photo')->getFileName();
$size = Input::get('photo')->getClientSize();
Input::get('photo')->move(public_path().'/uploads', $name);
You can find a full list of methods in the file vendor\symfony\http-foundation\Symfony\Component\HttpFoundation\File\UploadedFile.php
I'm trying to make a form that allows uploading a file and store it in the public/upload folder of the application.
my form is:
<form action="/image/save" method="post">
<input name="image" type="file" />
<input type="submit" value="Save"/>
</form>
and the controller:
public function save(){
$file = Input::file('image');
$destinationPath = 'upload/';
$filename = $file->getClientOriginalName();
Input::file('image')->move($destinationPath, $filename);
But when I run it I get the following error:
Call to a member function getClientOriginalName() on a non-object
You need to have a file input enabled on a form. (Doc)
In Laravel, you can use:
Form::open('image/save', array('files'=> true))
or
<form action="/images/save" method="post" enctype="multipart/form-data">
Otherwise all you are receiving from the file input is the file name as a string.
If you not use Blade for generate your form like this
{{ Form::open(array('url' => 'foo/bar', 'files' => true)) }}
Just add this parameter to your form tag
enctype="multipart/form-data"
I also faced the same problem with one of my forms, the problem was the form wasn't defined with the option, 'files' => true , which tells the Laravel4 form helper to define a form with enctype="multipart/form-data"
Here is what i did:
Define the following array in your controller and pass it to your view ,
$form_options = array (
'url' => 'path/to/defined/route',
'class' => 'form-horizontal',
'name' => 'newUser',
'files' => true
);
In your view, define your form as follows:
{{ Form::open( $form_options ) }}
That will define your form with enctype="multipart/form-data"
You have to get the filename as follows:
//Blade form
{{ Form::open_for_files('/image/save') }}
{{ Form::file('image') }}
{{ Form::close() }}
//get imagename
$filename = Input::file('image.name');
Good luck! :)
You can just add the field 'files'=> true in your array() if you use Laravel form style something like
{{ Form::open(array('url'=>'/images/save','role'=>'form','files'=> true)) }}
otherwise you can use the html form tag something like
<form action="/images/save" method="post" enctype="multipart/form-data">
Hope this will help you.