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


PHP ftp_put()用法及代碼示例

ftp_put()函數是PHP中的內置函數,用於將文件上傳到FTP服務器。

用法:

ftp_put( $ftp_connection, $remote_file_path, $local_file_path, $mode, $start_position );

參數:該函數接受上述和以下所述的五個參數:


  • $ftp_connection:它是必填參數。它指定用於上傳文件的現有FTP連接。
  • $remote_file_path:它是必填參數。它指定了遠程服務器(即文件上傳到的FTP服務器)中的路徑。
  • $local_file_path:它是必填參數。它指定要在FTP服務器中上載文件的路徑。
  • $mode:它是必填參數。它指定傳輸模式。參數的值為FTP_ASCII或FTP_BINARY。
  • $start_position:它是可選參數。它指定了遠程文件中開始上傳到的位置。

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

注意:

  • 此函數在PHP 4.0.0和更高版本上可用。
  • 以下示例無法在在線IDE上運行。因此,請嘗試使用適當的ftp服務器名稱在某些PHP托管服務器或localhost中運行。

以下示例說明了PHP中ftp_put()函數的用法:

範例1:

<?php 
   
// Connect to FTP server 
$ftp_server = "localhost"; 
   
// Use correct ftp username 
$ftp_username="user"; 
   
// Use correct ftp password corresponding 
// to the ftp username 
$ftp_userpass="user"; 
   
// File name or path to upload to ftp server 
$file = "filetoupload.txt"; 
    
// 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); 
       
    // Checking whether logged in successfully or not 
    if($login) { 
        echo "<br>logged in successfully!"; 
           
        if (ftp_put($ftp_connection, 
                "uploadedfile_name_in_server.txt", $file, FTP_ASCII)) 
        { 
          echo "<br>Uploaded successful $file."; 
        } 
        else { 
          echo "<br>Error while uploading $file."; 
        } 
            
    } 
    else { 
        echo "<br>login failed!"; 
    } 
   
    // Closeing the  connection 
    if(ftp_close($ftp_connection)) { 
        echo "<br>Connection closed Successfully!"; 
    }  
} 
?>

輸出:

Successfully connected to the ftp server!
logged in successfully!
Uploaded successful filetoupload.txt.
Connection closed Successfully!

範例2:使用端口號21連接到ftp服務器,然後上傳文件。

<?php 
  
// Connect to FTP server 
  
// Server name 
$ftp_server = "localhost"; 
  
// Use correct ftp username 
$ftp_username="user"; 
  
// Use correct ftp password corresponding 
// to the ftp username 
$ftp_userpass="user"; 
  
// File name or path to upload to ftp server 
$file = "filetoupload.txt"; 
   
// 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!"; 
          
        if (ftp_put($ftp_connection,  
            "uploadedfile_name_in_server.txt", $file, FTP_ASCII))  
        { 
          echo "<br>Uploaded successful $file."; 
        } 
        else { 
          echo "<br>Error while uploading $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!"; 
    }  
} 
  
?>

輸出:

Successfully connected to the ftp server!
logged in successfully!
Uploaded successful filetoupload.txt.
Connection closed Successfully!

參考: https://www.php.net/manual/en/function.ftp-put.php



相關用法


注:本文由純淨天空篩選整理自gekcho大神的英文原創作品 PHP | ftp_put() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。