Laravel 5.4 show uploaded image issue - php

I'm learning Laravel 5.4 and for now I'm trying to upload image and show uploaded image in html.
It seems files are uploaded successfully but when I want to show the image in Html, there's 404 error for images and nothing will display.
This is my settings and code:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|string|max:255',
'desc' => 'required|string',
'width' => 'required|numeric',
'height'=> 'required|numeric',
'artist'=> 'required|exists:artists,id',
'photo' => 'required|file'
]);
$artistInfo = Artist::find($request->artist);
$fileExtension = $request->file('photo')->extension();
$originalFileName = $request->file('photo')->getClientOriginalName();
$filePath = Storage::putFile('public', $request->file('photo'), 'public');
Photo::create([
'name' => $request->name,
'origName' => $originalFileName,
'url' => $filePath,
'artist_id' => $request->artist,
'desc' => $request->desc,
'width' => $request->width,
'height' => $request->height
]);
return $filePath;//this for test
}
And my Blade:
#forelse($photos as $photo)
<tr>
<td>{{ $photo->name }}</td>
<td>
<img class="img" src="{{ asset( 'public/storage/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>
<td>{{ $photo->artist->name . ' ' . $photo->artist->family }}</td>
<td>{{ $photo->desc }}</td>
<td>{{ $photo->width }}</td>
<td>{{ $photo->height }}</td>
<td class="text-center">
<a href="{{ route('artists.edit', ['id' => $photo->id]) }}">
<i class="material-icons">edit</i>
</a>
</td>
<td class="text-center">
<a href="{{ route('artists.edit', ['id' => $photo->id]) }}">
<i class="material-icons">assignment_turned_in</i>
</a>
</td>
</tr>
#empty
<tr>
<td>There's no photo Yet</td>
<td>—</td>
<td>—</td>
<td>—</td>
<td>—</td>
</tr>
#endforelse
NOTE :
I made a symbol link to my public folder using php artisan storage:link and symlink works fine...
I changed this line in blade:
<img class="img" src="{{ asset( 'public/storage/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>
and tried these manners:
<img class="img" src="{{ asset(storage_path() . ($photo->url)) }}" alt="{{ $photo->name }}" /></td>
<img class="img" src="{{ storage_path() . ($photo->url) }}" alt="{{ $photo->name }}" /></td>
$photo->url has image filename for example jDHPOTh7iejFABANSzasNbBWJ09DqLbqJb8ymJhv.png
Would you please let me know which part is my problem?
Thanks in Advance
UPDATE :
I'm using PhpStorm Ide and when I download my public folder to my project -or synchronize this- my symbol link does not appear in public folder list...
This is ls from my public folder :
And this is my phpStorm project view AFTER download a fresh copy of public folders in my host:
And as a note :
I started to develop this website in windows at first and then I did move entire project to my linux remote server...

For anyone looking for answer to this question, please follow this link contains great answer
https://stackoverflow.com/a/30191854/417899

Thanks to all for participating, but in my case there was a tricky issue...
I was logged in to my server using root and when I did type php artisan storage:link, the symbol link was made by root as owner and it seems Apache didn't have permission to access this symbol link and because of that, the files was not accessible in http.
SOLUTION
I have a vds that serve 5 different websites, so
I did simply change the owner of symbol link's owner to my relative username and the problem solved...
chown -R usergroup:username /path/to/link
That's it...

I think the issue is here:
$filePath = Storage::putFile('public', $request->file('photo'), 'public');
here you are putting your file on public folder and here:
<img class="img" src="{{ asset( 'public/storage/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>
you are trying to get it from public/storage/. So change the path like:
<img class="img" src="{{ asset( 'public/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>
and try again.
Note: Here uploaded images path is stored in public/image.extension, so fetching it from the same public directory

Related

Laravel 9 - Upload Image - Image does not display

I'm creating a shop, with an upload image in Laravel 9.
Everything is on my database, and my image is sent on file storage/app/public/nameofthefile/nameoftheimage.png.
But my image does not appear on my website like this :
As you can see on the console, the image has the correct direction I give you the code of my controller, my html page :
My CONTROLLER :
public function CreateArticleAction(Request $request) {
$request->validate([
'nom' => ['required'],
'description' => ['required'],
'prix' => ['required'],
'quantité' => ['required'],
'img' => ["required", "image", "mimes:jpg,jpeg,png,gif,svg,webp", "max:2048"],
]);
$path = $request->file('img')->store('boutique-storage', 'public');
$iduser = Session::get('iduser');
$article = new Boutique();
$article->iduser = $iduser;
$article->nom = request('nom');
$article->description = request('description');
$article->prix = request('prix');
$article->quantité = request('quantité');
$article->img = $path;
$article->save();
return redirect('/boutique');
}
My HTML page :
#foreach ($boutiques as $boutique)
<div class="produit">
<img src="{{ asset('storage/app/public/' . $boutique->img) }}" alt="Produit" class="img-produit" />
<p>{{ $boutique->nom }}</p>
<p>{{ $boutique->description }}</p>
<p>{{ $boutique->prix }}</p>
<p>{{ $boutique->quantité }}</p>
<div>
<a class="text-danger" href="/delete-article/{{ $boutique->idproduit }}">Supprimer</a>
<a class="text-primary" href="/FormUpdate/{{ $boutique->idproduit }}">Modifier</a>
</div>
</div>
#endforeach
And the DIRECTION :
I think you can see everything is ok, each data is sent correctly on the database but why my image does not appear ??
Thanks a lot !
Try run this command
php artisan storage:link
Ah ok so I forgot to add this on .env file : FILESYSTEM_DISK=public
Now with the command php artisan storage:link it works :D
replace
<img src="{{ asset('storage/app/public/' . $boutique->img) }}"
with
<img src="{{ asset($boutique->img) }}"

Laravel: Can't Display Image In Blade File

I tried to display the image in the blade. but it cant load the image, it displays the image of else statement
<?php $user_image_path = 'images/user_images/' . $userDetails['image']; ?>
#if(!empty($userDetails['image']) && file_exists($user_image_path))
<img src=" {{ asset('images/user_images/'.$userDetails['image'])}}" alt="Cinque Terre">
#else
<img src="{{ asset('images/product_images/small/no_image.png') }}" alt="Cinque Terre">
#endif
I also debug the code above the if statement it fetches the image
<?php $product_image_path = 'images/user_images/' . $userDetails['image']; ?>
<?php
echo "<pre>";
print_r($user_image_path);
die;
?>
From above comments on your question, seems like you are loading the image with no extension. add the image extension
<img src="{{ asset('images/user_images/'.$userDetails['image'].".png") }}" alt="">
or for .jpg
<img src="{{ asset('images/user_images/'.$userDetails['image']).".jpg" }}" alt="">
Try:
<img src="{{ !empty($userDetails['image']) ? asset('images/user_images/'.$userDetails['image']) : asset('images/product_images/small/no_image.png') }}" alt="Cinque Terre">
It is the second condition of your if condition that doesn't match. The function of file_exists can't locate the the image file. You either have to provide the full path for the file_exists , or remove it:
#if(!empty($userDetails['image']))
<img src="{{ asset('images/user_images/'.$userDetails['image'])}}" alt="Cinque Terre">
#else
<img src="{{ asset('images/product_images/small/no_image.png') }}" alt="Cinque Terre">
#endif
Use this code:
<img src=" {{ !empty(asset('images/user_images/'.$userDetails['image'])) ? <img src="{{ asset('images/user_images/'.$userDetails['image'])}}" alt="Cinque Terre"> : <img src="{{ asset('images/product_images/small/no_image.png') }}" alt="Cinque Terre"> }}" alt="Cinque Terre">
#if(!empty($userDetails['image']))
<img src="{{ asset('images/user_images/'.$userDetails['image'])}}" alt="Cinque Terre">
View the src of image inside inspector. Most of the times it gives us the wrong path instead of public directory.
Secondly, there is space in src. Try to remove and run that.
If asset function doesn't give the public directory path, add ASSETS_URL In .env to /public

No Image showing in a home Laravel

in laravel, i have to link the storage using php artisan storage:link so i can show the image that when user to post a article with a image, the image will be shown. but for now, the image didn't show up. How do i fix it.
here's the code from views/post/index.blade.php
<tbody>
#foreach($posts as $post)
<tr>
<td>
<img src="{{ asset($post->image) }}" alt="">
</td>
<td>
{{ $post->title }}
</td>
<td>
<a href=" {{ route('categories.edit', $post->category->id) }}">
{{ $post->category->name }}
</a>
</td>
when the user post a image, the image will be stored in the linked storage that i've doing before. so here's the code from postscontroller.php if i change the 'posts' to 'post' it says error. because i didn't suitable for the function 'posts' that i write before.
public function store(CreatePostRequest $request)
{
// upload image omegalul
$image = $request->image->store('posts');
// create the post
$post = Post::create([
'title' => $request->title,
'description' => $request->description,
'content' => $request->content,
'image' => $image,
'published_at' => $request->published_at,
'category_id' => $request->category,
'user_id' => auth()->user()->id,
]);
if ($request->tags) {
$post->tags()->attach($request->tags);
}
// flash a message
session()->flash('success', 'Post created successfully');
// redirect the user
return redirect(route('posts.index'));
}
if you guys see any problems that i didn't appeared here, just ask me. Thank you guys so much
If you are working with Laravel Storage it's recommended to use the storage functions to store, get or delete media files.
Use theses commands to do it:
To store:
Storage::disk(config('filesystems.default'))->put($path, $file);
To get:
Storage::disk(config('filesystems.default'))->url($file);
To remove:
Storage::disk('local')->delete($path);
Your code will be like this:
<tbody>
#foreach($posts as $post)
<tr>
<td>
<img src="{{ Storage::disk(config('filesystems.default'))->url($post->image) }}" alt="">
</td>
<td>
{{ $post->title }}
</td>
<td>
<a href=" {{ route('categories.edit', $post->category->id) }}">
{{ $post->category->name }}
</a>
</td>
And:
public function store(CreatePostRequest $request)
{
// upload image omegalul
$image = Storage::disk(config('filesystems.default'))->put('posts', $request->image);
Note: Don't forget to check if your filesystem is setted like 'local' - if you are using local storage.

String replace in Laravel Blade

I have project in Laravel 5.8.
I have this code:
<a href="{{ route('user', ['url_address' => $user->url_address, 'id' => $user->id]) }}"><img
src="{{ $user->images->first()->path ?? $placeholder }}"
class="img-responsive center" alt="{{ $user->name }}"></a>
This return me:
<img src="upload/images/UserImage/c561b1742783ed038521c488d1cd59a1.jpg" class="img-responsive center" alt="angel">
I need replace this:
upload/images/UserImage/c561b1742783ed038521c488d1cd59a1.jpg
to:
upload/images/UserImage/thumbs3/c561b1742783ed038521c488d1cd59a1.jpg
How can I make it?
You have a few options here. You could update your DB data, add a new column to store the thumb path, or modify the existing path in place.
Here's one way to do it using https://laravel.com/docs/5.8/helpers#method-str-replace-first
<?php
use Illuminate\Support\Str;
...
$search = 'upload/images/UserImage/';
$replace = 'upload/images/UserImage/thumbs3/';
$original = $user->images->first()->path;
$replaced = Str::replaceFirst($search, $replace, $original );
// Now, use `$replaced` as your image src.
<a href="{{ route('user', ['url_address' => $user->url_address, 'id' => $user->id]) }}">
<img src="{{ $replaced ?? $placeholder }}"
class="img-responsive center" alt="{{ $user->name }}">
</a>
I should also mention, that in my experience it is easier to maintain a system where all you store in the DB is the file name of the uploaded document. In this case, c561b1742783ed038521c488d1cd59a1.jpg.
Your views then control which directory to look in: ...public_path('upload/images/thumbs/' . $user->profile_image)

PHP - How to make condition if no image show image no image available Laravel

I am newbie in laravel so I really need some help.
I want to make some code if no image available then it will be showing no image available image, this is the example image of no image available http://imgur.com/ppmPcKA
This is my controller to store the image
public function store(CreateBannerRequest $request)
{
$input = $request->all();
//get original file name
$filename = Input::file('photo')->getClientOriginalName();
$input['photo'] = $filename;
$banner = $this->BannerRepository->create($input);
//upload file
Input::file('photo')->move($this->path, $filename);
Flash::success('Banner saved successfully.');
return redirect(route('banner.index'));
}
And this is the table image code
<td>
<img src="{{ asset('img/banner/' . $banner->photo) }} " width="100px" height="100"/>
</td>
And this is the view http://imgur.com/a/X4udv
So if no image it will be show No image available
You can use Blade conditional
Default text if no image available
<td>
#if ($banner->photo)
<img src="{{ asset('img/banner/' . $banner->photo) }} " width="100px" height="100"/>
#else
No image available
#endif
</td>
Default image if no image available
<td>
<img src="{{ asset($banner->photo ? 'img/banner/' . $banner->photo : 'default-image.png') }} " width="100px" height="100"/>
</td>
#if ($banner->photo)
photo) }} " width="100px" height="100"/>
#else
#endif
//from the above you can use if-else statement to fix it up.
Hope it will hepls you :)

Categories