rumd3x / php-ftp
一个用于通过FTP处理文件的简单易用的PHP工具。
0.1.2
2022-03-02 04:21 UTC
Requires
- php: >=5.5
- ext-mbstring: *
- nesbot/carbon: ^1.33
- rumd3x/php-baseobject: *
This package is auto-updated.
Last update: 2024-09-29 05:34:17 UTC
README
一个用于通过FTP处理文件的简单易用的PHP工具。
安装
通过composer安装,只需运行
composer require rumd3x/php-ftp
用法
连接到服务器
构造函数可以接受任意数量和顺序的参数。它将自动识别主机、端口和SSL设置,但你仍然需要首先指定用户名,然后是密码。
如果需要,你可以将端口指定为整数,默认端口为21。
你可以通过传递一个包含字符串'SSL'的额外参数来指定连接是否使用SSL。
你也可以不指定任何参数,稍后再连接。
$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;
其他FTP命令
保持连接活跃
$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
要列出当前目录中的所有内容,只需
$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
处理目录
你可以使用连接内置的方法在文件夹之间导航并创建新文件夹。
$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"
要获取当前目录的文件夹列表
$ftp = new Rumd3x\Ftp\Ftp('host.example.com', 'user', 'pass', 21); $folders = $ftp->getFolders(); // Outputs an array of Rumd3x\Ftp\FtpFolder
或通过名称获取特定文件夹的FtpFolder实例
$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
创建和删除目录
你还可以使用FtpFolder对象在文件夹之间导航、创建和删除。
//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
处理文件
要获取当前目录中的文件列表
$ftp = new Rumd3x\Ftp\Ftp('host.example.com', 'user', 'pass', 21); $folders = $ftp->getFiles(); // Outputs an array of Rumd3x\Ftp\FtpFile
或通过名称获取特定文件的FtpFile实例
// 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
要读取服务器上文件的全部内容,只需
$file->getContents(); // Returns a string with the file contents
要从服务器上删除文件,只需
$file->delete(); // Returns a boolean with the success flag
下载文件
要下载服务器上的文件,只需
$local_file = '/tmp/file.txt'; $file->download($local_file); // Will download the file to the local file path
对于大文件,你还可以通过传递第二个参数进行异步下载
$local_file = '/tmp/file.txt'; $file->download($local_file, true); // Will download the file asynchronously to the local file path
你还可以传递一个回调函数,在下载完成后执行。
$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.'; } });
编辑文件
$contents = 'new file contents'; $file->setContents($contents); $file->upload();
创建新文件
$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();