定制 rumd3x/php-ftp 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rumd3x/php-ftp

Composer 安装命令:

composer require rumd3x/php-ftp

包简介

A Nice and easy to use PHP utility for handling Files over FTP.

README 文档

README

A Nice and easy to use PHP utility for handling Files over FTP.

Installation

To install via composer just run

  composer require rumd3x/php-ftp

Usage

Connecting to Server

The constructor takes any amount of arguments, in any order. It will identify automatically the host, port and ssl specifications, but you still have to specify the username first, then the password.

If you need to, you can specify the port as an integer, the default is 21.

You can specify if the connection uses SSL or not by passing an extra argument with the string 'SSL'.

You can also not specify any args and connect later.

$ftp = new Rumd3x\Ftp\Ftp('host.example.com', 'user', 'pass', 21);
$conn1 = $ftp->isConnected(); 

print_r($conn1); // Returns a boolean;

//or

$ftp2 = new Rumd3x\Ftp\Ftp();
$conn2 = $ftp2->setHost('192.168.1.123')->setSecure()->setPort(666)->connect()
->setUser('test')->setPass('secret')->login()->isConnected();

print_r($conn2); // Returns a boolean;

// or 

$ftp3 = new Rumd3x\Ftp\Ftp(21, 'ssl', 'user', 'pass', 'host.example.com');
$conn3 = $ftp3->isConnected(); 

print_r($conn3); // Returns a boolean;

Other FTP Commands

To keep the connection alive

$ftp = new Rumd3x\Ftp\Ftp('host.example.com', 'user', 'pass', 21);
$ftp->keepAlive(); // Sends NOOP to the server to keep the connection alive

$return = $ftp->executeRaw("NOOP"); // Allows you to send a arbitrary commands to the server 

print_r($return); // Outputs a object with the response data

To list everything in the current directory, simply:

$files = $ftp->getAll(); 
// Returns an array of mixed Directories and Files as instances of FtpFolder and FtpFile respectively
// All directories comes first, then all the files

Handling directories

You can navigate through folders and create new folders using methods built-in the connection.

$dir = $ftp->currentFolder(); // gets the current folder directory you are in on the server 
// $dir has "/"

$dir = $ftp->createFolder('test/example/123')->dir('test')->dir('example/123')->currentFolder();
// $dir now has "/test/example/123"

$dir = $ftp->up()->up()->currentFolder();
// $dir now has "/test"

To get the list of folders on your current directory:

$ftp = new Rumd3x\Ftp\Ftp('host.example.com', 'user', 'pass', 21);
$folders = $ftp->getFolders(); // Outputs an array of Rumd3x\Ftp\FtpFolder

Or to get the FtpFolder instance of a specific folder by its name:

$folder = $ftp->getFolder('test'); 
// Outputs an instance of Rumd3x\Ftp\FtpFolder in case the folder with name 'test' exists in the current directory

$folder_name = $folder->name; // name property of FtpFolder 
$folder_full_name = $folder->full_name; // full_name property of FtpFolder 
$folder_timestamp = $folder->timestamp; // timestamp property of FtpFolder 
$folder_permission = $folder->permission; // permission property of FtpFolder 

Creating and Deleting Directories

You can also navigate through folders, create and delete using the FtpFolder Object.

//Connect to the FTP Server
$ftp = new Rumd3x\Ftp\Ftp('host.example.com', 'user', 'pass', 21);

//Create the folder 'FolderName' on your current dir
$folder = new Rumd3x\Ftp\FtpFolder($ftp, 'FolderName');

$folder->create()->navigateTo(); // creates the folder and navigates to it
$ftp->up(); // navigates one level up
$folder->delete(); // deletes the folder from the server

Handling files

To get the list of files on your current directory:

$ftp = new Rumd3x\Ftp\Ftp('host.example.com', 'user', 'pass', 21);
$folders = $ftp->getFiles(); // Outputs an array of Rumd3x\Ftp\FtpFile

Or to get the FtpFile instance of a specific file by its name:

// Outputs an instance of Rumd3x\Ftp\FtpFile in case the folder with name 'file.txt' exists in the current directory
$file = $ftp->getFolder('file.txt'); 

// File properties
$file_name = $file->name; // name property of FtpFile
$file_full_name = $file->full_name; // full_name property of FtpFile
$file_timestamp = $file->timestamp; // timestamp property of FtpFile
$file_permission = $file->permission; // permission property of FtpFile

To read the contents of a file on the server simply

$file->getContents(); // Returns a string with the file contents

To remove a file from the server simply

$file->delete(); // Returns a boolean with the success flag

Downloading Files

To download a file on the server simply

$local_file = '/tmp/file.txt';
$file->download($local_file); // Will download the file to the local file path

For large files you can also make async downloads by passing a second parameter

$local_file = '/tmp/file.txt';
$file->download($local_file, true); // Will download the file asynchronously to the local file path 

You can also pass a callback to be executed on the download completion.

$local_file = '/tmp/file.txt';

// Will also download the file asynchronously to the local file path 
$file->download($local_file, function($status) {
  if ($status === FTP_FINISHED) {
    echo 'Download success.';
  } elseif ($status === FTP_FAILED) {
    echo 'Download failed.';
  } else {
    echo 'Something else happened.';
  }
}); 

Editing Files

$contents = 'new file contents';
$file->setContents($contents);
$file->upload();

Creating new Files

$ftp = new Rumd3x\Ftp\Ftp('host.example.com', 'user', 'pass', 21);

$local_file = '/etc/file.txt';
$contents = file_get_contents($local_file);

//Create the file 'file.txt' on your current dir
$file = new Rumd3x\Ftp\FtpFile($ftp, 'file.txt');
$file->setContents($contents);
$file->upload();

rumd3x/php-ftp 适用场景与选型建议

rumd3x/php-ftp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 184 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「client」 「ftp」 「php-ftp」 「rumd3x」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rumd3x/php-ftp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rumd3x/php-ftp 我们能提供哪些服务?
定制开发 / 二次开发

基于 rumd3x/php-ftp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 184
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-20