Working with the League

Flysystem is one of awesome packages provided byPHP League, a group of developers who have banded together to build solid, well tested PHP packages using modern coding standards.

Table Of Content

  1. Flysystem Introduction
  2. Building RemoteCopy class
  3. The end
1. Flysystem Introduction

We had the need to move images from our legacy server to a new server for one of our projects. Instead of downloading hundreds of images manually, which was too tedious. We used Flysystem to do the job for us.

Flysystem provides an abstraction for various filesystems. Currently it supports:

  • Local Disk
  • Azure
  • AWS S3
  • Dropox
  • FTP
  • GridFS
  • Memory
  • Rackspace
  • ReplicateAdapter
  • SFTP
  • WebDAV
  • ZipArchive

What does this mean is that we can treat all the filesystems above as if they are the same. The benefits are obvious. We can switch between the filesystems without much of trouble. And we can move a file from one filesystem to another in a breeze.

2. Building RemoteCopy class

Back to our project, our requirement is to read a file remotely from a URL and then copy it to a local disk.

First let's create a class RemoteCopy with a static method copy, which takes two parameters $remote and $desDir. Parameter $remote is a string pointing to a remote URL and $desDir is a string containing the destination directory where you want to store the downloaded images.

The class signature looks like this:

Next we instantiate an Flysystem object with Local adapter:

Then we bind the remote image to a stream using PHP's native function fopen():

Next we use the method putStream of Flysystem object to read the content of remote image and put it into our local disk. At the end, we close the remote stream:

That is basically it. Now this simple code snippet can be used to download image from a remote URL and store in a defined local directory.

To make the function to return a more useful value, It makes sense to have method copy return a directory path of the downloaded file. If the method fails due to unexpected reasons, it will just return NULL.

The final code:

3. The End

We hope you found this tutorial useful. If you have any question, do leave a comment below. If you like our tutorial, please tweet and share it on Facebook.

Hopefully this simple tutorial helped you with your development .
If you like our post, please follow us on Twitter and help spread the word . We need your support to continue.
If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know .