There are no files to be backed up spatie/laravel-backup - laravel

I'm trying to backup a gallery folder stored in storage/app/public/gallery so I added
'include' => [
base_path('storage/app/public/gallery'),
],
to the backup config and run 'php artisan backup:run', it gives the error
Backup failed because There are no files to be backed up..
I tried a different approach and change it to
'include' => [
base_path('storage'),
],
The backup works but I get everything inside the storage folder except the public folder. It somehow excludes the public directory.

Related

php artisan storage:link doesn't create link

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'),
],

Where it is recommended to save files in Laravel

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'),
],

Laravel - Deleting cache files - insufficient permissions

I'm trying to delete some files located in storage/framework/cache/data using php artisan cache:clear. This is silently failing, because the user trying to perform the action does not have write access to these files.
The files are located on a linux server running Nginx - the folders have the following permissions/owner/group:
drwxr-xr-x www-data www-data
and the files have:
-rw-r--r-- www-data www-data
The user I am trying to delete the cached files with is called deploy, and is a member of the www-data group. I can't use sudo because this command happens during a deploy script.
I have found this pull request, https://github.com/laravel/framework/pull/31579, which seems to describe my exact problem, and I have also looked at https://laravel.com/docs/7.x/filesystem#the-local-driver (under the permissions section). Based on this, I tried adding the following to filesystems.php:
'permissions' => [
'file' => [
'public' => 0775,
'private' => 0775,
],
'dir' => [
'public' => 0775,
'private' => 0775,
],
]
under both the public and local disk sections, to see if Laravel created the cache files with these permissions, rather than the ones shown above, however it didn't seem to affect anything. I'm guessing this only applies to files stored or created with the File facade, and not automatically generated cache files?
How can I set the permissions that Laravel creates these files with?
Thanks in advance for any help

Deployer PHP not copying files from Laravel's storage folder

I'm using Deployer PHP with laravel and rsync recipes to deploy my Laravel project but for some reason the files in the storage/app folder are not copied from the git branch to the storage folder on the server.
The .gitignore is correctly set and the files that I need to be synced are in fact in git. So the problem seems to lie with Deployer not copying the files to the shared/storage/app folder
storage/app/.gitignore:
*
!.gitignore
!public/
!public/*
!templates/
!templates/*
!templates/fonts/
!templates/fonts/*
deployer.php
<?php declare(strict_types=1);
namespace Deployer;
// Include the Laravel & rsync recipes:
require 'recipe/rsync.php';
require 'recipe/laravel.php';
...
set('rsync_src', function () {
return __DIR__ . '/www'; // My LAravel project is in a sub folder of the git branch
});
// Configuring the rsync exclusions.
add('rsync', [
'exclude' => [
'.git',
'/.env',
'/storage/framework/',
'/storage/logs/',
'/vendor/',
'/node_modules/',
'.gitlab-ci.yml',
'deploy.php',
],
]);
...
Any ideas on what could be the issue and possible solution here?
This appears to be by design. Deployer manages /storage as a shared folder because it contains information that you probably want to retain across releases, such as sessions, logs, etc.
I'm guessing the files you want moved in this way would be better off if you maintained them under /resources

How to open file from storage by URL address?

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.

Resources