Let's say that I want to upload 3 images a.jpeg, b.jpeg and c.jpeg
I've successfully selected the images that i want to upload.
Sent them to the Controller and checked with print_f() to see if they're actually to the controller. They were.
Then when I've checked my img folder only c.jpeg were successfully uploaded while rest are not.
What is the reason of this?
// AdminController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Redirect;
use Image;
// Route
Route::post('/upload', 'AdminController#store')->name('upload');
// AdminController#store:
public function store(Request $request){
if ($request->hasfile('images')) {
foreach ($request->images as $image) {
$name = time() . '.' . $image->getClientOriginalExtension();
print_r($name."<br>"); // to see if they're actually passed.
Image::make($image)->save(public_path('img/new/'. $name));
}
}
return('done');
}
<form action="{{ route('upload') }}" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" name="images[]" id="exampleInputFile" multiple />
</div>
{{ csrf_field() }}
<button type="submit" class="btn btn-default">Submit</button>
</form>
Problem was that I was using the time() function and not rand() or any similar function that returns a random value.
So the code shoud look like this now:
public function store(Request $request){
if ($request->hasfile('images')) {
foreach ($request->images as $image) {
$name = rand() . '.' . $image->getClientOriginalExtension();
print_r($name."<br>"); // to see if they're actually passed.
Image::make($image)->save(public_path('img/new/'. $name));
}
}
return('done');
}
Related
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');
}
here the user transfers the photo from the device to the form
it is necessary that when registering a user he entered a photo and this photo could be viewed by the administrator in the admin area
web.php
Route::post('/head', 'ImageController#upload')->name('image.upload');
ImageController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ImageController extends Controller
{
public function upload(Request $request)
{
$path = $request - file('image')->store('upload', 'public');
return view('default',['path'=> $path]);
}
}
(php artisan storage:link) entered
input form
<div class="form-group row">
<label for="ydostak" class="col-md-4 col-form-label text-md-right">{{ __('Фото удостоверение с двух сторон') }}</label>
<form action{{ route('image.upload')}}="" method="post" accept-charset="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<input type="file" name="image">
</div>
</form>
</div>
withdrawal form
#isset ($path)
<img class="img-fluid" scr="{{asset('/storage/'. $path)}}" alt="">
#endisset
public function upload(Request $request)
{
$user = new user ;
if($request->hasFile('image')){
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image)->resize(300, 300)->save( storage_path('/uploads/' . $filename ) );
$user->image = $filename;
$user->save();
};
$user->save();
return redirect()->route('user.index')
->with('success','User created successfully');
}
Your form tag is incorrectly formed:
<form action{{ route('image.upload')}}="" method="post" accept-charset="multipart/form-data">
You are not assigning anything to the action as what you have isn't the action attribute. You have something like this:
<form actionhttp://yoursite/head="" ...>
Which isn't the action attribute. You want:
<form action="{{ route('image.upload') }}" ...>
Which will give you something like:
<form action="http://youriste/head" ...>
Once you fix that you can move onto actually dealing with your Controller method.
I keep on getting this error when I try to upload a pdf document file, does anybody have any idea how to solve this?
I had tried looking for similar problems but still can't solve it(eg: Upload pdf file using Laravel 5)
I tried doing dd() to see if the file have been uploaded and it did show the file name but the error said Call to a member function getClientOriginalName() on null, so now I'm kind of confused on what to do now.
Here are my codes, thanks in advance for helping.
Controller:
class CreateController extends Controller
{
public function create(){
return view('create');
}
public function store(Request $request){
$uniqueFileName = uniqid() . $request->get('upload_file')->getClientOriginalName() . '.' . $request->get('upload_file')->getClientOriginalExtension();
$request->get('upload_file')->move(public_path('files') . $uniqueFileName);
//dd($request);
return redirect()->back()->with('success', 'File uploaded successfully.');
}
create.blade.php
<form enctype="multipart/form-data" class="form-horizontal" method="post" action="{{ url('/user')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="upload_file" class="control-label col-sm-3">Upload File</label>
<div class="col-sm-9">
<input class="form-control" type="file" name="upload_file" id="upload_file">
</div>
</div>
<div class="form-group">
<div class="col-md-6-offset-2">
<input type="submit" class="btn btn-primary" value="Save">
</div>
</div>
</form>
Route:
Route::get('/user/create','CreateController#create');
Route::post('/user','CreateController#store');
This enctype solved function call on the null problem
<form method="" action="" enctype="multipart/form-data">
You need to use the file() function to have access to your uploaded file
$request->file('upload_file')
use this code in your tag form enctype="multipart/form-data"
<form method="" action="" enctype="multipart/form-data">
Try to put the file into a var.
$file = $request->file('upload_file');
And get the extension and name from it.
$uniqueFileName = uniqid() . $file->getClientOriginalName() . '.' . $file->getClientOriginalExtension();
Hope it helps.
The key to solving this problem is that you must write getClientOriginalName() function in the if clause just like that:
if($request->hasFile('logoImage')){
$logoImage = $request->file('logoImage');
$name = $logoImage->getClientOriginalName();
}
if($request->has('sound_file')) {
$image = $request->file('sound_file');
$filename = $image->getClientOriginalName();
request()->$image->move(public_path('images/users'), $filename);
$sounds->image = $request->file('sound_file')->getClientOriginalName();
}
Using Laravel 5.4, I'm tring to setup a form where a user can enter a food name and its image. While trying to upload a PNG or JPG image, I get the following Validation Error:
The image must be an image.
If I remove the validation for image, I get a FatalErrorException:
Call to a member function getClientOriginalExtension() on null
which is a helper function provided by Intervention/Image library. This could mean that the image didn't upload at all.
FORM
<form action="/hq/foods" method="POST">
{{ csrf_field() }}
<input type="text" name="name">
<input type="file" name="image">
<button type="submit" class="btn btn-success ">
ADD FOOD ITEM
</button>
</form>
CONTROLLER
public function store(Request $request) {
$this->validate($request, [
'name' => 'required|min:2|max:255',
'image' => 'required|image'
]);
$food_category = FoodCategory::find($request->food_category_id);
$food = new Food;
$food->name = $request->name;
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('img/foods/' . $filename);
Image::make($image)->resize(800, 400)->save($location);
$food->image = $filename;
$food->save();
Session::flash('success',
'
<h4>Success!</h4>
<p>The food item has been added to the Menu.</p>
');
return back();
}
What am I missing?
To upload images you need to add:
enctype="multipart/form-data"
to your form so instead of:
<form action="/hq/foods" method="POST">
you should use
<form action="/hq/foods" method="POST" enctype="multipart/form-data">
If you use Laravelcollective
{!! Form::open(['method'=>'post','files' => true,'url'=>'/hq/foods']) !!}
OR
Using HTML
<form action="/hq/foods" method="POST" enctype="multipart/form-data">
you need to add enctype="multipart/form-data" in your from
I have a 2 tables, Courses and Lessons:
Course:
id, user_id, title
Lessons:
id, course_id , title
And I have updated their Eloquent Relationship.
Now my problem is, how to create a lesson without using a parameters in the form? Because i think it's not a good practice and prone to security issues, like editing the html tag.
<form method="POST" action="{{url('/lesson/store/3')}}" class="form-horizontal" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label class="control-label col-sm-2" for="title">Title:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="title" name="title" placeholder="Enter title">
</div>
</div>
</form>
From my route:
Route::group(['prefix' => 'lesson'] , function(){
Route::get('create/{course_id}' , 'LessonController#create');
Route::post('store/{course_id}' , 'LessonController#store');
});
And my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Course;
use App\Lesson;
class LessonController extends Controller
{
public function create($course_id)
{
$course = Course::find($course_id);
return view('lesson.create' , compact('course'));
}
public function store(Request $request, $course_id)
{
$lesson = new Lesson;
$lesson->title = $request->title;
$lesson->course_id = $course_id;
$lesson->description = $request->description;
$lesson->episode = $request->episode;
if($request->hasFile('video'))
{
$file = $request->file('video');
$extension = $file->getClientOriginalExtension();
$video = 'course' . $course_id . '_ep' . $request->episode . $extension;
$destinationPath = public_path() . '/uploads/lessons/';
$file->move($destinationPath, $video);
$lesson->video = $video;
}
$lesson->save();
return redirect('course/show/' . $course_id);
}
}
and also, hidden input is not also advisable.
Another option is to have a hidden input with the id which will be passed with the form submission to your controller.