John Davidson

How to hide the path of image in php/laravel

0 comments
Message:


I am trying to hide the real path of the image, but not getting any proper solution.
I have added some lines in .htaccess but its not working,
I have added :


RewriteEngine On
RewriteRule ^ewallet\/public\/frontend\/img\/(.*)$ /ewallet/public/frontend/images/$1 [L]

Please help me out,
Thanks in advance.



My Answer:
One way to hide the path of an image in PHP/Laravel is to store the images outside of the public directory and serve them through a controller. Here's how you can do it:

1. Move your images to a directory outside of the public directory, for example, in the storage directory.

2. Create a route in your routes/web.php file that will serve the images through a controller. For example:

php
Route::get('images/{filename}', 'ImageController@show');


3. Create a controller called ImageController with a method called show that will return the image file. For example:

php
namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;

class ImageController extends Controller
{
public function show($filename)
{
$path = storage_path('app/images/' . $filename);

if (!Storage::exists('images/' . $filename)) {
abort(404);
}

return response()->file($path);
}
}


4. Update your image paths in your views to point to the new route. For example:

html
Example Image


By serving the images through a controller, you can hide the real path of the images and control access to them. Make sure to handle permissions and security considerations when implementing this solution.

Rate this post

5 of 5 based on 9252 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us