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

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

我尝试 运行 Flysystem 的 Local 适配器的基本示例代码并得到 Class 'League\Flysystem\Adapter\Local' not found 错误。这是我的过程:

版本检查:

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

安装飞行系统:

composer require league/flysystem

输出显示我是最新的(这是我第二次 运行ning):

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

现在 Web 根目录中有一个 vendor 文件夹。 ./web/vendor/league/flysystem/src/Adapter$ 中有这些文件:

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

...只是表明它似乎安装正确(?)我在我的网络根目录中创建了一个测试文件和一个测试目录:

  1. 飞-local.php
  2. 我的文件/

进入 fly-local.php 我从他们的文档 (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);

...并将适配器的根文件夹更改为 myfiles(正确吗?)。那我运行吧:

php fly-local.php

它输出:

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

我做错了什么?

您使用了 composer,那么您需要包含 composer autoload.php 文件。

fly-local.php应该是:

<?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);

如果您使用框架,您会看到它包含自动加载的 php 文件(通常为 index.php)。如果您的 test/custom 文件未包含在框架中,您需要手动包含该文件。