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


PHP ftp_mdtm()用法及代碼示例

ftp_mdtm()函數是PHP中的內置函數,用於獲取上次修改FTP服務器上文件的時間。

用法:

ftp_mdtm( $ftp_connection, $file )

參數:該函數接受上述和以下描述的兩個參數:


  • $ftp_connection:它是必填參數。它指定已經存在的FTP連接。
  • $file:它是必填參數。它指定遠程服務器(即將檢索其最後修改的FTP服務器)中的文件或文件的路徑。

返回值:如果成功,它將返回上次修改時間作為UNIX時間戳;如果錯誤,則返回-1。

注意:

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

例:

<?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"; 
  
// File name or path to upload to ftp server 
$file = "demo_test.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); 
      
    if($login) { 
          
        // Checking whether logged in successfully or not 
        echo "<br>logged in successfully!"; 
          
        // Storing last modified data in $last_mod 
        $last_mod = ftp_mdtm($ftp_connection, $file); 
          
        if ($last_mod != -1) { 
              
            // Checking whether any error occurred or not  
            // while retrieving last modified data 
            echo "<br> $file was modified on ".  
                    date("F d Y H:i:s.", $last_mod)."."; 
        } 
        else { 
            echo "<br>could not get last modified."; 
        } 
           
    } 
    else { 
        echo "<br>login failed!"; 
    } 
          
    // Closeing  connection 
    if(ftp_close($ftp_connection)) { 
        echo "<br>Connection closed Successfully!"; 
    }  
} 
?>

輸出:

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



相關用法


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