how to add images into folder using laravel 4 - validation

Iam new in laravel. 1st Im create a new folder (gallery) in public/img/uploads/gallery.
code is $f1=Input::get('f1');
$path ='img/uploads/'.$f1;
File::makeDirectory($path, 0777, true, true);
Then Im trying to insert image to this gallery folder.
1st Im create a select box to etch all the folders from public/img/uploads/ using this code { Form::select('f1', $list, null, array('class' => 'form-control')) }}
and enter gallery name(textbox) ,browse image(file).then im submit. the details are goto database .but the image not go to gallery folder ,but create a new folder &image save into that folder. using controller.php is here else
{ $f1=Input::get('f1');
$file=File::directories('/img/uploads/', '=', $f1)->first();
$file=Input::file('galimg');
$filename=$file->getClientOriginalName();
$file->move('img/uploads/'.$f1,$filename);
ForumGallery::create([
'galname'=>Input::get('galname'),
'galimg'=>$f1.'/'.$filename
]);return Redirect::route('addnewgallery')
->with('success',' Gallery Details Entered succesfully');}
i Want image save into gallery(selcted) folder and databse.
How to save image into selected folder??

Related

Vue js and laravel keep an uploaded image permanently

I wrote a code to upload an image file and that is already working, I also can save the image url in my database. The problem is the image is not loaded permanently. Which means I can see the image after I uploaded it, but the moment I save it, the url is not valid anymore. Does anyone know how to fix it? I mean store the image permanetly on my project. I want users to upload and use their own images? Thank you for youre help.
uploading in template
<input style="margin-left: 35px" type="file" #change="onFileChange" />
Displaying the image
<img v-if="data.image" :src="data.image" style="max-width:450px;vertical-align:middle;margin:0px 5px 5px"/>
script method
onFileChange()e {
const file = e.target.files[0];
this.data.image = URL.createObjectURL(file);
},```
You could do something like this in your Controller:
$request->validate([
'image' => 'required|image|mimes:jpeg,jpg,png,gif|max:2048',
]);
$imageName = time().'.'.$request->image->extension(); //What you store in DB
$request->image->move(public_path('images'), $imageName); //Put the image in a public folder
And from there you could access it via URL.

How to keep original image name in Laravel image file upload

I am trying to upload or update an image file.but this code is changing the name of original image into a hexa number.How can i keep the file name same in DB and my project images folder?
$image = $request->file('cus_image');
if(($image)){
$data=array();
$image_name=hexdec(uniqid());
$ext=strtolower($image->getClientOriginalExtension());
$image_full_name=$image_name.'.'.$ext;
$upload_path='images/';
$image_url=$upload_path.$image_full_name;
$success=$image->move($upload_path,$image_full_name);
$data['pic']=$image_full_name;
DB::table('customer')
->where('Cus_id',$Cus_id)
->update($data);
}
Change $image_name=hexdec(uniqid()); to $image_name = $image->getClientOriginalName();

Remove unused Images/files from upload folder laravel

I have laravel5.4 application.I want to remove unused images/files from my upload folder which is not available in my database.
For example :
I have 50 images in my upload folder for user profile but some of the image not use for any user.i think he removed or update his image from frontend.
Yes i know we need to code to remove file when user update or remove profile picture at a time also delete from upload folder.but my app run from many time and i want to remove unused file using script not manually beacause i have lot's of files so it's hard to check and remove file manually.anyone can you please help me for create any function for remove file from folder.
Sorry for my bad English.
I use something like this in my AdminController to remove images by clicking on a button.
Maybe you need to change the path or extensions
public function deleteUnusedImages()
{
$file_types = [
'gif',
'jpg',
'jpeg',
'png'
];
$directory = public_path();
$files = File::allFiles($directory);
foreach ($files as $file)
{
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (in_array($ext, $file_types)) {
if(DB::table('users')->where('votes', '=', $file)->count())
continue; // continue if the picture is in use
echo 'removed' . basename($file)."<br />";
unlink($file); // delete if picture isn't in use
}
}
}

How to create a folder inside a folder using laravel

In new in laravel.
I trying to create a folder inside a folder using laravel.I want to create a new folder inside the another folder .what is the html code for blade.php page.
you can use File::makeDirectory to create folder inside folder
$path = public_path().'/uploads/test';
File::makeDirectory($path,0777, true, true);
List All Directory for user john
main directory john_1
1.1 john has 3 sub directory abc,xyz and mno
get all directory of user john and pass it view from controller
$list = File::directories('/john_1');
return View::make('upload',array('list' => $list));
finally display it as selectbox
{{ Form::select('folder_name', $list, null, array('class' => 'form-control')) }}
list all image from user john
foreach (File::allFiles(public_path().'/john_1/img/') as $file) {
$filename = $file->getRelativePathName();
echo HTML::image('public/john_1/img/'.$filename, $filename);
}
This will attempt to create /uploads if it doesn't exist. Then
/uploads/test if it doesn't exist.If all created directories are
successfully created then true will be returned.

I am using the upload function of codeigniter but how do I attach the image uploaded on the server once I edit that item?

I have been successful in using the File Uploading Library to upload my files on the server (mostly images).
My problem is when I need to update that specific item, I need to browse for photo again. Is there a way I can just attach the image uploaded already in the server to my upload button? I am thinking to make a media page like that of the wordpress but is there any starting point that anyone knows who can point me to? Thanks so much!
You can use a hidden field...check this example
View
<?php echo form_upload("vLogo", $campaign->vLogo , "id='vLogo'"); ?>
<?php echo form_hidden("vLogo_old", $campaign->vLogo , "id='vLogo_old'"); ?>
Controller
(Edit function)
//The way you do when you click on browse and select a file
if(isset($_FILES['vLogo']['tmp_name']) && !empty($_FILES['vLogo']['tmp_name']))
{
$this->load->library('upload', $config);
// Initialaizing Logo upload
$this->upload->initialize($config);
if ( !$this->upload->do_upload('vLogo')){
$logo_error = array('vLogo' => $this->upload->display_errors());
}
else{
$data1 = array('upload_data' => $this->upload->data());
}
}
else
{
//The browse filed is empty..So hidden input is taken
$data1['upload_data']['file_name'] = $post['vLogo_old'];
}

Resources