【问题标题】:PHP copy file from HTTP URL to FTP serverPHP 将文件从 HTTP URL 复制到 FTP 服务器
【发布时间】:2017-07-19 21:50:21
【问题描述】:

我有这个链接:http://gdlp01.c-wss.com/gds/6/0300002536/03/PSG11_CUG_EN_03.pdf,我想把这个文件复制到我的 FTP 服务器上。我试过了:

$file = "http://gdlp01.c-wss.com/gds/6/0300002536/03/PSG11_CUG_EN_03.pdf";
$data = file_get_contents($url);

$ftp_server = "ftp_server";
$ftp_user = "ftp_user";
$ftp_pass = "ftp_pass";

$ftp = ftp_connect($ftp_server,21) or die("Couldn't connect to $ftp_server"); 

if (ftp_login($ftp, $ftp_user, $ftp_pass)) {
  echo "Connecté en tant que $ftp_user@$ftp_server\n";
} else {
  echo "Connexion impossible en tant que $ftp_user\n";
}

连接成功,但之后不知道如何开始。

【问题讨论】:

    标签: php ftp


    【解决方案1】:

    你必须使用ftp_fput,但我不确定这个函数是否能够处理 URL(我不这么认为),所以我决定将你现有的变量放入内存并伪造文件处理程序:

    $tmpFile = fopen('php://memory', 'r+');
    fputs($tmpFile, $data);
    rewind($tmpFile);
    if (ftp_fput($ftp, 'manual.pdf', $tmpFile, FTP_ASCII)) {
     echo "worked";
    } else {
     echo "did not work";
    }
    

    【讨论】:

      【解决方案2】:

      如果你有URL wrappers enabled,那么简单:

      $file = "http://gdlp01.c-wss.com/gds/6/0300002536/03/PSG11_CUG_EN_03.pdf";
      $ftp_server = "ftp_server";
      $ftp_user = "ftp_user";
      $ftp_pass = "ftp_pass";
      
      copy($file, "ftp://$ftp_user:$ftp_pass@$ftp_server/PSG11_CUG_EN_03.pdf");
      

      如果您需要更好地控制写入(传输模式、被动模式、偏移量、读取限制等),请使用 ftp_fputphp://temp (or the php://memory) stream 的句柄。

      Transfer in-memory data to FTP server without using intermediate file

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-08
        • 2014-04-18
        • 1970-01-01
        • 2015-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多