Issue with laravel file uploads on local storage - laravel

I am using
$request->file('photo')->store('public/photos');
to store files in public/photos directory under storage.
Created a link to storage from public directory using artisan storage:link. The output of
$request->file('photo')->store('public/photos');
is stored in db against the photo field. While retrieving the photo in view, I use asset() helper which output the link as
domain.com/storage/public/photos/photo.jpg
which returns 404 error. It should actually return
domain.com/storage/photos/photo.jpg
as public/storage is linked to app/storage/public. Please help.

You save it like this... $request->file('photo')->store('public/photos');
Store is the location to save the photo.
Remove public from here ->store('public/photos');

Related

How to store images with heroku laravel app

With store route in laravel i pass data (text) to MySQL and image to storage. However i cant store images on local storage (after new deploy) changes in file system resets so i need to do it with external storage. Whats the easiest way to store images (low size) and to have permission to fetch them (with js)?
You need to store them to something persistent, like Amazon S3.
You can use Laravel's Storage system for this. Once you've set up an S3 disk with the right AWS credentials, it's as simple as:
$path = $request->file('your-field')->store('your-folder', 's3')
or:
$path = Storage::disk('s3')->put('path/to/folder', new File('/path/to/temporary/file'));
I encountered the same problem recently and here is what I figured out for a solution:
Heroku does not have storage for images, which calls for using a service that stores images for free and use it.
OR
You can just move them to the public folder instead of the storage folder, and use this in your view code:
<img src="{{ secure_asset($whereEverYouStoredYourImage->theImageAttribute) }}" alt="example">

Laravel 5.6 custom storage link/mount to other location on Windows OS

Laravel 5.6 custom storage link/mount to other location on Windows OS
https://laravel.com/docs/5.6/filesystem#configuration
php artisan storage:link will point the
http://localhost/storage to c:/project/storage/app/public
means visit http://localhost/storage/image.jpg = c:/project/storage/app/public/image.jpg
but how to custom the uri link to other location ? eg..
http://localhost/ramdisk/image.jpg link to z:/ramdisk/image.jpg
or
http://localhost/avatar/image.jpg link to z:/avatar/image.jpg
First of all: why would you like to do that? The images you upload should be in /storage/app/... or in /public/images/... Other disks may be unavailable if putting the app on a server.
To clarify, the php artisan storage:link only creates a symlink in /public/ folder pointing to the app/public. (I think in 5.6 it has absolute path so on server or after moving the application you have to delete and generate it again.)
So if you really want to access disk z on your PC from the app you should create a symlink in /public/ folder with the desired name. For example:
http://localhost/ramdisk/... would be a symlink named ramdisk in the public folder and would point to z:/ramdisk/. Then you would be able to access the folder contents as you wish.
Of course you can have multiple symlinks so you would create another one for the avatar folder too. Also make sure that disk z has a public access.
I was not able to test it, but I am sure that it would work, however I would not recommend it. If you want to use something in your app it should be within the app. (Except S3 drivers like amazon cloud storage.) I hope it helped.

laravel storage method not displaying images on another OS or machine

I store images using this method:
'avatar' => $r->avatar->store('public/avatars')
and get them like
<img src="{{Storage::url($user->avatar)}}" style="border-radius:50%;height:30px;width:30px;" style alt="">
They get displayed on the local machine, but not in others. What could be the issue?
Check the value of user's avatar column, you might have some sort symlinks enabled on local machine, Instead you can also use public_path, as storage should not be exposed outside.
https://laravel.com/docs/5.6/helpers#method-public-path
Also check file visibility
https://laravel.com/docs/5.6/filesystem#storing-files
you have stored file inside public/avatars
be sure $user->avatar has public/avatars concatenated if not you might have to
{{Storage::url("public/avatars/".$user->avatar)}}
use this as source of your image
had to delete the public/storage folder on the other machine then run
php artisan storage:link
images started showing as normal

prevent the access of folders that are not under public in laravel 4.2

hi guys im pretty new in laravel 4.2 i have a project that stores file in the server and what i did based from the opinion of other people, when the user saves a file it goes to app/storage/uploads my problem is when i know the url to the file, it still have access to the file. for example it is a pdf file, it opens in the browser or if it is doc, xls, xlsx etc it triggers a download what i'm tying to do is when the user tries to access that url, it would go to a certain view informing them that the folder is restricted so far i have this on my routes
Route::get(storage_path().'uploads/{all?}' , 'sample#restrict');
then in my controller
public function restrict()
{
dd("WHOOOPS!"); //for trial purposes
}
any ideas what im doing wrong? thanks in advance
With normal configuration web server has access only to a public directory.
If you set public directory as root for a virtual host, noone will be able to access app or storage directories which are outside public.

restrict access on public folder in laravel 4.2

im fairly new to laravel and im currently working on a project that needs to store files in the server. What im doing is store it under public/docs the problem is the files under that directory is accessible all the time given that they know the url to the certain file. for example even if they are logged out if they know this url http://localhost/dummy/public/uploads/docs/2016-06-02-10-52-43-oth-content-inventory-and-planning-spreadsheet.pdf they would be able to access it what im thinking is to do something like this
in my routes i would do something like
Route::any('uploads/docs/' , 'go to some controller to check if user can acces');
is this possible? thanks in advance!
it's called public for a reason...
you should use storage instead (laravel 5) in storage you dont restrict access, actualy the opposite, you grant access to the file .
try this for laravel 4.2 (move the file after upload like this to a storage folder)
Input::file('file')->move(__DIR__.'/storage/',Input::file('file')->getClientOriginalName());
});
Anyway ,the conclusion is that you can't restrict access (you shouldn't even if there is a way) because it's meant to be public ! so you should use storage folder(or whatever its called in laravel 4)

Resources