lazzard / php-ftp-client
这个库提供帮助类和方法,以面向对象的方式管理FTP文件。
v1.7.0
2022-03-13 23:25 UTC
Requires
- php: ^7.4 || ^8.0
- ext-ftp: *
Requires (Dev)
- phpunit/phpunit: ^9.0
README
Lazzard/FtpClient
这个库提供帮助类和方法,以面向对象的方式管理FTP文件。
这个库旨在成为针对旧版本(^5.5)和较新版本(^8.0)PHP发行版的完整FTP/FTPS客户端解决方案,这些版本支持FTP扩展。 (见下方的版本指南)
快速入门
composer require lazzard/php-ftp-client
<?php require __DIR__ . '/vendor/autoload.php'; use Lazzard\FtpClient\Connection\FtpSSLConnection; use Lazzard\FtpClient\Config\FtpConfig; use Lazzard\FtpClient\FtpClient; try { if (!extension_loaded('ftp')) { throw new \RuntimeException("FTP extension not loaded."); } $connection = new FtpSSLConnection('host', 'username', 'password'); $connection->open(); $config = new FtpConfig($connection); $config->setPassive(true); $client = new FtpClient($connection); print_r($client->getFeatures()); $connection->close(); } catch (Throwable $ex) { print_r($ex->getMessage()); }
用法
上传/下载
// download a remote file $client->download('path/to/remote/file', 'path/to/local/file'); // upload a local file to remote server $client->upload('path/to/local/file', 'path/to/remote/file');
异步上传/下载
// download a remote file asynchronously $client->asyncDownload('path/to/remote/file', 'path/to/local/file', function ($state) { // do something every second while downloading this file }, 1, FtpWrapper::BINARY); // upload a remote file asynchronously $client->asyncUpload('path/to/local/file', 'path/to/remote/file', function ($state) { // do something }, 1, FtpWrapper::BINARY);
更多关于异步内容的信息 请在此处查看。
列出
// get files names within an FTP directory $client->listDir('path/to/directory'); // get only directories $client->listDir('path/to/directory', FtpClient::DIR_TYPE); // get detailed information of each file within a remote directory including // the file path of each file $client->listDirDetails('path/to/directory'); // recursively $client->listDirDetails('path/to/directory', true);
复制
// copy a remote file/directory to another directory $client->copy('path/to/remote/source', 'path/to/remote/directory'); // copy a local file/directory to the server $client->copyFromLocal('path/to/local/file', 'path/to/remote/directory'); // copy a remote file/directory to local machine $client->copyToLocal('path/to/remote/source', 'path/to/local/directory');
搜索
// get all png files within the giving directory with their details $client->find('/.*\.png$/i', 'path/to/directory'); // recursively $client->find('/.*\.png$/i', 'path/to/directory', true);
大小
// get file size $client->fileSize('path/to/file'); // get directory size $client->dirSize('path/to/directory');
文件/目录创建
// create an FTP file $client->createFile('path/to/file'); // create a file with content $client->createFile('path/to/file', 'Hello world!!'); // create a remote directory // note: this method supports recursive directory creation $client->createDir('directory');
追加
// append the giving content to a remote file $client->appendFile('path/to/file', $content);
删除/重命名
// remove an FTP file $client->removeFile('path/to/file'); // remove an FTP directory (be careful all the files within this directory will be removed) $client->removeDir('path/to/directory'); // rename an FTP file/directory $client->rename('path/to/file', $newName);
移动
// move an FTP file or directory to another folder $client->move('path/to/file', 'path/to/directory');
计数
// get the count of all the files within a directory $client->getCount('path/to/directory'); // recursively $client->getCount('path/to/directory', true); // recursively and files only $client->getCount('path/to/directory', true, FtpClient::FILE_TYPE);
权限
// set a permissions on the giving FTP file/directory $client->setPermissions('path/to/file', [ 'owner' => 'r-w', // read & write 'group' => 'w', 'world' => 'w-r-e' ]); // or you can use the UNIX file permission digits $client->setPermissions('path/to/file', 777);
是方法
// is an ftp directory ? $client->isDir('path/to/file/or/directory'); // is a file type ? $client->isFile('path/to/file/or/directory'); // is an empty file/directory ? $client->isEmpty('path/to/file/or/directory'); // is exists on the FTP server ? $client->isExists('path/to/file/or/directory'); // is the server support the size feature ? $client->isFeatureSupported('SIZE');
其他
// get the last modified time of the giving file (not working with directories) $client->lastMTime('path/to/file'); // get a content of an FTP file $client->getFileContent('path/to/file', FtpWrapper::ASCII); // get all supported features by the FTP server $client->getFeatures(); // get the server system $client->getSystem(); // send a request to allocate a space of bytes for the next transfer operation // some FTP servers requires this before transfer operations $client->allocateSpace(2048); // prevent the server from closing the connection and keeping it alive $client->keepAlive();
您可以在此处查看所有可用方法。
更多文档
- 使用 ConnectionInterface 操作FTP连接。
- 使用 FtpConfig 配置连接实例。
- 从基础类 FtpClient 开始工作。
- 使用 FtpCommand 发送FTP命令。
- 如何直接使用 FtpWrapper 类。
- 运行集成测试。
版本指南
贡献
如果您想增强它或添加新功能,请随意fork此存储库,也欢迎报告在使用库时遇到的问题,非常感谢!