将FTP转换为SFTP (CURL / PHP)

convert FTP to SFTP (CURL / PHP)

提问人:nechmi bader 提问时间:11/7/2023 最后编辑:nechmi bader 更新时间:11/7/2023 访问量:58

问:

我有一个PHP应用程序,适用于FTP协议。 我想从 FTP 切换到 SFTP。

我的代码适用于 ftp,如果我安装其他库以继续使用 sftp/curl

        private function send($fileName)
  {
    $ancvSageConf = sfConfig::get("ap_reports");
    $remoteName = $ancvSageConf["ftpDir"] . basename($fileName);
    $timeout = $ancvSageConf["ftpTimeout"];
    $fp = fopen($fileName, 'r');

    $this->curl = curl_init("ftp://" . $ancvSageConf["ftpAddress"] . "/" . $remoteName);
    curl_setopt($this->curl, CURLOPT_PORT, $ancvSageConf["ftpPort"]);
    curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($this->curl, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
    curl_setopt($this->curl, CURLOPT_VERBOSE, TRUE);
    curl_setopt($this->curl, CURLOPT_USERPWD, $ancvSageConf["ftpUser"] . ":" . $ancvSageConf["ftpPass"]);
    curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($this->curl, CURLOPT_BINARYTRANSFER, TRUE);

    curl_setopt($this->curl, CURLOPT_FTP_USE_EPSV, TRUE);
    curl_setopt($this->curl, CURLOPT_UPLOAD, 1);
    curl_setopt($this->curl, CURLOPT_INFILE, $fp);
    curl_setopt($this->curl, CURLOPT_INFILESIZE, filesize($fileName));

    curl_setopt($this->curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($this->curl, CURLOPT_FTP_USE_EPSV, false);
    curl_exec($this->curl);
    $errorNo = curl_errno($this->curl);

    fclose($fp);
    curl_close($this->curl);

  }

我应该在我的代码中更改什么以使用 SFTP 而不是 FTP?

我无法测试我的代码的问题,服务器只能在生产中访问

谢谢。

php 卷曲 ftp sftp

评论

1赞 Sammitch 11/7/2023
SFTP 和 FTP 的关联仅在于它们传输文件,协议完全不同,因此需要完全不同的库和工作流程。
0赞 nechmi bader 11/7/2023
不能将 curl 与 SFTP 一起使用吗?
0赞 Adi 11/7/2023
你能检查一下这个图书馆吗?phpseclib
0赞 Sammitch 11/7/2023
如果使用 libssh 编译,则 TIL curl 支持 SSH。你可以试试这个,但 YMMV。还要记住,SFTP 不是 FTP,您可能应该从头开始使用此功能。
0赞 nechmi bader 11/7/2023
所以我应该用 sftp:// !!!!!改变 ftp://我无法测试代码的问题,服务器只能在生产环境中访问

答: 暂无答案