我需要使用PHP FTP在网页中显示文件名,并且列表必须按时间戳排序。我怎么能这样做?
答案 0 :(得分:1)
您将需要:
请参阅我的代码示例:
<?php
define('FTP_URL', '');
define('FTP_USERNAME', '');
define('FTP_PASSWORD', '');
define('FTP_DIRECTORY', '');
//Connect ot FTP
$ftp = ftp_connect(FTP_URL);
//Login to FTP
ftp_login($ftp, FTP_USERNAME, FTP_PASSWORD);
//Get files
$filesAndFolders = ftp_nlist($ftp, FTP_DIRECTORY);
$dates = array();
$files = array();
foreach ($filesAndFolders as $key => $file) {
//Get date
$date = ftp_mdtm($ftp , $file);
if ($date !== -1) {//Filter folders
$files[] = $file;
$dates[] = $date;
}
}
//Sort files by date
array_multisort($dates, SORT_DESC, $files);
//Render files list
echo '<ul>';
foreach ($files as $file) {
echo '<li>' . $file . '</li>';
}
echo '</ul>';