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!
Related
Hi im new to laravel 9 and im trying to upload file from view to controller, but somehow the input file didnt pass the file to controller.
im using laravel 9 with boostrap in it.
heres my code :
view
<form enctype="multipart/form-data" action="{{ route('profile.put') }}" method="POST">
#csrf
#method('POST')
<div class="card-body">
<input id="file" name="file" type="file">
<button type="submit" class="btn btn-block btn-primary">{{ __('Save') }}</button>
</div>
</form>
controller
public function put(Request $request){
return $request;
}
i try to see the $request variable but the result is like this
enter image description here
i try the solution like add enctype="multipart/form-data" but still didnt work.
thank you in advance
I think you simply should get the request by its name or using `$request->all()`, you can do these two things:
In your controller you should write your put method like below:
public function put(Request $request){
$file = '';
if($request->file('file'))
{
$file = $request->get('file);
}
return $file;
}
or you can use the below code in your put method
public function put(Request $request){
return $request->all();
}
Note: If you don't send the file as a JSON Response you should return it to view after saving operation;
and also, there is no need to put #method('POST') inside the form;
I have a form that I post file. I am trying to use validation to accept only word documents. I tried using mime types however doesn't seem to work and I couldn't spot my mistake.
<form action="" method="post">
<div class="form-group{{ $errors->has('myFile') ? ' has-error' : '' }}">
<div class="col-xs-12">
<div class="form-material">
<input class="form-control" type="file" id="myFile" name="myFile">
<label for="myFile">MyFile</label>
#if ($errors->has('myFile'))
<div {{ $errors->first('myFile') }}</div>
#endif
</div>
</div>
</div>
</form>
This posts successfully and I receive it in my controller.
In my controller, I try to validate it, however it is always returning the error, even though the file is in '.doc' format.
public function myController(Request $request) {
$validator = MyValidations::myFormValidator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
...
}
and the validator:
public static function myFormValidator($data) {
return Validator::make($data, [
'myFile' => 'required|mimes:application/msword'
// I also tried
// 'myFile' => 'required|mimes:doc,docx'
]);
}
Edit: I have seen some posts on SO, regarding that there is a file config > mimes.php but I don't have it on Laravel 5.3
Actually, there is nothing really wrong with your validation code. The problem is that your form is not submitting the files properly.
Add the proper enctype to your form like so:
<form action="" method="post" enctype="multipart/form-data">
Also make sure to get the syntax right for validation (but I think you knew this part). Use either of these two:
'myFile' => 'required|mimes:doc,docx'
'myFile' => 'required|mimetypes:application/msword'
Try this:
'myFile': 'required|mimes:doc,docx'
When i want to insert data in the database i get this error
MethodNotAllowedHttpException in RouteCollection.php line 219
I'm use resource controller
this is my form
<form action="library" method="POST" enctype="multipart/form-data">
{!! csrf_field() !!}
Enter the name of section: <input type="text" name="section_name"> <br>
Upload an image: <input type="file" name="image"> <br>
<button type="submit" class="btn btn-default">Create Section</button>
</form>
and this is my store function
public function store(Request $request)
{
$section_name = $request->input('section_name');
$file = $request->file('image');
$destenationPath = 'iamges';
$filename = $file->getClientOriginalName();
$file->move($destenationPath, $filename);
DB::table('sections')->insert(['section_name' => $section_name, 'image_name' => $filename]);
return redirect('admin');
}
and this my Route
Route::resource('library', 'Main');
You are using action="library", so the form is submitted to library. But, here is nothing to deal with library. You need to submit the form to store() method in Mian controller.
Change action="library" to action="{{ action('Main#store') }}" in form starting tag.
add this route='library.store' to your form:
<form method="POST" route="library.store" enctype="multipart/form-data" files="true">
and your rout should be:
Route::resource('library', 'controller_class_name');
Change you route to:
Route::resource('library', 'MainController');
Also, check out your controller. It should be placed into app\Http\Controllers directory, named MainController.php and it should include this code:
class MainController extends Controller
{
....
public function store(Request $request)
....
}
I am setting up a simple form in laravel:
This is the route file:
Route::get('backoffice/upload', [ 'as' => 'backoffice/upload',
'uses' => 'UploadController#uploadForm']);
Route::post('backoffice/saveimage',[ 'as' => 'backoffice/saveimage',
'uses' => 'UploadController#saveImage']);
This is the controller:
class UploadController extends \BaseController
{
public function uploadForm()
{
return View::make("backoffice.upload.create");
}
public function saveImage()
{
return "Uploading...";
}
}
And this is the View file:
<h1>Upload Image</h1>
{{ Form::open(['action' => 'UploadController#saveImage']) }}
<div class='formfield'>
{{ Form::label('newfilename','New File Name (optional):') }}
{{ Form::input('text','newfilename') }}
{{ $errors->first('newfilename') }}
</div>
<div class='formfield'>
{{ Form::submit($action,['class'=>'button']) }}
{{ Form::btnLink('Cancel',URL::previous(),['class'=>'button']) }}
</div>
{{ Form::close() }}
// Generated HTML
<h1>Upload Image</h1>
<form method="POST" action="http://my.local/backoffice/saveimage" accept-charset="UTF-8"><input name="_token" type="hidden" value="x9g4SW2R7t9kia2B8HRJTm1jbLRl3BB8sPMwvgAM">
<div class='formfield'>
<label for="newfilename">New File Name (optional):</label>
<input name="newfilename" type="text" id="newfilename">
</div>
<div class='formfield'>
<input class="button" type="submit" value="Create">
</div>
</form>
So, if I go to: http://my.local/backoffice/upload I get the form with the HTML above.
However, if I type anything, then click SUBMIT, I return to the form but now have the following URL:
http://my.local/backoffice/upload?pz_session=x9g4SW2R7t9kia2B8HRJTm1jbLRl3BB8sPMwvgAM&_token=x9g4SW2R7t9kia2B8HRJTm1jbLRl3BB8sPMwvgAM&newfilename=ddd
This makes no sense to me. Up until now I have always used route::resource when dealing with forms, and had no problem. I am trying to do a simple form with GET and POST and am having no end of grief. What am I missing?
Furthermore, if I modify routes.php and change it from post to any, then open a browser window and type: http://my.local/backoffice/saveimage then I get the message "Uploading..." so that part is working ok.
Found the solution. In making the backoffice of the system, I had re-used the frontoffice template but removed all the excess. Or so I had thought. However, the front office header template had a form which I had only partially deleted.
So the problem was that there was an opening FORM tag I didn't know about. Consequently, when I clicked on submit to my form, it was actually submitting to this other form.
As the other form had no action it was default to itself.
Of course, had I just validated the HTML this would have shown up straight away. The lesson learned here is to validate my html before submitting questions!
Try this, and be sure to correctly configure your url at app/config/app.php
{{Form::open(['url'=>'backoffice/saveimage'])}}
//code
{{Form::close()}}
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.