代码之家  ›  专栏  ›  技术社区  ›  Marty1452

PHP从浏览器上传文件到FTP

  •  1
  • Marty1452  · 技术社区  · 7 年前

    我想上传文件从用户的机器到文件夹 uploads 在我的FTP中,它不起作用。你能帮助我吗?

    $ftp_server = "some ip";
    $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
    $login = ftp_login($ftp_conn, "some name", "some password");
    
    $target_dir = "uploads/";
    $target_file = basename($_FILES["filename"]["name"]);
    
    if (ftp_fput($ftp_conn, $target_dir.$target_file, FTP_ASCII))
      {
      echo "Successfully uploaded $target_file.";
      }
    else
      {
      echo "Error uploading $target_file.";
      }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Martin Prikryl    6 年前

    您需要指定要上载到FTP服务器的本地文件(从Web服务器开始)。

    您可以检索临时文件的名称,该临时文件包含通过HTTP POST从用户计算机上载到Web服务器的文件,使用 $_FILES["filename"]["tmp_name"] . 了解 POST method uploads 在PHP中。

    你可以把它传给我 ftp_put (不需要 ftp_fput ):

    ftp_put($ftp_conn, $target_dir.$target_file, $_FILES["filename"]["tmp_name"], FTP_IMAGE)
    

    代码中的另外两个问题(这些问题不是您当前的问题,但在解决之后您将立即面对它们):

    • FTP_ASCII ,如果您正在上载二进制文件,例如 .mp3 . 使用 FTP_IMAGE

    • 你呢 need to use ftp_pasv .

        2
  •  0
  •   panza panta    7 年前

    使用 php-ftp-client 图书馆:

    $ftp = new \FtpClient\FtpClient();
    $ftp->connect($host, true, 990);
    $ftp->login($login, $password);
    // upload with the BINARY mode
    $ftp->putAll($source_directory, $target_directory);
    
    // Is equal to
    $ftp->putAll($source_directory, $target_directory, FTP_BINARY);
    
    // or upload with the ASCII mode
    $ftp->putAll($source_directory, $target_directory, FTP_ASCII);