php ライブラリ「league/flysystem」を使ってファイルの読み込みや書き込みを行う

phpでライブラリ「league/flysystem」を使ってファイルの読み込みや書き込みするまでのサンプルコードを記述してます。composerを使用して「league/flysystem」はインストールしてます。
環境
- OS windows10 pro 64bit
- Apache 2.4.43
- PHP 7.4.5
- Composer 1.10.5
※windows10にApacheのインストールはこちら
※windows10にphpのインストールはこちら
※Windows10にComposerのインストールはこちら
league/flysystemインストール
phpが動作しているフォルダで、composerを使用してインストールします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
composer require league/flysystem <出力結果> Using version ^1.0 for league/flysystem ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing league/flysystem (1.0.69): Downloading (100%) league/flysystem suggests installing league/flysystem-eventable-filesystem (Allows you to use EventableFilesystem) league/flysystem suggests installing league/flysystem-rackspace (Allows you to use Rackspace Cloud Files) league/flysystem suggests installing league/flysystem-azure (Allows you to use Windows Azure Blob storage) league/flysystem suggests installing league/flysystem-webdav (Allows you to use WebDAV storage) league/flysystem suggests installing league/flysystem-aws-s3-v2 (Allows you to use S3 storage with AWS SDK v2) league/flysystem suggests installing league/flysystem-aws-s3-v3 (Allows you to use S3 storage with AWS SDK v3) league/flysystem suggests installing spatie/flysystem-dropbox (Allows you to use Dropbox storage) league/flysystem suggests installing srmklive/flysystem-dropbox-v2 (Allows you to use Dropbox storage for PHP 5 applications) league/flysystem suggests installing league/flysystem-cached-adapter (Flysystem adapter decorator for metadata caching) league/flysystem suggests installing ext-ftp (Allows you to use FTP server storage) league/flysystem suggests installing league/flysystem-sftp (Allows you to use SFTP server storage via phpseclib) league/flysystem suggests installing league/flysystem-ziparchive (Allows you to use ZipArchive adapter) Package mschop/noteephp is abandoned, you should avoid using it. Use mschop/notee instead. Writing lock file Generating autoload files 21 packages you are using are looking for funding. Use the `composer fund` command to find out more! |
自分の場合は、下記にインストールしました。

league/flysystem利用
「C:\Apache24\htdocs」に「test.php」を下記のコードで作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php use League\Flysystem\Filesystem as FlyFilesystem; use League\Flysystem\Adapter\Local as FlyLocal; use League\Flysystem\FileExistsException as FlyFileExistsException; require_once __DIR__ . '/vendor/autoload.php'; //ファイルを読み込み表示 $fs = new FlyFilesystem(new FlyLocal(__DIR__)); $content = $fs->read('sample.txt'); print($content); //ファイル書き込み(ファイルがなければ新規作成あれば更新) try { $content = $fs->put('sample-white.txt', 'white word'); } catch (FlyFileNotFoundException $e) { throw $e; } |
「test.php」と同じ階層に「sample.txt」を下記の内容で作成しておきます。
1 |
hello world |
実行結果を確認すると、読み込んだファイルの内容が表示され、「 sample-white.txt 」というファイルが作成されていることが確認できます。


-
前の記事
noクラスのCSSフレームワーク new.cssを使ってみる 2020.07.29
-
次の記事
mac catコマンドをシンタックスハイライト表示できる「bat」をインストールする 2020.07.29
コメントを書く