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>
Related
I've looked at code from other people and try to implement the same thing, but the application could not store the image to the desire folder, would you please have a look at what the reason is? Thank you!
Here is my code for route:
Route::post('upload_pic', 'UploadController#storePhoto');
Here is my code for the php laravel template:
<div class="panel-body">
<form action="{{url('/upload_pic')}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="upload-user-photo">Upload Photos</label>
<input type="file" name="image" class="form-control" placeholder="Upload Student Image, Size:207(W)x408(H)">
</div>
<div class="row">
<div class="col-sm-4">
<input class="btn btn-success" type="submit" value="Upload Student Photo" name="submit">
</div>
</div>
</form>
</div>
Here is my code in the controller:
public function storePhoto(Request $request){
$valid = $request->validate([
'image' => 'required|image|mimes:jpg,png,jpeg,|dimensions:max_width=272,max_height=408,min_width=271,min_height=407'
]);
$data = new Postimage();
$file = $request->file('image');
$filename = $file->getClientOriginalName();
// dd($filename);
$file -> move(public_path('./public/img/student_photos'),$filename);
$data['image'] = $filename;
$data->save();
return redirect('/upload')->with('success', 'Photo Uploaded');
}
Your problem is somewhere here
$filename = $file->getClientOriginalName();
$file -> move(public_path('./public/img/student_photos'),$filename);
First, the file comprises only the file extension. Instead, you should have something like this $filename = 'name.' . $file->getClientOriginalName(); Notice the dot in 'name.'
Secondly, no need to add public to the file path string. So it should be $file->move(public_path('img/student_photos'),$filename);
Finally, make sure the upload folder exists and is writeable
i want to insert link of an image into a field in the database like annonces\August2020\image.jpg in storage, but it gives me error:The POST method is not supported for this route. Supported methods: GET, HEAD.
file.blade.php
<form action="/file" method="post" enctype="multipart/form-data">
#csrf
<div class="form-group">
<input type="file" class="form-control-file" name="file" id="file" aria-describedby="fileHelp">
<small id="fileHelp" class="form-text text-muted">Please upload a valid image file. Size of image should not be more than 2MB.</small>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
web.php
Route::get('file', 'fileController#index');
Route::get('save', 'fileController#save');
FileController.php
public function save()
{
request()->validate(['file' => 'required|mimes:doc,docx,pdf,txt|max:2048',]);
if ($files = $request->file('fileUpload')) {
$destinationPath = 'public/file/'; // upload path
$profilefile = date('YmdHis') . "." . $files->getClientOriginalExtension();
$files->move($destinationPath, $profilefile);
$insert['file'] = "$profilefile";
}
$check = Document::insertGetId($insert);
return Redirect::to("file")
->withSuccess('Great! file has been successfully uploaded.');
}
as the error said, you need to declare the route as POST
Route::post('save', 'fileController#save');
Also, put the correct route into the form action attribute
<form action="/save" method="post" enctype="multipart/form-data">
you are using post method in the form and then in the web.php you are using get method which is wrong.
in We.php use something like below:
Route::post('save', 'fileController#save');
Also please change the action attribute value to save which is a url in web.php because you are calling save method in the Controller
<form action="/save" method="post" enctype="multipart/form-data">
I can get photos to upload as a user,
but I need to be able to pick that photo I just uploaded and display it as an avatar?
the problem is all the photos from all the users go into the same public folder so not sure how to show only the photo that the user uploaded
I am using Laravel
here is my form:
<h1>File Upload</h1>
<form class="picwid" action="{{ URL::to('../user/edituser') }}" 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>
here is my function:
public function uploads(Request $request) {
$users = DB::table('users')->where('id', '=', auth()->id())->latest()->get();
if(Input::hasFile('file')){
$file = Input::file('file');
$file->move('profilepics', $file->getClientOriginalName());
$myImage = '<img src="profilepics"/>' . $file->getClientOriginalName();
}
return view('edituser', compact(['baskets', 'myImage']));
}
and here is my avatar output:
<div class="media-left">
<img src="../profilepics/ty.jpg" class="media-object" style="width:60px">
</div>
I changed the function uploads to:
public function uploads() {
$users = DB::table('users')->where('id', '=', auth()->id())->latest()->get();
$users_id = auth()->id();
if(Input::hasFile('file')){
$file = Input::file('file');
$file->move("profilepics/ $users_id", $file->getClientOriginalName());
}
return view('edituser', compact(['baskets', 'myImage', 'users_id']));
}
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 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');