Restrict direct access to image url in laravel - laravel

If i give image path directly it accessible now. I want image accessible only for if user loged in.
Ex:
This is image path http://localhost:8000/images/test.jpg
If user give this path without loged in through error message as access dined
Can you help me..

There are several ways to fix this problem, the way I would use is to save all images in your resources/assets/ folder outside of the public folder.
Now you could create a route like this:
Route::get('images/{file}', 'ImageController#getImage')->where('file', '.*');
And now in your ImageController, you can check if the user is logged in. If so, you can load the image from the resources folder and send it in a file response. If they are not logged in, there is no way they will be able to access the file.

Tested with Laravel 8
Define a new disk in file: config/filesystem.php
'disks' => [
// ...
'private' => [
'driver' => 'local',
'root' => storage_path('app/private'),
'url' => env('APP_URL').'/storage',
'visibility' => 'private',
],
],
Define web route in file: routes/web.php
Route::any('/private{any}', function (Request $request, $file) {
abort_if(!Storage::disk('private')->exists($file), 404, "File doesn't exist.");
return Storage::disk('private')->response($file);
})->middleware('auth')->where('any', '.*');
Upload image file to folder: storage/private
Test on your localhost at: http://localhost:8000/private/image_name.jpg

Related

laravel storage embed pdf not found

I want to embed a PDF file into a modal blade view. The disk is not public (because it shouldn't be) and with a logged in user, I get the 404 not found message.
Inside config/filesystems.php I got:
<?php
'disks' => [
'custom' => [
'driver' => 'local',
'root' => storage_path('app/custom'),
],
Inside my blade view I already tested successfully
<iframe src="https://www.inkwelleditorial.com/pdfSample.pdf" width="100%" height="300"></iframe>
However, when I try:
<iframe src="{{ storage_path('app/custom/myFile.pdf')}}" width="100%" height="300"></iframe>
I get
404 NOT FOUND
I have made sure the file does exist:
dd(Storage::disk('custom')->path('myFile.pdf')) => shows absolute path correctly
dd(Storage::disk('custom')->exists('myFile.pdf')) => returns true.
How do I fix this?
First,you must know that storage_path() is a private path.
if you want show storage file on web, you can see the config file config/filesystems.php
'links' => [
public_path('storage') => storage_path('app/public'),
],
run this command: php artisan storage:link , then go to public directory,you can see the soft link directory on the public directory.
Then you can use the url on webpage like this <ifram src="/storage/app/custom/xxx.pdf" ></iframe>

Shared Hosting Laravel 8 Hosting problem: Symlink is not working

I need the deploy my laravel project to the web. With this introduction But the hosting provider is Doesn't allow me to use a symbolic link(symlink), I called customer service, and told me blocked for security reasons.
So, since I do not use symbolic links, there are big problems in uploading images and deploying them.
Is there a way to do deployment my app?
Change your disk configurations in the config/filesystems.php to read and write to public folder via public_path() function.
If you're deploying over a shared host, you probably have another folder like public_html as your public folder which is not included in your source. Try to link to the public_html using dots ../../public_html/
'public' => [
'driver' => 'local',
// 'root' => storage_path('app/public'),
'root' => public_path(),
'url' => env('APP_URL'),
'visibility' => 'public',
],
'photos' => [
'driver' => 'local',
'root' => public_path('photos'),
'url' => env('APP_URL') . '/photos',
'visibility' => 'public',
],
Another way is just create something like symlink using php, example you can create a route like below:
Route::get(
'/storage/{file}',
function ($file) {
$filepath = "../storage/app/public/".$file;
header("Cache-Control: public");
header("Content-Type: ".mime_content_type($filepath));
header('Content-Length: '.filesize($filepath));
readfile($filepath);
die();
}
);
Maybe not best practice, but it will solve your problem. It's will do same as symlink.

Laravel upload without form and request functionality

I want to run a scheduler (CRON) that reads a csv file every minute and imports it into a database.
I want to grab the file from a predefined directory on my windows directory system and upload it into the storage area in Laravel on my host server.
I created a test function to read the contents of a directory. I get an error
'The "C:/Users/alfre/code" directory does not exist.'.
What is the correct way to upload files with a scheduler?
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'odoofiles' => [
'driver' => 'local',
'root' => '\Users\alfre\code\storage\csv',
],
I could not find an answer yet on the web.
you should not use absolute paths from windows.
try placing the cvs folds in in the storage folder and try
on your computer it will be something like C:/Users/alfre/code/storage/csv
public function test()
{
$files = File::allFiles(storage_path('csv'));
foreach ($files as $file)
{
echo (string)$file, "\n";
}
}

404 Page Not Found When Viewing Images Laravel

I have an image stored in the database as conventions/convention_title.jpg
The file is located in "storage/app/conventions/convention_title.jpg"
When i do something like this flyer }}">, it doesn't display...
OR
flyer }}"> the image still wont display...
And even worse still, if i try to access the image directly via link like this...
http://localhost:8000/storage/app/convention/convention_title.jpg i get a view not found error -> 404 page not found..
What could be the cause of this?
php artisan storage:link
problem solved while writing
src=" {{ asset('/storage/'.$post->image) }}"
Web content must be stored on the laravel root /public folder and you can easily access to it via asset helper.
For example:
/public/img/conventions/convention_title.jpg
Then you can access on the views to it like:
{{ asset("/img/conventions/convention_title.jpg")}}
In case you want to test if you can access to the image you can try:
http://localhost:8000/convention/convention_title.jpg
You can create a new disk in the config/filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path(),
],
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],
]
Then use it :
Storage::disk('uploads')->put('filename', $file_content);
Or you can do this simple method
$request->file('photo')->move(public_path("/uploads"), $newfilename);
Then, using any of the above methods, you can still access your images using the asset() helper function...

How to change storage path in laravel 5.3 to public?

In laravel 5.3, if I upload a file from a form, it will be stored in own storage directory. Then I should create a symlink to that storage path in public path.
I can't use creating symlinks on my system because of the limitation. How can I upload files directly to public/uploads instead of storage/app/uploads?
I tried to set the path in AppServiceProvider.php but it doesn't work.
public function register()
{
//not working:
$this->app->useStoragePath( $this->app->basePath() . "/upload");
//also not working:
// $this->app->bind('path.storage', function () {
// return $this->app->basePath() . '/upload';
// });
}
by "not working" I mean that it is still uploading into the default storage directory. I cleared also the cache.
Here is the upload part (used in a controller):
$request->image->storeAs("uploads", "uploaded_image.jpg");
Thanks for any hints. :)
In Laravel 5 you now have to do this in config/filesystems.php. You can add a new disk like so:
'disks' => [
'uploads' => [
'driver' => 'local',
'root' => public_path(), // previously storage_path();
],
]
and then do the following to use it:
Storage::disk('uploads')->put('filename', $file_content);

Resources