簡體   English   中英

PHP:通過FTP打開並顯示文件夾中的最新文件

[英]PHP: Open and show the most recent file from a folder via FTP

我想通過PHP連接到FTP服務器,並從某個目錄中獲取最新文件,並將其顯示在該PHP文件中。

因此,我可以轉到www.domain.com/file.php並查看該文件中的內容。 這些文件具有以下名稱“ Filename_20150721-085620_138.csv” ,因此第二個值20150721是實際日期。 這些文件也只包含CSV文本。

有什么辦法可以做到這一點?

歡迎來到Stackoverflow! 考慮以下代碼和說明:

// connect
$conn = ftp_connect('ftp.addr.com');
ftp_login($conn, 'user', 'pass');

// get list of files on given path
$files = ftp_nlist($conn, '');

$newestfile = null;
$time = 0;
foreach ($files as $file) {
    $tmp = explode("_", $file);     // Filename_20150721-085620_138.csv => $tmp[1] has the date in question

    $year = substr($tmp[1], 0, 4);  // 2015
    $month = substr($tmp[1], 4, 2); // 07
    $day = substr($tmp[1], 6, 2);   // 21

    $current = strtotime("$month/$day/$year"); // makes a timestamp from a string
    if ($current >= $time) { // that is newer
        $time = $current;
        $newestfile = $file;
    }
}

ftp_close($conn);

之后,您的$newestfile將保存最新的文件名。 這是你所追求的嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM