Laravel 5.5.40 file upload not working - php

I have downgrade laravel from 5.6 to 5.5.40 successfully.But now I have a problem : before downrading laravel input file was working, now it's not. When I write $request->file('image') it returns null.
I have set enctype but still is not working. The same form was working with laravel 5.6 ! Only input with type="file" is not working. Is there any one that can help me ?
view :
<form action="{{route('apply')}}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="file" name="image"/>
<br>
</br >
<input type="submit" name="submit" value="Send"/>
</form >
in Controller :
public function apply(Request $request)
{
return $request->all();
}
It returns:
{
"_token":"o3s4YrXK2L98gU3H6JWFGSWbPfCdm7Z4JsM5azK3",
"submit":"Send",
"image":{}
}

Try This code to upload a file
public function apply(Request $request)
{
$file = $request->image;
//getting timestamp
$timestamp = time().str_random(20);
$name = $timestamp. '-' .$file
->getClientOriginalName();
$file->move(public_path().'/images/', $name);
}

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;

Can't edit the database, Laravel

I have an edit form with a text field and an image field where a user can edit and upload new text or a image if he/she wants to. But if the user does not upload a new image I just want to keep the old image in the database.
My Problem
The problem is when I hit upload button, I'm redirected to the article page and no error is shown but the DB has not been updated what so ever.
Any help would be appreciated as I have tried multiple methods with no success.
Thank you in advance.
web.php
Route::get('/tests/edit/{id}', 'TestController#edit');
Route::patch('/tests', 'TestController#update');
TestController.php
public function edit(Request $request)
{
$test = Test::findOrFail($request->id);
return view('test.edit', ['test' => $test]);
}
public function update(Request $request, Test $test)
{
$test->user_id = $request->user()->id;
$test->name = $request->name;
if($request->hasFile('image')) {
Storage::delete('public/image/' . $test->image); //Delete old image
$path = $request->file('image')->store('public/image');
$test->image = basename($path);
}
$test->update();
return redirect('/tests')
}
edit.blade.php
#extends('layouts.app')
#section('content')
<div>
<form action="/tests" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('patch') }}
name:<input type="text" name="name" value='{{ $test->name }}'><br>
image: <input type="file" name="image"><br>
<input type='submit' value='upload'>
</form>
</div>
#endsection
edit.blade.php add this :
<input type="hidden" name="id" value="{{ $test->id }}">
TestController.php
use $test->save(); instead of $test->update();
I had the same problem as you.I work on a ordering website with laravel that you can get data from laravel and show it to user and also he can order something from the website and the database will be updated.But, I cannot neither get or update data in database.I work with wampserver, mysql and the problem is solved once I switched to MariaDB instead of mysql in phpmyadmin: http://localhost/phpmyadmin/

File not uploading

I was working on a project and was trying to develop a file uploading system for skins.
When I tried to upload my skin, I was given "Call to a member function storeAs() on null"
public function uploadSkin(Request $request)
{
/* $request->validate([
'skins' => 'required|mimes:png|max:1024',
]); */
$storage_dir = storage_path('app/skins');
$request->file('skins')->storeAs($storage_dir, Auth::user->name . '.png');
return route('settings')->with('success', 'skin uploaded :)');
}
Form code:
<form method="post" enctype="multipart/form-data" action="/settings">
#csrf
<br/>
<div class="form-group">
<input type="file" class="form-control-file" id="skins" name="skins" required>
</div>
<button type="submit" class="btn btn-success">Upload</button>
</form>
To store a file like an image or any kind of files you can use a code like this:
public function uploadSkin(Request $request){
$image = $request->file('skins');
if ($image != null) {
$image->move('uploads/skins/', Auth::user()->name . $image->getClientOriginalExtension());
}
return route('settings')->with('success', 'skin uploaded :)');
}
To uppload a file there are various ways in the laravel but for now you can try this to simply move your file to your directory:
if($files= $request->file('skins')){
$files->move('uploads/skins/', Auth::user->name . '.png');
}

upload file didn't work in laravel 5.1

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

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