by Devin Yang
(This article was automatically translated.)

Published - 1 year ago ( Updated - 1 year ago )

Today, I will test the use of sftp driver on Laravel. If you have never used it, come and see the results of my test.
By the way, it is very convenient for us to perform the Storage function in the tinker environment of Laravel. , whether local or remote.
After adjusting the settings, remember to exit and enter again.

The Laravel version used in this article is as follows:

dlaravel@4e1f08f98079:~/html$ artisan -V
Laravel Framework 9.40.1

Laravel's Storage sftp driver support can be installed

composer require league/flysystem-sftp-v3 "^3.0"

Then we can add the required driver in config/filesystems.php, here I added a ccc SFTP driver

    'disks' => [
     'ccc' => [
            'driver' => 'sftp',
            'host' => '192.168.99.2',
            'username' => 'dlaravel',
            'privateKey' => '/home/dlaravel/.ssh/id_ed25519',
            'port' => 2258,
            'root' => '/var/www/html/storage/app/public',
            'url'=>'https://www.ccc.tc/storage',
        ],
    ],

Here, we can customize the root as public
/var/www/html is my Laravel project directory.
In this way, when we want to capture the files in the public directory, we can directly give the images directory.
The root is adjusted according to the needs of the time. In my case, it is quite convenient to set the root to public.


In ccc disk In the SFTP driver, I also set the url, so when we use Storage::url,
bring in the files listed in Storage::disk("ccc")→files, the remote end can be captured very smoothly URL of the image file

Remarks, regarding the public folder in storage/app, it should be open to external access, remember to issue a command to create a soft link.

php artisan storage:link

Tags: laravel storage sftp

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


laravel storage

How do I sync files with Laravel's Storage's sftp driver

The installation and setting of SFTP is beyond the scope of this article. If you want to know more, I suggest you read another article https://www.ccc.tc/article/Laravel-SFTP-Driver-Configuration. In this article, I will use a very fast Sample. Demo How do I use the self-built artisan command to synchronize the remote data to the local end. Some people may ask why rsync is not used. This article is to introduce the use of Storage to synchronize files. 🤪Let’s look directly at the code, I set up an sftp disk called ccc in config/filesystems.php.

laravel,sftp

Use Laravel's Storage SFTP Drvier for remote file upload

Why do I separate the front and back of the website? My idea is very simple, that is to rely on a set of background to control all the website data in the foreground. Assuming that the front-end website is a pure marketing website, it is nothing more than the subject content, just like the article above, without any particularly complicated logic. Therefore, it is enough to set up the backend database and connect different frontends. Then there is the last question, how can my backend HTML editor post pictures directly to the frontend? Laravel's Storage SFT Driver is a good antidote.