Search code examples
phpcodeigniter-2flysystem

Calling Flysystem, why do I get PHP Fatal error: Class 'League\Flysystem\Adapter\Local' not found?


I try to run Flysystem's basic example code for the Local adapter and get a Class 'League\Flysystem\Adapter\Local' not found error. This is my process:

version check:

php -v
PHP 5.5.9-1ubuntu4.23 (cli) (built: Feb  8 2018 21:59:47)

install Flysystem:

composer require league/flysystem

output shows I'm up-to-date (this is my 2nd time running it):

Using version ^1.0 for league/flysystem
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

now there's a vendor folder in web root. And within ./web/vendor/league/flysystem/src/Adapter$ are these files:

AbstractAdapter.php
AbstractFtpAdapter.php
CanOverwriteFiles.php
Ftpd.php
Ftp.php
Local.php
NullAdapter.php
Polyfill/
SynologyFtp.php

...just showing that it seems to be installed correctly(?) I create one test file and one test directory in my web root:

  1. fly-local.php
  2. myfiles/

Into fly-local.php I paste the text from their docs (https://flysystem.thephpleague.com/docs/adapter/local/):

<?php

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

$adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
$filesystem = new Filesystem($adapter);

...and change the adapter's root folder to myfiles (is that correct?). Then I run it:

php fly-local.php

It outputs:

PHP Fatal error:  Class 'League\Flysystem\Adapter\Local' not found in /[PROJECT DIR]/web/fly-local.php on line 6
PHP Stack trace:
PHP   1. {main}() /[PROJECT DIR]/web/fly-local.php:0

What am I doing wrong?


Solution

  • You used composer, then you need to include the composer autoload.php file.

    The fly-local.php should be:

    <?php
    
    require __DIR__.'/vendor/autoload.php';
    
    use League\Flysystem\Filesystem;
    use League\Flysystem\Adapter\Local;
    
    $adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
    $filesystem = new Filesystem($adapter);
    

    If you use a framework, you can see it includes the autoload php file for you (index.php, in general). If your test/custom file is not included to framework, you need to include the file manually.