I store files in storage.
No I tried to generate link that open file like:
http://[temp.com]/storage/tests/de139607636857fade861a3c2c472643.txt
But it does not work.
How to open file from storage by URL address?
Files in the storage are not accessible by url by default
You could use Public Disk. For that you need to create symbolic link from public/storage to storage/app/public
From Larvel 5.3 and up, you have artisan command that will help you to create symlink
php artisan storage:link
If you are using older version of Laravel, you can find an answer how to create symbolic link here
Another approach would be to create new disk "uploads", by editing the file config/filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path(),
],
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],
]
To store files in this location
Storage::disk('uploads')->put($file_name, $file_content);
And to get the file
asset('uploads/'. $file_name)
The storage directory exists outside the web root, as some of the stuff in there shouldn't necessarily be publicly accessible.
https://laravel.com/docs/5.5/filesystem#the-public-disk
The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.
I strongly recommend not to modify the Laravel's directory structure.
What I would do is to create a URL to download the file, for example:
Route::get('download/{file}', 'DownloadsController#index');
And in that method's controller, put some logic to assert that the file exists and serve it with a response download, for example
public function index($file)
{
$filePath = storage_path('tests/'.$file);
if (! file_exists($filePath)) {
// Some response with error message...
}
return response()->download($filePath);
}
Then you can download your file with a link like this
http://[temp.com]/download/de139607636857fade861a3c2c472643.txt
The controller's way allows you to make some authentication checks before serving the file for example, or to count how many times the file was downloaded.
Related
I am working on a laravel project which i want to move to production.
Now, created a new folder in the root directory called core and moved all files from the root directory into the 'core' folder.
Now here is my new folder structure
project
|-core
|-every other folders and files
|-public
|-every other asset files
After doing this an updated the path in index.php , the website works well, but the storage images are not loading, it an error that the path was found.
Now, in the public folder, I deleted the storage link and and run php artisan storage:link , I get this return
php artisan storage:link
The system cannot find the path specified.
INFO The [C:\Users\user2\Desktop\project\core\public\storage] link has been connected to [C:\Users\user2\Desktop\project\core\storage\app/public].
But when i go to public, no storage folder link is created.
How can i resolve this?
If you come across this problem, this is how I eventually solved it,
go to config/filesystems.php and replace this
'links' => [
public_path('storage') => storage_path('app/public'),
],
with this;
'links' => [
'../public/storage' => storage_path('app/public'),
],
I am still not very clear in which folder to save the files uploaded by the user such as avatars, or the images of a post etc.
I've already read the guide https://laravel.com/docs/8.x/filesystem but couldn't figure out the best solution.
I can safely upload files to:
storage / app / folder
Or in
storage / app / public / folder
For example, I use the following code to load avatars and it is saved in storage/app/avatars:
$path = $request->file('avatar')->store('avatars');
If they need to be publicly accessible they need to end up in public some where. If you have setup the storage link then the public disk which is set as its root to storage/app/public will be accessible via yoursite.blah/storage as it is symlinked to the public/storage folder
normally in this route: storage/app/public/folder
Just remind you that if you create a folder, example: avatar and you want to save there all the avatars
you have to register that folder in the filesystems configuration file: config/filesystems.php
you have also more examples, in the filesystem documentation
I entered the second line for the avatars, in my controller I save the file like this:
$path = $request->file('avatar')->store('avatars');
But keep uploading it to storage/app/avatars
'links' => [
public_path('storage') => storage_path('app/public'),
public_path('avatars') => storage_path('app/public/avatars'),
],
i have two project in laravel 5.8.
First in sub1.domain.com
Second in sub2.domain.com
With sub2 i save images and files.
I need to view the image and files from sub2 and sub1.
Where is the best directory store for sharing the image and files ?
I have a shared hosting.
Thanks.
UPDATE - SOLVED
I have changed my filestystem config in this mode:
'public' => [
'driver' => 'local',
// 'root' => public_path() . '/uploads',
'root' => '/home/xxx/www',
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
Now the file is stored in "public_html" of my "website.com" on my hosting for every subdomain.
Thanks everybody.
The docs state
The public disk is intended for files that are going to be publicly
accessible. By default, the public disk uses the local driver and
stores these files in storage/app/public. To make them accessible from
the web, you should create a symbolic link from public/storage to
storage/app/public
Assuming all your images & files will be publicly accessible, follow those instructions.
Then, instead of using asset('storage/file.txt') to print out the publicly accessible url, you could use a value from your .env file to get the path, which would be something like "https://sub2.domain.com/images/" and then just append your file name.
Or write a custom helper method that allows you to specify the subdomain, something like custom_asset('storage/file.txt', 'sub2')
you can upload in public folder as symbolic link is hard hard to make cpanel
modify ur
filesystems.php
in
'public' => [
'driver' => 'local',
'root' => public_path('app/public/uploads'),
'url' => env('APP_URL').'/uploads',
'visibility' => 'public',
],
in above linke
ur upload path will be
public/uploads
and
\Storage::url('example.png') to get full image url
in my shared hosting (without SSH) my laravel installation save file from form only in storage/app/public/namefolder but not in storage/namefolder and this causes the files not to be displayed.
How can I solve it? Is there anything I need to change in the filesystem of the online site on shared hosting? or do I have to do anything else?
thanks a lot
this is where i store my file
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
i think your problem that you can not make the symlink command for storage as you do not has a SSH
php artisan storage:link
this command will create a symlink - a shortcut- to storage directory inside public directory,
there is several ways to apply this command with out accessing server via SSH
Route::get('artisan-command', function(){
Artisan::call('storage:link');
});
or you can call it with in a class
after you run the command every thing will work just fine , and no need to change config more than usual stuff.
I have my public folder files in /public_html/ and other files and folders in /public_html/files. Everything works fine except I can't link my storage directory via php artisan storage:link command because of my DirectAdmin host with limited functionalities. So I decided to create a new disk in public folder which is /public_html/. I added the following code to config/filesystems.php:
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],
Also used this code to upload files in controller but it upload file somewhere except the disk I defined.
$request->file('avatar')->store('avatars', 'uploads');
In your new disk, you defined root folder as public_path which is public folder, not storage/app/public folder, your image saves in public/uploads/avatars directory if you want to move them to storage folder, Try:
'uploads' => [
'driver' => 'local',
'root' => storage_path('app/public') . '/uploads',
],