当我无法访问PHP的本地FTP句柄时,如何使用PHP CLI来自动化FTP? [英] How do I use PHP CLI to automate FTP when I don't have access to PHP's native FTP handle?

查看:98
本文介绍了当我无法访问PHP的本地FTP句柄时,如何使用PHP CLI来自动化FTP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生产服务器上编写一个自动化脚本,除此之外,它需要通过FTP获取远程文件列表(FTP是与远程文件系统交互的唯一选项)并有选择地下载它们。 p>

为什么我不能使用PHP的本地FTP包装

这是一个生产服务器在非常脆弱的环境中。我正在使用PHP CLI编写它,因为大多数现有的自动化脚本都是以这种方式编写的。然而,虽然我有一个非常新的PHP 5.1.2安装,但我无法使用--with-ftp重新编译它,并且该选项未启用。



其余选项



因此,我的选项是连接,获取我的文件列表,并有选择地使用shell_exec()或php_filesystem函数使用FTP流和PHP本地文件系统功能。



不幸的是,我找不到任何好的代码示例。当我尝试使用FTP命令执行shell_exec时,程序挂起,大概是因为一旦打开FTP提示符,控制就停留在shell中。

  $ ftp_connect_command =ftp -v -n $ bl_ftp_host; 
$ ftp_login_command =user $ bl_ftp_user $ bl_ftp_password;
$ ftp_bye_command =bye;

$ ftp_connect_response = shell_exec($ ftp_connect_command);
//这个永远不会执行,因为它在这里挂起,等待返回到shell
$ ftp_login_response = shell_exec($ ftp_login_command);

或者,我想像基于流的方式来做到这一点:

  $ ftp_path =ftp:// $ bl_ftp_user:$ bl_ftp_user @ $ bl_ftp_host /; 
$ stream_options = array('ftp'=> array('overwrite'=> false));
$ context = stream_context_create();
if($ dh = opendir($ ftp_path,$ context))
{
while(filename = readdir($ dh))
{
print($ filename );
}
}

但我不确定这是否被认为是可靠的方法。



任何人都可以提供代码示例,展示如何捕获目录列表并通过这些方法下载文件?

解决方案

显然,wordpress使用 pemftp for pure-PHP FTP(在没有FTP支持的情况下编译的系统上)


I'm writing an automation script on a production server that, among other things, needs to grab a list of remote files via FTP (FTP is the only option for interacting with the remote filesystem) and selectively download them.

Why I can't use PHP's native FTP wrappers

This is a production server in a very brittle environment. I'm writing it using PHP CLI, since most of the existing automation scripts are written this way. However, although I have a very new PHP 5.1.2 installation, I'm not able to recompile it with --with-ftp, and that option is not enabled.

The remaining options

So, my options are to connect, get my file list, and selectively download using shell_exec() or the php_filesystem functions using an FTP stream and the PHP native filesystem functions.

Unfortunately, I'm not able to find good code examples of either. When I try to shell_exec using FTP commands, the program hangs, presumably because control stays at the shell once I open up the FTP prompt.

$ftp_connect_command = "ftp -v -n $bl_ftp_host";
$ftp_login_command = "user $bl_ftp_user $bl_ftp_password";
$ftp_bye_command = "bye";

$ftp_connect_response = shell_exec("$ftp_connect_command");
// this never executes, because it hangs here waiting for a return to shell
$ftp_login_response = shell_exec($ftp_login_command);

Or, I imagine the stream based way to do this would be:

$ftp_path = "ftp://$bl_ftp_user:$bl_ftp_user@$bl_ftp_host/";
$stream_options = array('ftp' => array('overwrite' => false));
$context = stream_context_create();
if ($dh = opendir($ftp_path, $context))
{
    while (filename = readdir($dh))
    {
        print($filename);
    }
}

But I'm not sure if this is considered a reliable method.

Can anyone provide code samples showing how to capture a directory list and download files by either of these methods?

解决方案

Apparently, wordpress uses pemftp for pure-PHP FTP (on systems compiled without FTP support)

这篇关于当我无法访问PHP的本地FTP句柄时,如何使用PHP CLI来自动化FTP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆