How show images in laravel - laravel

I want to show all images that are in a particular folder, I have the folder path but I do not see how the number of images there are inside and how show all. Someone could help me?

If your files are inside public or a subfolder of it, this is is a way:
foreach (File::allFiles(public_path().'/assets/img/') as $file)
{
$filename = $file->getRelativePathName();
echo HTML::image('public/assets/img/'.$filename, $filename);
}
This is a router you can use to test it:
Route::any('images', function() {
$images = '';
foreach (File::allFiles(public_path() . '/assets/img/') as $file)
{
$filename = $file->getRelativePathName();
$images .= HTML::image('public/assets/img/'.$filename, $filename);
}
return "<htm><body>$images</body></htm>";
});
Edit the /assets/img/ to your own and just hit http://yourserver.dev/images.

Related

How to read all folder name from a folder in laravel?

I am developig a website using laravel. here i need to upload photos in different folder chossed by user. Here i think i need to know all the folder names inside image folder. Is there any way in laravel to read all folder names inside image folder.
I have done that.
$foler_names = [];
$i = 0;
$dir = public_path().'/img/gallery_img/';
$dirList = scandir($dir);
foreach ($dirList as $key => $value) {
if (strpos($value, '.') !== false) {
}else {
$foler_names[$i] = $value;
echo $value;
echo '</br>';
$i++;
}
}
You can use Storage facade for it.
$directories = Storage::directories($directory);
// Recursive...
$directories = Storage::allDirectories($directory);
Check the details here https://laravel.com/docs/5.8/filesystem#directories

Laravel deploy: storage image doesn't work correctly

After deploying my laravel project from local to Apache web server, all works correctly except images link. Here the code:
Images are stored in:
storage/app/public/photos
after i've run command:
php artisan storage:link
Images are linked at:
public/storage/photos
Controller:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photo = $file->storeAs('public/photos', 'foto-' . time() . '.' . $extension);
$user->photo = $photo;
$user->save();
}
Images are uploaded correctly on storage/app/public/photos and correctly linked in public/storage/photos but it doesn't display on frontend.
in blade, i've tried to use Storage::url to retrieve the path
{{Storage::url($user->photo)}}
and asset()
{{asset($user->photo)}}
in both cases, image doesn't exist
The public path of image is:
http://mywebsite.com/storage/photos/foto-1522914164.png
You should Use url function to show your image like below way.
url($user->photo);
I'd suggest to change the controller code as follows:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photoFileName = 'foto-' . time() . '.' . $extension;
$photo = $file->storeAs('public/photos', $photoFileName);
$user->photo = 'photos/' . $photoFileName;
$user->save();
}
Then you can use {{asset($user->photo)}} in your blade.
on my webspace, it seems that the only way to display image correctly is to create a custom route that read and serve the image.
i'm solved like this:
i'm storing only image name in db:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photoFileName = 'photo-' . $model->id . '.-' . time() . '.' . $extension;
$photo = $file->storeAs('public/photos', $photoFileName);
$store = $photoFileName;
}
then, i've create custom route that read images and display them:
Route::get('storage/{filename}.{ext}', function ($filename, $ext) {
$folders = glob(storage_path('app/public/*'), GLOB_ONLYDIR);
$path = '';
foreach ($folders as $folder) {
$path = $folder . '/' . $filename . '.' . $ext;
if (File::exists($path)) {
break;
}
}
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header('Content-Type', $type);
return $response;
});
in blade, i'm using Storage to display image:
{{ Storage::url($photo->photo) }}}

I want to delete the stored image while update new image

I want to delete the stored image while update new image
public function update($id)
{
$users = AdminLogin::find($id);
if(Input::hasFile('image_file'))
{
$file = Input::file('image_file');
$name = time() . '-' . $file->getClientOriginalName();
$file = $file->move(('uploads/images'), $name);
$users->image_file= $name;
}
$users->save();
return response()->json($users);
}
You can write this. This will solve your problem
public function update($id)
{
$users = AdminLogin::find($id);
if(Input::hasFile('image_file'))
{
$usersImage = public_path("uploads/images/{$users->image_file}"); // get previous image from folder
if (File::exists($usersImage)) { // unlink or remove previous image from folder
unlink($usersImage);
}
$file = Input::file('image_file');
$name = time() . '-' . $file->getClientOriginalName();
$file = $file->move(('uploads/images'), $name);
$users->image_file= $name;
}
$users->save();
return response()->json($users);
}
This will delete the previous image and update the new image
Well, the answer is technically incorrect. What if the save operation fails, since you have deleted that image the current record will not have an image anymore.
So to overcome this problem you can adjust your code like:
if(Input::hasFile('image_file'))
{
$file = Input::file('image_file');
$name = time() . '-' . $file->getClientOriginalName();
$file = $file->move(('uploads/images'), $name);
$users->image_file= $name;
}
$users->save();
if(Input::hasFile('image_file'))
{
$usersImage = public_path("uploads/images/{$users->image_file}"); // get previous image from folder
if (File::exists($usersImage)) { // unlink or remove previous image from folder
unlink($usersImage);
}
}

Change download filename codeigniter

Convert file name before force download.
I am trying to be able to convert the file name of the stored file name to the orignal file name before download
So when user clicks on a file to download instead of showing
post_1486965530_jeJNHKWXPMrwRpGBYxczIfTbaqhLnDVO.php
Like in image below
It will just rename the downloaded file something like
config.php
Question how to only change the downloaded filename when click on image.
public function downloads($id) {
$this->db->where('attachment_id', $id);
$query = $this->db->get('attachments');
if ($query->num_rows() == 0) {
return false;
}
$path = '';
$file = '';
foreach ($query->result_array() as $result) {
$path .= FCPATH . 'uploads/';
// This gives the stored file name
// This is folder 201702
// Looks like 201702/post_1486965530_jeJNHKWXPMrwRpGBYxczIfTbaqhLnDVO.php
$stored_file_name .= $result['attachment_name'];
// Out puts just example "config.php"
$original .= $result['file_name'];
}
force_download($path . $stored_file_name, NULL);
}
How about that
(imho your foreach loop doesn't make any sense)
public function downloads($id) {
$this->db->where('attachment_id', $id);
$query = $this->db->get('attachments');
if ($query->num_rows() == 0) {
return false;
}
$path = '';
$file = '';
foreach ($query->result_array() as $result) {
$path .= FCPATH . 'uploads/';
// This gives the stored file name
// This is folder 201702
// Looks like 201702/post_1486965530_jeJNHKWXPMrwRpGBYxczIfTbaqhLnDVO.php
$stored_file_name .= $result['attachment_name'];
// Out puts just example "config.php"
$original .= $result['file_name'];
}
force_download("your_filename.txt",file_get_contents($path . $stored_file_name));
}
Simple like that!
force_download(
$filenameWithFileExtension,
file_get_contents($fileToDownload),
mime_content_type($fileToDownload)
);
For this situation use force_download() something like this -
$file_name = "using md5 to convert it on normal text and stroe variable";
$file_path = "It's your file path, where have file in your drive"
$file_path = 'uploads/'.$path;
force_download($file_name, file_get_contents($file_path));
It's simple and working fine. Just need to store your file path and file name store in 2 different variables and pass them in force_download().
Hope it's work.
Good Luck.

How to upload multiple images and store their name in database with laravel 5.1?

I have created a form for users can upload multiple images,and move uploaded images to 'Upload' folder and store their names in database. This is my code
public function multiple_upload() {
$multiupload = new Multiupload();
// getting all of the post data
$files = Input::file('images');
// Making counting of uploaded images
$file_count = count($files);
// start count how many uploaded
$uploadcount = 0;
foreach($files as $file) {
$rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
$validator = Validator::make(array('file'=> $file), $rules);
if($validator->passes()){
$destinationPath = 'uploads';
$filename = $file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $filename);
$uploadcount ++;
$multiupload->fileimage = $filename;
$multiupload->save();
}
}
if($uploadcount == $file_count){
Session::flash('success', 'Upload successfully');
return Redirect::to('/');
}
else {
return Redirect::to('/')->withInput()->withErrors($validator);
}
}
After upload all images successfully move to 'Uploads' folder but, in database it store only one image name. So how to store all images name in database?
Please help me and thanks you for help.
The reason is that you are reusing the same Multiupload instance in your loop and just overwriting the saved name with the name of next file. You should be creating a new Multiupload instance for every file that gets uploaded.
As #edrzej.kurylo said
You have to add the below line to inside of foreach($files as $file) {
$multiupload = new Multiupload();
Because you are reusing the same Multiupload function again and again. You have to re initialize the Model for every time the loop runs.
You should move your $multiupload = new Multiupload(); into the foreach loop.
foreach($files as $file) {
$multiupload = new Multiupload();
}
I would use for loop in this manner:
if($request->hasFile('images'){
$files = $request->file('images');
for($i=0; $i<count($files); $i++){
$img = new SampleImage();
$name = rand().'.'.$files[$i]->getClientOriginalExtension();
$files[$i]->move('uploads/samples/',$name);
$img->image_name = $name;
$img->save();
}
}

Resources