ltrtuan's avatar

Access file in symbolic link folder public/storage

Hi everyone. I have problem with "php artisan storage:link". I use homestead and vagrant on window 7 Ultimate. I have 2 cases:

  • 1/ I used command "php artisan storage:link"
  • RESULT:
  • command is successful
  • a new folder called storage is created in public WITH icon shortcut. See screenshot http://prntscr.com/ddqhpb
  • when upload 1 image with my own controller, the image will store in storage/app/public/avatars/file1.jpg, at the same time the image will be stored in root/public/storage/avatars/file1.jpg => that works great.
  • PROBLEM:
  • I can not access to file1.jpg in root/public/avatars/file1.jpg. I try by access on browser domain/storage/avatars/file1.jpg or call in code asset('storage/file1.jpg'). But them return 404 page.
  • RESULT:
  • a new folder called storage is created in public WITHOUT icon shortcut.
  • I temporary put 1 image with name file2.jpg into root/public/storage. Then access on browser domain/storage/avatars/file2.jpg => It woks normally.
  • PROBLEM:
  • When upload a image with my own controller (same case 1), there is just 1 image show in root/storage/app/public/avatars/file3.jpg , in symbolic link public/storage/avatars, the image file3.jpg do not exist Do Anyone else have same problem with me ?
0 likes
13 replies
ejdelmonico's avatar

You probably should not have linked another folder. Did you check if visibility is public for the file? Did you check the url? There are helper commands for all of that.Also, remember to try a slash in front of paths. Depending on config, you may need it. Storage::url('file1.jpg'); Storage::getVisibility('file.jpg');

ltrtuan's avatar

@ejdelmonico do you mean should upload file directly into public folder ??? Do not use symbolic link ? I want to follow CORE of laravel, so i decide coding follow instruction of laravel https://laravel.com/docs/5.3/filesystem. My code for uploading

$fileUploadUrl = Storage::putFileAs('avatars', new File($file), 'filename.jpg','public');

The uploaded file is already visible.

ejdelmonico's avatar

No, you should store things in local storage or S3, etc. I use local storage by just doing Storage::disk('local')->put('some.jpg'); That will store the file in /storage/public/app/some.file. Now, if you linked public through artisan command, you can access file at /storage/some.file

ltrtuan's avatar

@ejdelmonico .Thanks for supporting. I have been confused with Storage. I will tell you step by step of processing upload file in my project. I hope you can help me.

  1. Config in config/filesystems.php
return [   

 'default' => 'public',// This option is changed by me
'cloud' => 's3',
'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => 'your-key',
            'secret' => 'your-secret',
            'region' => 'your-region',
            'bucket' => 'your-bucket',
        ],

    ]
]

Is it right ? So i do not need use your code : Storage::disk('local') ? right ? I need uploaded files will be stored in root/storage/app/public. If i use 'default' => 'local', uploaded files will be stored in root/storage/app and they are not linked to public folder

  1. I follow document of Storage . And i use it for my purpose
$fileUploadUrl = Storage::putFileAs('avatars', new File($file), 'filename.jpg','public');

What i know about above code is upload $file (value from input form) with new name 'filename.jpg' in root/storage/app/public folder and set them visibility (with public parameter). With 'public' parameter, the uploaded file will link automatically with root/public/storage. And when i run above code. My thinking is right. But i can not access file at root/public/storage although it is exist. I type domain.local/storage/filename.jpg => 404 page. What's problem ?

ejdelmonico's avatar

You are correct. You can leave out stating the local disk and the rest of the code seems correct. If you want public access to the file...you must link it and make sure file has public visibility.

ejdelmonico's avatar

To view contents, you can do a test like this

Route::get('/file', function () {
    $data = Storage::get('sample.txt');
    return view('file', compact('data'));
});
ltrtuan's avatar

@ejdelmonico : i appreciate your supporting. I know with route, image maybe show (i do not try). But obviously I have uploaded files in pubic folder, but i can not access them directly from browser. domain/storage/filename.jpg (note: storage is symbolic link and exist in public folder). I already changed chmod every folder is 777

ltrtuan's avatar

@ejdelmonico I decided not use symbolic link anymore. Symbolic link is big problem for newbie as me. Thank for your patient and sorry my bad english. I changed config in filesystem.php

'public' => [
            'driver' => 'local',
            'root' => public_path('storage'),
            'visibility' => 'public',
        ],

It works now.

WebKenth's avatar

Just gonna throw this in, with Laravel 5.3 i can access any files placed in storage/public folder

so if i had test.png in storage/public/images/ i would access it via devsite.dev/images/test.png

This works both on my Forge server and via Valet on my mac

Did no setup to make this work it came out of the box

ltrtuan's avatar

@WebKenth yes i understand document, i followed exactly the same and i really want to code in the box. Run "php artisan storage:link" successful -> A new folder storage (symbolic folder) was occur in root/public folder -> Upload image -> Image is uploaded successful -> It was stored in root/storage/app/public -> Check in root/public/storage, it existed -> i access via domain/storage/image.jpg or domain/image.jpg , return 404 page -> i try to access again via domain/storage/app/public/image.jpg -> return 404 page again.

  • WebKenth, correct path of storage when use Storage facade is root/storage/app/public , you are missing app folder
ltrtuan's avatar
ltrtuan
OP
Best Answer
Level 1

Hi @WebKenth and @ejdelmonico my endless efforts finally have perfect result. My problem is i use window 7 and i use vagrant and virtualbox on it. Since Windows doesn't have native support for Linux-style symlinks, this would fail and break provisioning.. My steps for fixing it.

  1. Add permission to create symbolic link http://superuser.com/questions/104845/permission-to-make-symbolic-links-in-windows-7
  2. I user Git bash for my command line. And i need Run As Administrator. I access
  • vagrant ssh
  • cd "path_root_my_project"
  • php artisan storage:link
  • Check folder public, i saw a unknown file call storage, not directory http://prntscr.com/dexd43 . But it works now.

Please or to participate in this conversation.