dongwai4434 2018-04-09 20:35 采纳率: 0%
浏览 66

Laravel中的CFFTP等价物

I have a ColdFusion page on which a secure FTP connection is made to a remote ftp server, and the names of all of the files in a certain directory on that server are listed (in a query object). This is relatively simple in CF - you use the cfftp tag to open an ftp connection, and then use the cfftp tag again to list out the files that are in a specific directory:

<cfftp action = "open"
              username = "username"
              connection = "myConnection"
              password = "password"
              fingerprint = "(eight pairs of hexadecimal values in the form 
              hh:hh:hh:hh:hh:hh:hh:hh)"
              server = "xxx.xxx.xx.xx"
              secure = "yes">

    <cfftp action = "listDir"
                connection="myConnection"
                directory="Output_Directory"
                name="qDir" />
<!--- Files in the specified directory are now stored in a query object named "qDir", and their filenames can be pulled using a CF query of query --->

I'm new to PHP/Laravel, and am trying to figure out how to do the same in Laravel. So far, I haven't found any videos or tutorials that explain how to open ftp connections in Laravel (normal or secure), or how to view the files within a directory once that connection has been made.

I did find something at https://laravel.com/docs/5.6/filesystem#downloading-files, on what to put in the filesystems.php page for SFTP driver configuration. Which, I gather, needs to be set up before I can open any SFTP connection within my Laravel site. But, I'm finding this a little confusing. As seen below, that code appears to require values for "privateKey" and "password", and includes no optional setting for "fingerprint", which I'm including in the CFFTP tag above.

//include on filesystems.php:

'sftp' => [
    'driver' => 'sftp',
    'host' => 'example.com',
    'username' => 'your-username',
    'password' => 'your-password',

    // Settings for SSH key based authentication...
    // 'privateKey' => '/path/to/privateKey',
    // 'password' => 'encryption-password',

    // Optional SFTP Settings...
    // 'port' => 22,
    // 'root' => '',
    // 'timeout' => 30,
],

I can connect to the ftp server securely through an ftp client (Bitvise), which only asks for the username, password, host IP, and port (22). So I'm not sure what "privateKey" or "password" values need to be provided to the code above...nor how there can be two variables named "password" within the 'sftp' array (one for "your-password" and one for "encryption-password").

Laravel seems to have good tutorial videos in general -- can anyone point me to any where ftp/sftp is covered, particularly "open" and "listDir" type actions? (I'm looking for Laravel in particular -- I can figure out how it's done in vanilla PHP on my own). Thanks much.

(Addendum) adding this here later, so that I can wrap the below in code format. I think one thing I need to do is use the ftp filesystem driver, rather than the sftp filesystem driver, and set 'ssl' to true. So on filesystems.php, I should have something like:

'ftp' => [
            'driver'   => 'ftp',
            'host'     => env('FTP_SERVER'),
            'port'     => 22,//env('FTP_PORT'),
            'username' => env('FTP_USERNAME'),
            'password' => env('FTP_PASSWORD'),
            'passive'  => true,
            'ssl'      => true,
            'timeout'  => 30,
            'root'     => '/',
        ],

Still working on establishing a connection; though. I was getting an error message when I tried to do that: "Use of undefined constant FTP_BINARY - assumed 'FTP_BINARY'". Figured out that problem - was because I needed to enable php_ftp.dll in the php config, as this is not enabled by default on Windows. But now I'm getting a different error message -- "Could not connect to host xxx.xxx.xx.xx port:22". Will dig into that, see what the problem is and report back.

(Later) Looks like I'm having an authentication problem of some sort. I've tried connecting to the same FTP server just using vanilla PHP, i.e., the ftp_ssl_connect() function. That function is returning false, which means there's an error connecting. (Before I enabled php_ftp.dll, I was just getting an error message saying that ftp_ssl_connect() was an undefined function). Wondering if omitting the fingerprint, which we were previously including as a param with the Coldfusion cfftp tag, has something to do with it. But this isn't a possible parameter in the Laravel or core PHP functions. Digging...will report back...

(4/11/18) The crucial question seems to be what exactly is happening with the cfftp tag, and how it's making its connection (sFTP, or FTPS?) Because however it's working is what we want to re-create in PHP/Laravel. I've asked a separate question regarding this.

The documentation for the cfftp tag says nothing about it using sFTP or FTPS. It only says that when you specify secure="yes", then it allows you to open a connection to a Secure Shell (SSH) server by using either symmetric or asymmetric encryption. When using symmetric encryption (which is what we're doing), you pass username, password, and fingerprint (which we do).

You can specify the port in the cfftp tag; if no port is specified, then it defaults to 21. As a test, I've explicitly passed the port as both 21 and 22, and the connection worked fine in both cases. I'm not sure why that is; I'd expect it to work in one case or the other, but not both.

Meanwhile, in case we indeed need to use SFTP, I've tried making a SFTP connection to the ftp server in core PHP, using the ssh2_connect() function. I'm getting an error message saying that this function is undefined, even though I've added and enabled the php_ssh2.dll extension, so that's another thing to look into.

  • 写回答

1条回答 默认 最新

  • dth2331 2018-04-09 20:45
    关注

    The privateKey field is if you're using public key authentication on your SSH connection. Public key authentication is used as an alternative to standard username/password authentication. It's typically considered more secure than just username/password. Here's a decent primer.

    Laravel's SFTP filesystem driver uses The PHP League's flysystem-sftp: https://github.com/thephpleague/flysystem-sftp

    If you opt to not use public key authentication, you can probably leave that field commented out. The second password entry is probably just a typo in the documentation; see the flysystem-sftp project for the actual configuration syntax.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题