當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。