0

My PHP application dynamically generates some script files that are later run by using require or include.

I'm now in the process of migrating my production server to Amazon Web Services, to get scaling and load balancing. This means that the generated script files should no longer be stored in the local file system, but rather in an S3 bucket.

Therefore I would like to use Flysystem, so I don't have to think about whether the files are stored locally (development) or in an S3 bucket (production).

But how do I require or include a file from Flysystem? Do I have to get the file content and then eval it?

Magnar Myrtveit
  • 2,432
  • 3
  • 30
  • 51
  • flysystem is not for that purpose, `Flysystem is a filesystem abstraction which allows you to easily swap out a local filesystem for a remote one` it was written to deal with static files such as txt files – hassan Jun 08 '17 at 13:23

2 Answers2

0

It is not possible to include or require a file directly from Flysystem. A workaround is to get the content of the file and eval it. Note that you must handle any leading <?php and trailing ?> tags in the evaled file.

Magnar Myrtveit
  • 2,432
  • 3
  • 30
  • 51
-1

Local storage:

use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;

$adapter = new Local(__DIR__.'/path/to/root');
$localFilesystem = new Filesystem($adapter);

S3 Storage:

use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;

$client = S3Client::factory([
    'credentials' => [
        'key'    => 'your-key',
        'secret' => 'your-secret',
    ],
    'region' => 'your-region',
    'version' => 'latest|version',
]);

$adapter = new AwsS3Adapter($client, 'your-bucket-name', 'optional/path/prefix');
$s3Filesystem = new Filesystem($adapter);

Then, open your file with one, and write to the other:

$contents = $localFilesystem->read('path/to/file.txt');
$s3Filesystem->write('path/to/file.txt', $contents);

You shouldn't need to require, include, or eval anything! Hope this helps!

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39