Upload image on laravel - image

add an image I can not load because it creates a new folder outside the public folder
$input = Input::all();
$polje = array('naslov' => 'required');
$provjera = Validator::make($input, $polje);
if($provjera->passes()){
$file = Input::file('file');
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('file')->move(base_path().'/images/vijesti', $filename);
$vijest = new Vijesti();
$vijest->naslov = $input['naslov'];
$vijest->slika = $uploadSuccess;
$vijest->tekst = $input['tekst'];
$vijest->tag = $input['tag'];
$vijest->kategorija_id = Input::get('kategorija_id');
$vijest->save();
return Redirect::to('admin/vijesti/dodaj')->withInput()->with('ok', 'Vijest je uspjesno dodata.');
}else {
return Redirect::to('admin/vijesti/dodaj')->with('no', 'Greska: morate pokusati ponovo, polje naslov i slika su obavezna polja.');
}

You are using the wrong function. Use public_path() instead of base_path().
$uploadSuccess = Input::file('file')->move(public_path().'/images/vijesti', $filename);
Hope that helps :)

Related

How to convert base64 images to Url images when using summer note text editor in Laravel inside controller?

I am trying to use image url instead of base64. i kindof figured a way how to get images out of the editor and save it inside the directory, but i am trying to insert it into database. i get this error.
Object of class DOMElement could not be converted to string
below i have added the store function inside my controller
public function store(Request $request)
{
//
$data = $request->all();
if (!$request->has('published')) {
$data['published'] = 0;
} else {
$data['published'] = 1;
}
if (!$request->has('featured')) {
$data['featured'] = 0;
} else {
$data['featured'] = 1;
}
$img = null;
if ($request->hasfile('title_img')) {
$image = $request->file('title_img');
$name = time() . '.' . $image->getClientOriginalExtension();
$image->move(public_path() . '/uploads/blogs/', $name);
$img = '/uploads/blogs/' . $name;
}
/* summernote */
$detail=$data['description'];
$dom = new \DomDocument();
$dom->loadHtml($detail, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$images = $dom->getElementsByTagName('img');
foreach($images as $k => $img){
$data = $img->getAttribute('src');
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
$image_name= "/uploads/summernote/" . time().$k.'.jpeg';
$path = public_path() . $image_name;
file_put_contents($path, $data);
$img->removeAttribute('src');
$img->setAttribute('src', $image_name);
}
$detail = $dom->saveHTML();
/* summernote */
$data['title_img'] = $img;
$data['user_id'] = auth()->user()->id;
$title = $data['title'];
$data['slug'] = Str::slug($title);
auth()->user()->posts()->create($data);
return Redirect::route('admin.blog.index')->withSuccess('Whoopie!! New Blog Added!');
}
I hope i can get some help regarding this. Thanks in advance

Laravel upload gifs

i am uploading gif for my posts in laravel but gif is like an image its not moving or something like this
<?php
if($request->hasFile('gif')){
$gif = $request->file('gif');
$gif_filename = time() . '.' . $gif->getClientOriginalName();
$gif_location = public_path('/images/' . $gif_filename);
Image::make($gif)->save($gif_location);
}
$post->gif = $gif_filename;
$post->save();
?>
here is the code what I am using I think everything is kinda correct
I use this method
if(Input::hasFile('imagen')) {
$time = Carbon::now()->format('Y-m-d');
$image = $request->file('imagen');
$extension = $image->getClientOriginalExtension();
$name = $image->getClientOriginalName();
$fileName = $time."-".$name;
$image->move(storage_path(),$fileName);
}
Please try this and let me know how it works :)
An easy way to update and save is to do it like this:
public function store(Request $request)
{
$imgLocation = Storage::disk('public')->put(time() . '.' . $request->file('image')->getClientOriginalName(), $request->gif), $request->file('gif'));
// This would save it to the gifs table if you need something like it, otherwise skip this creation
$gif= Gif::create([
'name' => $request->name,
'path' => $imgLocation
]);
if ($gif) {
return response()->json("Success!");
}
return response()->json("Error!"); // or you return redirect()...
}
$image = $request->file('image');
if(isset($image)) {
if($image->getClientOriginalExtension()=='gif'){
$image = $request->file('image');
$extension = $image->getClientOriginalExtension();
$name = $image->getClientOriginalName();
$fileName = 'exerciseimages'."-".$name;
$image->move('storage/courseimages/',$fileName);
}
else{
$fileName = 'exerciseimages'.'-'.uniqid().'.'.$image->getClientOriginalExtension();
if(!Storage::disk('public')->exists('courseimages')){
Storage::disk('public')->makeDirectory('courseimages');
}
$amenitiesimg = Image::make($image)->resize(250,250)->stream();
Storage::disk('public')->put('courseimages/'.$fileName, $amenitiesimg);
}
}
else {
$fileName = 'default.png';
}

How to handle update on image upload

recently i'm trying to practice with laravel 5.5 and create simple upload and update image
here is my controller on store
$input = $request->all();
$this->validate($request, [
'photo' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:500',
]);
if ($request->hasFile('photo')) {
$photo = $request->file('photo');
$ext = $photo->getClientOriginalExtension();
if ($request->file('photo')->isValid()) {
$photo_name = date('YmdHis'). ".$ext";
$photoName = time().'.'.$request->photo->getClientOriginalExtension();
$request->photo->move(public_path('photo-upload'), $photoName);
$upload_path = public_path('photo-upload');
$input['photo'] = $photoName;
}
}
controller update
if ($request->hasFile('photo')) {
// delete the old/previous one
$exist = Storage::disk('photo')->exists($student->photo);
if (isset($student->photo) && $exist) {
$delete = Storage::disk('photo')->delete($siswa->photo);
}
// upload new one
$photo = $request->file('photo');
$ext = $photo->getClientOriginalExtension();
$photoName = time().'.'.$request->foto->getClientOriginalExtension();
if ($request->file('photo')->isValid()) {
$upload_path = 'photo-upload';
$request->file('photo')->move($upload_path, $phptpName);
$input['photo'] = $iphotoName;
}
}
the store method works perfectly, it uploaded image to the right folder then update the databse
the problem is on the update, it update the database but it didn't upload the image to the photo-upload folder
Any suggestion?
Thanks

i m saving the multiple images at a single time with differnt ID in database in Laravel

and it is saved but the issue is all the images have different auto
increment ids in database i want to save the images with the same ids
which is save at the same time,kindly guide me.
****strong text** the code is below;**
$field = Input::file('image');
$location = public_path('uploads/');
foreach ($field as $k => $fd) {
$filename = str_random(2).'-' . time() . "." . $fd->getClientOriginalExtension();
$fd->move($location , $filename);
$product->image = $filename;
}
$user->save();
I think bellow code helps you..
You can try.
$picture = '';
if ($request->hasFile('image')) {
$files = $request->file('image');
foreach($files as $file){
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$picture = date('His').$filename;
$destinationPath = base_path() . '\public\images';
$file->move($destinationPath, $picture);
}
}
if (!empty($product['images'])) {
$product['images'] = $picture;
} else {
unset($product['images']);
}

laravel uploading pic path showing zero

WHen uploading, all data is inserted in table except pic_path column contains 0. Am i doing wrong when processing file uploaded:
This is the controller:
if(Session::has('userLogged'))
{
if($_POST)
{
$file;
if (Input::hasFile('pic'))
{
$file = Input::file('pic');
print_r($file);
}
$validator = $this->validate(Input::all(), $rules);
if ($validator != null)
{
return Redirect::to('basicDetails')->withErrors($validator);
}else{
$title = Input::get('title');
$summary = Input::get('summary');
$description = Input::get('description');
$destinationPath = base_path() . '/public/img';
$file->move($destinationPath);
$path = $file->getRealPath();
$personModel = new Course;
$personeModel->basicDetails($title, $summary, $description, $path);
}
and this is form url:
{{ Form::open(array('action' => 'PersonController#basic','files' => true)) }}
Thanks,
Before move the file get the path:
$title = Input::get('title');
$summary = Input::get('summary');
$description = Input::get('description');
$destinationPath = base_path() . '/public/img';
$path = $file->getRealPath();
$file->move($destinationPath);
$personModel = new Course;
$personeModel->basicDetails($title, $summary, $description, $path);
I am not sure why you need uploaded path? Usually this is the temp path where Laravel uploaded the file.
http://laravel.com/docs/requests#files

Resources