I'm making a new version of my site with laravel 5.4 and want to keep the same links than my old site has.
My old site has links images like this:
domain.com/uploaded/cat.jpg
In laravel, its necesary to create a symbolic link to show the stored images but it saved them with a default folder named storage", like this:
domain.com/storage/cat.jpg
How can i change the default name to "uploaded" or whatever?
I tried to change url inside the file \config\filesystems.php with this:
Before
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
After
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/uploaded',
'visibility' => 'public',
],
and my controller
$request->file('image')->storeAs('public', 'example.jpg');
Make sure you've created a symlink.
According to your configuration, you can run this command :
ln -s storage/app/public public/uploaded
Then, you can access your files :
Storage::disk('public')->url('example.jpg')
Or simply using asset() helper :
asset('uploaded/example.jpg');
Execute this code in your server using any php file to create the symbolic link. Once the link has been created, the files will become publicly accessible.
target = '/home/cfrcoclm/public_html/agencies/storage/app/public'
shortcut = '/home/cfrcoclm/public_html/agencies/uploaded'
symlink(target, shortcut)
ec ho("Link Created!")
`
In case you do not have SSH access, you can as well write this one-line script below and run it on your server - taking into consideration the right directory path
symlink('/home/domainname/laravelprojectname/storage/app/public','/home/domainname/public_html/storage');
Related
PHP version PHP/8.1.10
Laravel version 9.28.0
While working on a Laravel-admin backend, I tried creating a new user (basically inserting data into admin_user table in the database)
working on my windows computer, I encountered the error:
disk [users] not configured, please add a disk config in config/filesystems.php.
I revisited the Laravel docs and these are the things I did to solve the problem:
Opened the config/admin.php and added:
'disk' => 'users',
// Image and file upload path under the disk above.
'directory' => [
'image' => 'images',
'file' => 'files',
],
Then opened the config/filesystems.php and added:
'users' => [
'driver' => 'local',
'root' => storage_path('app'),
]
Closed my git bash and reopened it, ran:
php artisan serve
Accessed the page for creating a user and the error was solved..
Do you know any other way this could be solved?
To use a custom disk you must configure it first. To configure a disk, follow the following steps:
Open filesystems.php file, under config directory, in your preferred text editor or IDE.
Look for the key named disks. The array under disks is where you configure your disks. Laravel already provides some disks out of the box (I recommend not messing with them unless you know what you are doing).
Add your new disk configuration, something like:
'users' => [
'driver' => 'local',
'root' => storage_path('app/public/users'),
'url' => env('APP_URL').'/storage/users',
'visibility' => 'public',
]
Note: The above example assumes you have already executed php artisan storage:link command in order to create a symbolic link that allows the files in storage/app/public to be accessible from outside.
Save the file and now you can use that new disk like below:
use Illuminate\Support\Facades\Storage;
// initialize a new disk using the `users` disk config
$disk = Storage::disk('users');
// perform actions under that disk
$disk->delete('sub/folder/file.ext');
Also, it is important to note that you are not limited to have disks under the storage folder, you can actually have disks under the public folder:
'users' => [
'driver' => 'local',
'root' => public_path('/users'),
'url' => env('APP_URL').'/users',
'visibility' => 'public',
]
Note that the function public_path is used instead of storage_path. Also the command php artisan storage:link is not needed when your disks are under the public folder.
Learn more about File Storage in Laravel.
I have a project located in this path /var/www/html/backend/api_cars
I have a storage configured to hang certain files in the "public" folder, but now I would need to upload files (for this I use dropzone js), with the particularity that when they are uploaded must be copied to a directory that is not within the project,
Its path is /var/www/html/images/catalog/cars
My question is how can I do it with laravel, I have tried to configure a "storage" of type "folder" but it gives me an error.
In the file config/filesystems.php I have created this
'public' => [
'driver' => 'images-cars',
'root' => storage_path('/var/www/html/images/catalog/cars'),
'url' => env('APP_URL').'/catalog-cars',
'visibility' => 'public',
],
It had occurred o create a "Symbolic link" within a project folder to the other folder, but I don't know if it would work.
Thanks in advance.
You can direct to your desired folder by using public_path function like as below:
'public' => [
'driver' => 'public',
'root' => public_path('../../images/catalog/cars'),
'visibility' => 'public',
],
I'm trying to store and show image for my app. Image storing properly but failed to show.
First I created symbolic link using command: php artisan storage:link
config/filesystem.php look like:
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
to store image:
if($request->hasFile('avatar'))
{
$path = $request->file('avatar')->store('avatars');
//$path=avatars/generated_image_name.extension
$user->avatar=$path;
$user->save();
}
//image storing under: (../storage/app/avatars)
to display image to view:
<img src="storage/{{$user->avatar}}"
alt="no image!!"
class="img-circle"
style="width:150px;height:150px;">
tried src="storage/app/{{$user->avatar}}" & src="{{$user->avatar}}"
but can't fetch image. Don't understand what I'm doing wrong.
Hate to be that guy, but have you read the docs. You need to create a symbolic link from public/storage to storage/app/public.
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.
To create the symbolic link, you may use the storage:link Artisan
command:
php artisan storage:link
Edit
Again I would check out the docs or maybe read them :).
I think your default storage is local and not public. In config/filesystems.php look at the default driver.
Storing Files > File Uploads > Specifying A Disk
or try something like this.
if($request->hasFile('avatar'))
{
$path = $request->file('avatar')->store(
'avatars/'.$request->user()->id, 'public'
);
//$path=avatars/generated_image_name.extension
$user->avatar=$path;
$user->save();
}
I want to create a hyperlink to download a file in my storage/app/public folder. I am using this in the blade
Read Paper
But this show me the error file not found.What should i do
do
this is my filesystem.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'visibility' => 'public',
],
An easy way that allow you to store your files in the storage folder and allow to download without system changes: (in laravel 5.3)
You can make a normal link to the file:
Read Paper
Then you need to configure your routes/web.php
Route::get('/download/{file}', function ($file='') {
return response()->download(storage_path('app/public/'.$file));
});
This should open the download dialog for all the kinds of files, also for img's and pdf's. If you want to show the files online and not to download you should use the file method:
return response()->file(storage_path('app/public/'.$file));
Asset paths are relative to the /public directory. So you would just want asset('1489082996.docx') assuming you've properly sym linked your storage directory with php artisan storage:link.
Can I use laravel filesystem to save file outside current project?
I have this structure: project and cdn.
I've tried custom driver but fails. It wont put any file at cdn folder.
'custom' => [
'driver' => 'local',
'root' => '~/Code/cdn.web/storage',
],
$store = Storage::disk('custom')->put('index.txt', 'contents');
I've followed this answer too Storing files outside the Laravel 5 Root Folder
But there is no custom driver.
Is there anyway to make it working?
Thanks
Found the answer.
Laravel doesn't support ~ on the path.
I need to put full path to make it working.
'custom' => [
'driver' => 'local',
'root' => '/home/vagrant/Code/cdn.web/storage',
],