📜  PHP | ftp_get()函数

📅  最后修改于: 2022-05-13 01:54:11.613000             🧑  作者: Mango

PHP | ftp_get()函数

ftp_get()函数是PHP的一个内置函数,用于从 FTP 服务器获取或下载文件到本地服务器或机器。

句法:

ftp_get( $ftp_connection, $local_file_path, $server_file_path,
                             $mode_of_file_transfer, $starting_position );

参数:该函数接受上面提到和下面描述的五个参数:

  • $ftp_connection:它是必需的参数。它指定用于从 FTP 服务器下载文件的现有 FTP 连接。
  • $local_file_path:必填参数。它指定文件下载到的本地服务器或机器的路径。
  • $server_file_path:它是必需的参数。它指定要从 FTP 服务器下载文件的路径。
  • $mode_of_file_transfer:它是必需的参数。它指定传输模式。该参数的值为 FTP_ASCII 或 FTP_BINARY。
  • $starting_position:可选参数。它指定远程文件中开始下载的位置。

返回值:成功返回True,失败返回False。

笔记:



  • 此函数适用于PHP 4.0.0 及更新版本。
  • 以下示例无法在在线 IDE 上运行。所以尝试在一些PHP托管服务器或本地主机上运行,并使用正确的 ftp 服务器名称。
  • 应该正确选择模式,即 FTP_ASCII 或 FTP_BINARY

运行以下代码的重要信息:以下代码在在线 IDE 中不起作用,因为它们不允许与文件交互。所以尝试在PHP托管服务器上运行。确保提供正确的 FTP 服务器、用户名、密码、服务器文件路径和本地文件路径。如果文件指定为服务器文件不存在,则会发生错误,因此请确保服务器文件存在。在这些示例中,作为 $server_file 提及的文件将作为本地文件下载到 $local_file 中提供的相对路径。

下面的例子说明了PHP的 ftp_get()函数:

示例 1:

PHP
logged in successfully!";
         
        // Name or path of the localfile to
        // where the file to be downloaded
        $local_file = "local_file.txt";
         
        // Name or path of the server file to
        // be downoaded
        $server_file = "server_file.txt";
         
        // Downloading the specified server file
        if (ftp_get($ftp_connection, $local_file,
                $server_file, FTP_BINARY)) {
            echo "
Successfully downloaded "                . "from $server_file to $local_file.";           }           else {             echo "
Error while downloading from "                   . "$server_file to $local_file.";           }                }     else {         echo "
login failed!";     }           // echo ftp_get_option($ftp_connection, 1);     // Closing  connection        if(ftp_close($ftp_connection)) {         echo "
Connection closed Successfully!";     } } ?>


PHP
logged in successfully!";
          
        // Name or path of the localfile to
        // where the file to be downloaded
        $local_file = "local_file_shiva.jpg";
          
        // Name or path of the server file to
        // be downoaded
        $server_file = "shiva.jpg";
          
        // Downloading the specified server file
        if (ftp_get($ftp_connection, $local_file,
                $server_file, FTP_BINARY)) {
            echo "
Successfully downloaded from"                 . " $server_file to $local_file.";           }           else {             echo "
Error while downloading from"                 . " $server_file to $local_file.";           }                 }     else {         echo "
login failed!";     }            // echo ftp_get_option($ftp_connection, 1);     // Closing  connection         if(ftp_close($ftp_connection)) {         echo "
Connection closed Successfully!";     } } ?>


输出:

示例 2:从 FTP 服务器下载二进制文件。

PHP

logged in successfully!";
          
        // Name or path of the localfile to
        // where the file to be downloaded
        $local_file = "local_file_shiva.jpg";
          
        // Name or path of the server file to
        // be downoaded
        $server_file = "shiva.jpg";
          
        // Downloading the specified server file
        if (ftp_get($ftp_connection, $local_file,
                $server_file, FTP_BINARY)) {
            echo "
Successfully downloaded from"                 . " $server_file to $local_file.";           }           else {             echo "
Error while downloading from"                 . " $server_file to $local_file.";           }                 }     else {         echo "
login failed!";     }            // echo ftp_get_option($ftp_connection, 1);     // Closing  connection         if(ftp_close($ftp_connection)) {         echo "
Connection closed Successfully!";     } } ?>

输出:

参考: https://www. PHP.net/manual/en/函数.ftp-get。 PHP