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.
Related
I'm trying to make a form, which I self-defined the action like this in my controller:
$form = $this->createForm(ProgrammeSearchType::class, $search, [
'action' => $this->generateUrl('recherche_programme'),
'method' => 'GET',
]);
But, the form rendered in the view look like this:
<form id="myForm">
{{fields.....}}
</form>
So.. there is a problem. Why "action" is not specified in the HTML while I defined it into the controller.
Regards
Symfony doc: https://symfony.com/doc/current/forms.html#changing-the-action-and-http-method
Use {{ form_start(form) }} and {{ form_end(form) }} instead of <form> ... </form> tags in your view template.
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'm trying to upload a file, but it fails when the request lands to the controller.
With fails i mean that if i try $request->hasFile("filename") always returns false.
Is there some specific field that I have to specify in the view?
This is a snippet of the view:
<body>
<form action="{{url('dev/tester')}}" method="POST">
{{csrf_field()}}
<input type="file" name="file">
<button type="submit">Test</button>
</form>
</body>
And here is the controller
class Tester extends Controller
{
public function index(Request $request)
{
if($request->hasFile('file'))
{
dd('Got the file');
}
dd('No file');
}
public function testView()
{
return view('tests.file_upload');
}
}
I always get returned 'No file'.
Any clue? I've even check the php.ini to see if there was a size limitation but it's all set to 32M as MAMP's pro default settings...
Check if you may have forgotten to add enctype="multipart/form-data" in form
You must enabling upload form to your form,
there is 2 ways to do it :
By using HTML
<form action="{{url('dev/tester')}}" method="post" enctype="multipart/form-data">
By using laravel Form & HTML (https://laravelcollective.com/docs/5.2/html)
{!! Form::open( [ 'action' => url( 'dev/tester' ), 'method' => 'post', 'files' => true ] ) !!}
// Your form
{!! Form::close() !!}
This should work like a charm!
Try adding the enctype="multipart/from-data" to your form, then it should work!
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">
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