I am facing an error while uploading a file it is saying that the Undefined variable: filenametostore.
This is my controller :
if(!$validator->fails()){
$cate = Product::where('name','=',$request->product_name)->first();
if($cate){
$arr = array('status'=>'false-1','message'=>'Product name already exist');
}else{
$cat = new Product();
$cat->name = $request->product_name;
$cat->category_id = $request->product_category;
$cat->brand_id = $request->product_brand;
$cat->quantity = $request->product_quantity;
$cat->buy_per_piece = $request->product_buying_per_piece;
$cat->sell_per_piece = $request->product_selling_per_piece;
if($request->hasFile('thumbnail')){
$filenameExt = $request->file('thumbnail')->getClientOriginalName();
$filename = pathinfo($filenameExt,PATHINFO_FILENAME);
$extension = $request->file('thumbnail')->getClientOriginalExtension();
$filenametostore = $filename.'_'.time().'.'.$extension;
$request->file('thumbnail')->move(public_path('images'), $filenametostore);
}
$cat->image = $filenametostore;
$cat->save();
$arr = array('status'=>'true','message'=>'Successfully Inserted');
}
}else{
$arr = array('status'=>'false','message'=>$validator->errors()->all());
}
please help me to rectify the error
Related
I want to upload and display image, but i get error
Undefined variable: image_name
This is my controller
$supply = new DataSupplyProcess;
if($request->hasFile('supply_photo')){
$photo = Validator::make($request->all(), [
'supply_photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if($photo->fails()){
return redirect()->back()->with('warning', 'Image size should be 2MB or less');
}
$image = $request->file('supply_photo');
$image_name = rand().'.'. $image->getClientOriginalExtension();
$destination_path = public_path('/item');
$image->move($destination_path, $image_name);
//dd($image);
}
$supply->item = $request->item;
$supply->supply_details = $request->supply_details;
$supply->tgl_request_date = $request->tgl_need_date;
$supply->tgl_need_date = $request->tgl_need_date;
$supply->employee_id = $id;
$supply->id_approved_by = $manager->employee_manager_id;
$supply->is_approved = 0;
$supply->is_final_approved = 0;
$supply->supply_photo = $image_name;
$supply->save();
This Is My View
<label for="supply_photo">Photo</label>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" class="form-control" name="supply_photo">
In your controller, try something like:
if(Input::file('supply_photo') !== null){
$photo = Validator::make($request->all(), [
'supply_photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
...
}
I think this post can provide more info
when ever you pass variable like this with IF condition assign default value first.
so you won't get error if image not selected.
and in your cause check first you get image or not
dd($image = $request->file('supply_photo'));
$image_name = NULL;
if($request->hasFile('supply_photo')){
$image = $request->file('supply_photo');
$image_name = rand().'.'. $image->getClientOriginalExtension();
$destination_path = public_path('/item');
$image->move($destination_path, $image_name);
}
$supply->supply_photo = $image_name;
$supply->save();
$supply = new DataSupplyProcess;
if($request->hasFile('supply_photo')){
$photo = Validator::make($request->all(), [
'supply_photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if($photo->fails()){
return redirect()->back()->with('warning', 'Image size should be 2MB or less');
}
//$original_name=$request->file('supply_photo')->getClientOriginalName();
//$size=$request->file('supply_photo')->getSize();
$extension=$request->file('supply_photo')->getClientOriginalExtension();
$filename=uniqid().'.'.$extension;
$imagepath=url('/item/'.$filename);
$path=$request->file('supply_photo')->storeAs(public_path('/item'),$filename);
}
i am using laravel 5.6, i will try to make function multiple upload video, and get frame and duration with laravel-ffmpeg, but when i try to upload one video for example, always show error like "File not found at path:",
this is my function to store video and get duration & frame :
public function doCreate($lessonsid)
{
if (empty(Session::get('contribID'))) {
return redirect('contributor/login');
}
# code...
// validate
// read more on validation at http://laravel.com/docs/validation
$rules = array(
'judul' => 'required',
// 'video.*' => 'mimes:mp4,mov,ogg,webm |required|max:100000',
// 'image.*' => 'mimes:jpeg,jpg,png,gif|required|max:30000'
);
$validator = Validator::make(Input::all(), $rules);
// process the login
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
} else {
$now = new DateTime();
$cid = Session::get('contribID');
$title = Input::get('judul');
$image_video = Input::file('image');
$lessons_video = Input::file('video');
// dd($lessons_video);
// $media = FFMpeg::open('https:/dev.cilsy.id/assets/source/lessons/lessons-74/video-8/1. Introduction (2).mp4');
// $frame = $media->getFrameFromString('00:00:13.37');
// dd($media);
$description = Input::get('desc');
$video=Video::where('lessons_id',$lessonsid)->get();
$count_video=count($video);
if (!is_dir("assets/source/lessons/lessons-$lessonsid")) {
$newforder=mkdir("assets/source/lessons/lessons-".$lessonsid);
}
$i=$count_video + 1;
foreach ($title as $key => $titles) {
$type_video =$lessons_video[$key]->getMimeType();
if (!is_dir("assets/source/lessons/lessons-".$lessonsid."/video-".$i)) {
$newforder=mkdir("assets/source/lessons/lessons-".$lessonsid."/video-".$i);
}
$DestinationPath= 'assets/source/lessons/lessons-'.$lessonsid.'/video-'.$i;
//insert image
if(!empty($image_video[$key])){
$imagefilename = $image_video[$key]->getClientOriginalName();
$image_video[$key]->move($DestinationPath, $imagefilename);
}else{
$imagefilename = '';
}
if($imagefilename ==''){
$url_image= $imagefilename;
}else{
$urls=url('');
$url_image= $urls.'/assets/source/lessons/video-'.$i.'/'.$imagefilename;
}
//insert video
if(!empty($lessons_video[$key])){
$lessonsfilename = $lessons_video[$key]->getClientOriginalName();
$lessons_video[$key]->storeAs($DestinationPath, $lessonsfilename);
}else{
$lessonsfilename = '';
}
if($lessonsfilename ==''){
$url_video= $lessonsfilename;
}else{
$urls=url('');
$url_video= $urls.'/assets/source/lessons/video-'.$i.'/'.$lessonsfilename;
}
$store = new Video;
$store->lessons_id = $lessonsid;
$store->title = $titles;
$store->image = $url_image;
$store->video = $url_video;
$store->description = $description[$key];
$store->type_video = $type_video;
$store->durasi = 0;
$store->created_at = $now;
$store->enable=1;
$store->save();
if($store){
$media = FFMpeg::open($url_video);
// $frame = FFMpeg::open($link)
// ->getFrameFromSeconds(10)
// ->export()
// ->toDisk('public')
// ->save($filename.'.png');
dd($media);
$durationInSeconds = $media->getDurationInSeconds();
// dd($media);
}
$i++;
}
// Session::set('lessons_title',$title);
// Session::set('lessons_category_id',$category_id);
// Session::set('lessons_image',$image);
// Session::set('lessons_description',$description);
return redirect('contributor/lessons/'.$lessonsid.'/view')->with('success','Penambahan video berhasil');
}
}
this is message error, when i try to upload my video
anyone can help me?
try with public_path()
$DestinationPath= public_path().'/'.'assets/source/lessons/lessons-'.$lessonsid.'/video-'.$i;
You files are not saving, you should use public_path helper for storing and retrieving files back.
$image_video[$key]->move(public_path('lessons/lessons-'.$lessonsid.'/video-'.$i), $imagefilename);
Or you can store them into storage folder
$image_video[$key]->move(storage_path('lessons/lessons-'.$lessonsid.'/video-'.$i), $imagefilename);
You can retrieve back files using these helpers.
Hope this helps.
I have some problem with updating file. I have a form with the following attributes :
title
text
pdf file
The problem is the update operation will save the pdf file as the following value :
with file : ["example.pdf"]
no file : [""]
It will include [""] to the pdf file value when updated.
I want the pdf file updated to a new file when a new file is selected, old file remained when there is no new file selected and null value to file when there is no file, to begin with.
Here is the update controller.
public function update()
{
if (Auth::check()) {
$user_id = Auth::user()->id;
$main_id = Input::get('edit_id');
$announcements = Announcement::find($main_id);
$getFile = Input::file('new_brochure');
$rules = array(
'title' => 'required',
'text' => 'required',
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return back()->withInput()
->withErrors($validator)
->withInput();
}
if ($getFile) {
$file = array('new_brochure' => Input::file('new_brochure'));
$destinationPath = 'img/brochures/announcements'; // upload path
$extension = Input::file('new_brochure')->getClientOriginalExtension();
$fileName = rand(11111,99999).'.'.$extension; // renaming image
Input::file('new_brochure')->move($destinationPath, $fileName);
$announcements->brochure = $fileName;
}
$old = Announcement::where('id',$main_id)->pluck('brochure');
if (empty($old)) {
$announcements->brochure = null;
}
else {
$announcements->brochure = $old;
}
$announcements->title = (Input:: get('title'));
$announcements->from = (Input:: get('from'));
$announcements->to = (Input:: get('to'));
$announcements->text = (Input:: get('text'));
$announcements->is_active = '1';
$announcements->created_by = $user_id;
$announcements->updated_by = $user_id;
$current_date = date("Y-m-d H:i:s");
$announcements->created_at = $current_date.".000";
if ($announcements->save()){
$this->request->session()->flash('alert-success', 'Updated successfully!');
}
else{
$this->request->session()->flash('alert-warning', 'Could not update!');
}
return redirect()->route('Announcements_view');
}
}
What am I doing wrong in this code? Please help me. Thank you.
Change this:
$old = Announcement::where('id',$main_id)->pluck('brochure');
To:
$old = Announcement::where('id',$main_id)->value('brochure');
The thing is pluck() will return a collection of brochure, not a string. And value() will return a string or null.
public function update()
{
if (Auth::check()) {
$user_id = Auth::user()->id;
$main_id = Input::get('edit_id');
$announcements = Announcement::find($main_id);
$getFile = Input::file('new_brochure');
$rules = array(
'title' => 'required',
'text' => 'required',
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return back()->withInput()
->withErrors($validator)
->withInput();
}
if (!empty(Input::file('new_brochure'))) {
$file = array('new_brochure' => Input::file('new_brochure'));
$destinationPath = 'img/brochures/announcements'; // upload path
$extension = Input::file('new_brochure')->getClientOriginalExtension();
$fileName = rand(11111,99999).'.'.$extension; // renaming image
Input::file('new_brochure')->move($destinationPath, $fileName);
$announcements->brochure = $fileName;
}
else
$old = Announcement::where('id',$main_id)->value('brochure');
$announcements->brochure = $old;
}
$announcements->title = (Input:: get('title'));
$announcements->from = (Input:: get('from'));
$announcements->to = (Input:: get('to'));
$announcements->text = (Input:: get('text'));
$announcements->is_active = '1';
$announcements->created_by = $user_id;
$announcements->updated_by = $user_id;
$current_date = date("Y-m-d H:i:s");
$announcements->created_at = $current_date.".000";
if ($announcements->save()){
$this->request->session()->flash('alert-success', 'Updated successfully!');
}
else{
$this->request->session()->flash('alert-warning', 'Could not update!');
}
return redirect()->route('Announcements_view');
}
}
So I managed to upload multiple images but now I need to display it in a page I tried something like the code below but it says "unidentified variable: photo"
<img class="img-responsive" src="/images/{{ $photo->fileName }}" >
Here's my upload code, updated my code.
Route::post('space/add', array('before' => 'auth', function()
{
$data = Input::all();
$provider_id = Auth::user()->id;
$spaces = Space::where('provider_id', '=', $provider_id)->get();
$space = new Space;
....
$space->save();
$file = Input::file('image');
$provider_email = Auth::user()->email;
// $space = Space::find($id);
$rules = array(
'file' => 'required|mimes:png,gif,jpeg'
);
$validator = \Validator::make(array('file'=> $file), $rules);
if($validator->passes())
{
foreach(Input::file('image') as $file)
{
$ext = $file->guessClientExtension(); // (Based on mime type)
$name = $file->getClientOriginalName();
$fileName = md5($name) . '.' .$ext;
$destinationPath = 'images/' . $provider_email;
$file = $file->move($destinationPath, $fileName);
$photo = new Image;
$photo->provider_id = $provider_id;
$photo->spaces_id = $space->id;
$photo->filename = $fileName;
$photo->path = $destinationPath;
$photo->save();
}
} else{
//Does not pass validation
$errors = $validator->errors();
}
return Redirect::to('user/spaces')->with(array('spaces' => $spaces, 'photo' => $photo));
}));
you need to do something like
return View::make('yourviewname')->with('photo', $photo);
this will be in your route or your controller depending on your setup
EDIT:
You will access your varaible in user/spaces through $photo = Session::get('photo') from user/spaces you will pass that variable in your with i.e. ->with('photo', $photo)
Ok, when insert data in the database, in the form of my field is to image, but if you insert data without image, appears to me the following error.
Call to a member function getClientOriginalName() on a non-object
public function store() {
$unos = Input::all();
$obavezno = array('name' => 'required',
'number' => 'required|unique:os',
'zajednica' => 'required',
'slika' => 'image|size:3000',
);
$valid = Validator::make($unos, $obavezno);
if($valid->passes()) {
$biraci = new Biraci();
$filename = Input::file('slika')->getClientOriginalName();
$biraci->name = Input::get('name');
$biraci->slika = Input::file('slika')->move('public/uploads', $filename);
$biraci->path = $filename;
$biraci->number = Input::get('number');;
$biraci->zajednica = Input::get('zajednica');
$biraci->save();
return Redirect::to('biraci/dodaj')->with(array('ok' => 'Birac je uspjesno dodat.'));
} else {
return Redirect::to('biraci/dodaj')->withErrors($valid);
}
}
Try this:
if($valid->passes()) {
$biraci = new Biraci();
if (Input::hasFile('slika')) {
$filename = Input::file('slika')->getClientOriginalName();
}
$biraci->name = Input::get('name');
$biraci->slika = isset($filename) ? Input::file('slika')->move('public/uploads', $filename); : null;
$biraci->path = isset($filename) ? $filename : null;
$biraci->number = Input::get('number');;
$biraci->zajednica = Input::get('zajednica');
$biraci->save();
}
$filename = Input::file('slika')->getClientOriginalName();
// change to
$filename = Input::hasFile('slika') ? Input::file('slika')->getClientOriginalName() : null;