How to upload pdf file in Laravel 5.2 - laravel-5

I want to upload with this controller:
if(Input::has('status-text'))
{
$text=e(Input::get('status-text'));
$rules = [
'status_text'=>'required|string',
];
$validator = Validator::make($request->all(), $rules);
if(Input::hasFile('status_image_upload'))
{
$rules['status_image_upload'] = 'image';
$validator = Validator::make($request->all(), $rules);
if($validator->fails())
{
$image = $request->file('status_image_upload');
$imageName = str_random(8).'_'.$image->getClientOriginalName();
$image->move('status_images', $imageName);
$userStatus = new Status();
$userStatus -> class_id = $id;
$userStatus -> status_text = $text;
$userStatus -> image_url = $imageName;
$userStatus -> type = 1;
$userStatus -> users_id = Auth::user()->id;
$userStatus -> save();
Flash::success('Your status has been posted');
return redirect(route('class',['class_id'=>$id]));
}
}
I know this controller will work for upload pdf. But i want to store another colunm pdf_url. Whats why i can view file and pdf in my blade page.

May this help
Don't forget to add 'files'=>'true' to the header of the form
{!! Form::open(array('url'=>'', 'method'=>'post', 'files'=>'true')) !!}
and then in the controller
if ($file = $request->hasFile('image')) {
$file = $request->file('image');
$filename = time() . '.' . $file->getClientOriginalExtension();
$destinationPath = public_path() . '/images/';
$file->move($destinationPath, $filename);
$support->image = $filename;
}
and I suggest you use this package
intervention/image

Related

RESOLVED Laravel 8 Update image and delete old image

I need help with a little thing.
I have a product table with an image:
Here is my function store which works perfectly
$fileName = null;
if (request()->hasFile('image')) {
$image = request()->file('image');
$fileName = md5($image->getClientOriginalName() . time()) . "." . $image->getClientOriginalExtension();
$image->move('./img/', $fileName);
}
Product::create([
'title' => $request->input('title'),
'subtitle' => $request->input('subtitle'),
'description' => $request->input('description'),
'price' => $request->input('price'),
'image' => $fileName,
]);
I am trying to modify the image in update but I do not know how to do it and I am in difficulty currently
Here is my update function where the image is missing to modify it and delete the old one
public function update(Request $request, Product $product)
{
$product->title = $request->input('title');
$product->subtitle = $request->input('subtitle');
$product->description = $request->input('description');
$product->price = $request->input('price');
$product->save();
Thanks
You can modify your update function like this
public function update(Request $request, Product $product)
{
$fileName = null;
$currentImage = $product->image;
if (request()->hasFile('image')) {
$image = request()->file('image');
$fileName = md5($image->getClientOriginalName() . time()) . "." . $image->getClientOriginalExtension();
$image->move('./img/', $fileName);
}
else
$fileName = $currentImage
if($fileName && $currentImage)
{
// Delete the old file i.e $fileName
}
$product->title = $request->input('title');
$product->subtitle = $request->input('subtitle');
$product->description = $request->input('description');
$product->price = $request->input('price');
$product->image = $fileName;
$product->save();
}
It's good with this :
$fileName = null;
$currentImage = $product->image;
if (request()->hasFile('image')) {
$image = request()->file('image');
$fileName = md5($image->getClientOriginalName() . time()) . "." . $image->getClientOriginalExtension();
$image->move('./img/', $fileName);
}
else
$fileName = $currentImage;
if($fileName && $currentImage)
{
Storage::delete('./img/' . $currentImage);
}
Thanks

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

I am trying to upload multiple images in laravel and facing issues as the same image is uploaded each time?

The problem is that my loop only running and uploading the same first pic each time rather than uploading each other in a row one after one!
Here is my code of the form
{!! Form::file('photos[]', ['roles' => 'form', 'class' => 'form-control-file','multiple' => true]) !!}
Here is my code of the controller
$files=$request->file('photos');
foreach ($files as $file) {
$insert = new Images;
$insert->youth_fashion_images_category = $request->selectproduct;
$destinationPath = 'uploads/products';
$imageName = 'uploads/products/'.time().'.'.$file->getClientOriginalExtension();
$insert->Save();
$uid = $insert->id;
$file->move($destinationPath,$imageName);
$image = array(
'youth_fashion_images_img' => $imageName
);
Images::where('youth_fashion_images_id',$uid)->update($image);
}
return redirect('adminpanel/viewimages');
Try this code :
if($request->hasfile('photos'))
{
foreach($request->file('photos') as $image)
{
$destinationPath = 'uploads/products';
$imageName = 'uploads/products/'.time().'.'.$image->getClientOriginalExtension();
$image->move($destinationPath,$imageName);
$insert = new Images;
$insert->youth_fashion_images_category = $request->selectproduct;
$insert->youth_fashion_images_img = $imageName;
$insert->Save();
}
}
You should try this code.
if($request->hasfile('photos')) {
foreach($request->file('photos') as $image)
{
$destinationPath = 'uploads/products';
$name = 'uploads/products/'.time().'.'.$image->getClientOriginalName();
$image->move($destinationPath, $name);
$data[] = $name;
}
}
$insert= new Images;
$insert->youth_fashion_images_img = json_encode($data);
$insert->save();
json_encode to insert the multiple image names in one row.
So add $name in array $data[] = $name;.
I hope this will helps.

Route not working properly

I have two route. First one not working and 2nd working. If I put 2nd route in first then its working and another not working.
Here is my route:
Route::any('/class',[
'uses'=> 'classroom#getclass',
'as'=>'class',]);
Route::any('/class',[
'uses'=> 'classroom#showclass',
'as'=>'class',]);
Here is my controller:
public function getclass(Request $request)
{
if (Input::has('post_comment'))
{
$status=Input::get('post_comment');
$commentBox=Input::get('comment_text');
$selectedStatus=Status::find($status);
$selectedStatus->comments()->create([
'comment_text'=>$commentBox,
'user_id'=>Auth::user()->id,
'status_id'=>$status
]);
Flash::message('Your comments has been posted');
return redirect(route('class'));
}
if(Input::has('status-text'))
{
$text=e(Input::get('status-text'));
$rules = [
'status_text'=>'required|string',
];
$validator = Validator::make($request->all(), $rules);
if(Input::hasFile('status_image_upload'))
{
$rules['status_image_upload'] = 'image';
$validator = Validator::make($request->all(), $rules);
if($validator->fails())
{
$image = $request->file('status_image_upload');
$imageName = str_random(8).'_'.$image->getClientOriginalName();
$image->move('status_images', $imageName);
$userStatus = new Status();
$userStatus -> status_text = $text;
$userStatus -> image_url = $imageName;
$userStatus -> type = 1;
$userStatus -> users_id = Auth::user()->id;
$userStatus -> save();
Flash::success('Your status has been posted');
return redirect(route('class'));
}
}
else if ($validator->fails())
{
$userStatus = new Status();
$userStatus -> status_text = $text;
$userStatus -> video_url = $request['video_url'];
$userStatus -> type = 2;
$userStatus -> users_id = Auth::user()->id;
$userStatus -> save();
Flash::success('Your status has been posted');
return redirect(route('classroom'));
}
}
return view('class',[
'posts'=>status::orderBy('id','DESC')->get()
]);}
Another one
public function showclass(Request $request)
{
$randomnumber = rand(50001,1000000);
$classrooms = new Classrooms();
$classrooms->class_name = $request['class_name'];
$classrooms->subject_name = $request['subject_name'];
$classrooms->section = $request['section'];
$classrooms->class_code = $randomnumber;
$classrooms -> user_id = Auth::user()->id;
$classrooms -> save();
return view('class', array('class' => Auth::user()) );
}
What Should to do now? i think my tow url is same that is the problem.If i am right then how can i solve this problem?
The issue is that you are using Route::any().
As the name suggests, it will accept any request (POST, GET, PUT, PATCH, DELETE) to the /class URI and process that with the given controller function.
In your case, when you have this first:
Route::any('/class',[
'uses'=> 'classroom#getclass',
'as'=>'class',]);
Any request to to /class is just being processed by getClass, including your form submission.
And it works fine when you put the other first as that is the first one being used.
Try changing Route::any() to the type of request they actually get.
For example, something like this:
Route::get('/class',[
'uses'=> 'classroom#getclass',
'as'=>'class',]);
Route::post('/class',[
'uses'=> 'classroom#showclass',
'as'=>'class',]);
** NOTE: you cannot use the same URI (e.g /class) for two of the same method types (e.g GET) as the first route would always match first and be used.

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