Uploading images with laravel 5.2 - php

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');

Related

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');
}

How to Solve Error: Call to a member function move() on string

I am uploading an image to a folder and saving data to the database. How can I solve this error? The error is in the following line...
$property_features_image->move($destinationPath,$property_features_image);
public function store(Request $request)
{
$this->validate($request, [
'property_feature' => 'required|unique:property_features,property_features_name',
'property_icon' => 'required|image|mimes:jpg,png,jpeg|max:10240',
]);
$property_features_name = $request->property_feature;
$property_features_image = $request->file('property_icon');
$property_features_image = $property_features_image->getClientOriginalExtension();
$destinationPath = public_path('/images');
$property_features_image->move($destinationPath, $property_features_image);
DB::table('property_features')->insert([
'property_features_name' => $property_features_name,
'property_features_image' => $property_features_image,
]);
}
Blade
<form method="post" enctype="multipart/form-data" action="{{url('/admin/property-features')}}">
<div class="form-group">
<input type="hidden" value="{{csrf_token()}}" name="_token"/>
<label for="Property">Add Property Feature</label>
<input type="text" class="form-control" id="property_feature" name="property_feature"
placeholder="Enter Property Feature">
<label for="exampleFormControlFile1">Property Features's Icon</label>
<input type="file" class="form-control-file" id="property_icon" name="property_icon">
<?php echo($errors->first('property_feature', "<li class='ab' style='color:red;'>:message</li>"));?>
<?php echo($errors->first('property_icon', "<li class='ab' style='color:red;'>:message</li>"));?>
</div>
</form>
This line:
$property_features_image = $property_features_image->getClientOriginalExtension();
Assigns $property_features_image variable to a string value.
And strings cannot have any methods (you are trying to call move() method).
So removing the line should help, but then you must ensure everything else is ok. The getClientOriginalExtension() might be required somewhere, but we don't see all the code.

Laravel 5.5.40 file upload not working

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);
}

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

Laravel 5 and redactor file uploads

I am using the following code to upload image in Laravel 5 with redactor, nothing happends, Do I need to create form request class for this as well ?
Route::post('redactorUpload', function(App\Http\Requests $request)
{
$file = $request->file('file');
$fileName = $file->getClientOriginalName();
$request->file('file')->move(public_path().'/uploads', $fileName);
return Response::json(array('filelink' => '/uploads/' . $fileName));
});
You can solve this by using Request class, the static way...i.e.
Route::post('redactorUpload', function()
{
$file = Request::file('file');
$fileName = $file->getClientOriginalName();
$file->move(public_path().'/uploads', $fileName);
return Response::json(array('filelink' => '/uploads/' . $fileName));
});
I assume that you have created the form with method "post" and action "redactorUpload" and enctype to "multipart/form-data"
Example Form :
<form action="redactorUpload" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="file" name="file" id="file">
<input type="submit" value="Upload File" name="submit">
</form>

Categories