当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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上运行。因此,请尝试使用适当的ftp服务器名称在某些PHP托管服务器或localhost中运行。
  • 模式应正确选择,即FTP_ASCII或FTP_BINARY

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

以下示例说明了PHP中的ftp_get()函数:

范例1:

<?php 
  
// Connect to FTP server 
  
// Use a correct ftp server 
$ftp_server = "localhost"; 
  
// Use correct ftp username 
$ftp_username="user"; 
  
// Use correct ftp password corresponding 
// to the ftp username 
$ftp_userpass="user"; 
   
// Establishing ftp connection  
$ftp_connection = ftp_connect($ftp_server)  
    or die("Could not connect to $ftp_server"); 
  
if( $ftp_connection ) { 
    echo "successfully connected to the ftp server!"; 
      
    // Logging in to established connection 
    // with ftp username password 
    $login = ftp_login($ftp_connection, 
            $ftp_username, $ftp_userpass); 
      
    if($login) { 
          
        // Checking whether logged in successfully 
        // or not 
        echo "<br>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 "<br>Successfully downloaded "
               . "from $server_file to $local_file."; 
          } 
          else { 
            echo "<br>Error while downloading from "
                  . "$server_file to $local_file."; 
          } 
           
    } 
    else { 
        echo "<br>login failed!"; 
    } 
      
    // echo ftp_get_option($ftp_connection, 1); 
    // Closeing  connection 
   
    if(ftp_close($ftp_connection)) { 
        echo "<br>Connection closed Successfully!"; 
    }  
} 
?>

输出:

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

<?php 
   
// Connect to FTP server 
   
// Use a correct ftp server 
$ftp_server = "localhost"; 
   
// Use correct ftp username 
$ftp_username="user"; 
   
// Use correct ftp password corresponding 
// to the ftp username 
$ftp_userpass="user"; 
    
// Establishing ftp connection  
$ftp_connection = ftp_connect($ftp_server, 21)  
    or die("Could not connect to $ftp_server"); 
   
if( $ftp_connection ) { 
    echo "successfully connected to the ftp server!"; 
       
    // Logging in to established connection 
    // with ftp username password 
    $login = ftp_login($ftp_connection, 
            $ftp_username, $ftp_userpass); 
       
    if($login) { 
           
        // Checking whether logged in successfully 
        // or not 
        echo "<br>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 "<br>Successfully downloaded from"
                . " $server_file to $local_file."; 
          } 
          else { 
            echo "<br>Error while downloading from"
                . " $server_file to $local_file."; 
          } 
            
    } 
    else { 
        echo "<br>login failed!"; 
    } 
       
    // echo ftp_get_option($ftp_connection, 1); 
    // Closeing  connection 
    
    if(ftp_close($ftp_connection)) { 
        echo "<br>Connection closed Successfully!"; 
    }  
} 
?>

输出:

参考: https://www.php.net/manual/en/function.ftp-get.php



相关用法


注:本文由纯净天空筛选整理自gekcho大神的英文原创作品 PHP | ftp_get() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。