I need to display the file names in a webpage using PHP FTP, and the list has to be sorted by the timestamp. How can I do this?
You will need:
Please see my code example:
<?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>';
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments