upload file didn't work in laravel 5.1 - php

I want to upload a .CSV file to insert some records to my Database.
Unfortunately, it doesn't work.
My Routes:
Route::get('excel/import','Backend\ExcelController#getImport');
Route::post('excel/import','Backend\ExcelController#postImport');
My Controller:
public function getImport(){
$csrf_field = csrf_field();
$postUrl='import';
$html = <<<CREATE
<form action="$postUrl" method="post">
$csrf_field
<input type="file" name="importCsv" formenctype="multipart/form-data" ><br/><br/>
<input type="submit" value="submit"/>
</form>
CREATE;
return $html;
}
public function postImport(Request $request){
//get file
$file = Input::file('importCsv');
dd($file);
$upload=$request->file('importCsv');
dd($upload);
}
It just print null if I use :
Input::file('importCsv');<br>
And nothing if I use :
$request ->file('importCsv');<br><br>
So, I've tried to print like dd($request->all()); <br>
array:2 [▼
"_token" => "wPuAXUvSItR4MFJ4bAjQhanaf0W9avrqR2PgjxcU"
"importCsv" => "T_OpusDef.csv"
]
I could get only the name, and when I want the the path by getRealPath(), It told me :
FatalErrorexception: call to a memeber function getRealPath() on null
I need your help, Thanks a lot

The form should look like this:
<form action="{{ $postUrl }}" method="post" enctype="multipart/form-data">
{{ $csrf_field }}
<input type="file" name="importCsv"><br/><br/>
<input type="submit" value="submit">
</form>

use {{csrf_field()}} instead of $csrf_field if its in blade and csrf_field() if its in controller

Related

simple input type file not getting the file

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;

laravel 8 , How to display data from a FORM with post method?

my Route :
Route::resource('/posts',PostsControllerWithAll::class);
my form :
i try to read the given data from this form
<form method="POST" action="{{ action('PostsControllerWithAll#store') }}">
#csrf
<input type="text" name="title" placeholder="enter title">
<input type="submit" name="submit">
</form>
my controller :
public function create()
{
return view("posts.create");
}
public function store(Request $request)
{
return $request->all();
}
and this is my route list
route list image , please check the link image
Try using the named route, like you show on your print screen.
<form method="POST" action="{{ action('posts.store') }}">...</form>
To validated the fields values on store method, use dd($request->all()) this dump the variable and die, it's good to see what is expected.

Laravel post route with parameters

Im trying to do a post request via a form in my blade template with a parameter in the url .
Here is my form:
<form action="/products/{{$produit->id}}" method="post">
{{ csrf_field() }}
<input type="text" name="avis_body" />
<input type="submit" value="Send" />
</form>
Here is my web.php:
Route::post('/products/{$id}', function($id){
return $id;
});
I don't know what im doing wrong. Im receiving this error:
Remove the $ from route declaration:
Route::post('/products/{id}', function($id){
return $id;
});

Laravel form post to controller

I am new to Laravel and I'm having trouble with posting data to a controller. I couldn't find the corresponding documentation. I want to something similar in Laravel that I do in C# MVC.
<form action="/someurl" method="post">
<input type="text" name="someName" />
<input type="submit">
</form>
Controller
[HttpPost]
public ActionResult SomeUrl(string someName)
{
...
}
You should use route.
your .html
<form action="{{url('someurl')}}" method="post">
<input type="text" name="someName" />
<input type="submit">
</form>
in routes.php
Route::post('someurl', 'YourController#someMethod');
and finally in YourController.php
public function someMethod(Request $request)
{
dd($request->all()); //to check all the datas dumped from the form
//if your want to get single element,someName in this case
$someName = $request->someName;
}
This works best
<form action="{{url('someurl')}}" method="post">
#csrf
<input type="text" name="someName" />
<input type="submit">
</form>
in web.php
Route::post('someurl', 'YourController#someMethod');
and in your Controller
public function someMethod(Request $request)
{
dd($request->all()); //to check all the datas dumped from the form
//if your want to get single element,someName in this case
$someName = $request->someName;
}

Uploading images with laravel 5.2

I am trying to create a simple upload images function for my laravel project however I keep getting this error:
FatalThrowableError in UploadController.php line 23:
Fatal error: Call to a member function hasFile() on null
This is my controller:
public function uploadImg(Request $request){
$input = $request->input();
if($input->hasFile('file')){
echo 'Uploading';
$file = $input->file('file');
$file->move('uploads', $file->getClientOriginalName());
echo 'Uploaded';
}
}
This is my form:
<form action="/admin/media/uploadImg" method="post" enctype="multipart/form-data">
<label>Select image to upload:</label>
<input type="file" name="file" id="file">
<input type="submit" value="Upload" name="submit">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</form>
The hasFile() method only works on the request object, not the input array. Try this instead:
if($request->hasFile('file')){
See: https://laravel.com/docs/5.2/requests#files
You'll also need to change this line:
$file = $input->file('file');
To:
$file = $request->file('file');

Categories