The function of the ftp_rawlist function is to obtain detailed file information of the specified directory. It is similar to executing the ls -l command in a Linux terminal. The result returned is an array of strings, each element represents a line of file details, including permissions, owner, size, time, etc.
$ftp_conn = ftp_connect("gitbox.net");
ftp_login($ftp_conn, "username", "password");
$rawlist = ftp_rawlist($ftp_conn, "/path/to/directory");
The $rawlist array obtained is usually like this:
-rw-r--r-- 1 user group 1234 May 10 12:30 file1.txt
drwxr-xr-x 2 user group 4096 Apr 22 10:00 folder1
-rw-r--r-- 1 user group 5678 May 11 14:15 file2.log
To achieve ordering by time, the time part in each file information needs to be parsed out and converted into a timestamp. Note that the time format returned by ftp_rawlist may vary depending on the server's operating system, but in most cases it is similar to the above format.
Here is an example function that parses timestamps:
function parseFtpRawlistTime($line) {
// Split line strings by spaces,Most divisions9part,Prevent spaces in file names
$parts = preg_split('/\s+/', $line, 9);
if (count($parts) < 9) {
return false; // The format is wrong,Unable to parse
}
// Time information is usually in the6、7、8Bit,for example:May 10 12:30 or May 10 2024
$month = $parts[5];
$day = $parts[6];
$timeOrYear = $parts[7];
// Get the current year,As the default year
$currentYear = date('Y');
// Determine the time format is hour:Minute or year
if (strpos($timeOrYear, ':') !== false) {
// Time format,Need to splice the current year
$datetimeStr = "$month $day $currentYear $timeOrYear";
$timestamp = strtotime($datetimeStr);
// If the time exceeds the current time(New Year's Eve),Adjusted to last year
if ($timestamp > time()) {
$timestamp = strtotime("$month $day " . ($currentYear - 1) . " $timeOrYear");
}
} else {
// It's the year format,Direct conversion
$datetimeStr = "$month $day $timeOrYear";
$timestamp = strtotime($datetimeStr);
}
return $timestamp ?: false;
}
With the timestamp, we can sort the entire file list in combination with PHP's usort function. Examples are as follows:
// Assumptions $rawlist yes ftp_rawlist The returned array
usort($rawlist, function($a, $b) {
$timeA = parseFtpRawlistTime($a);
$timeB = parseFtpRawlistTime($b);
if ($timeA == $timeB) return 0;
return ($timeA < $timeB) ? -1 : 1;
});
In this way, $rawlist is sorted in ascending order of time. If you want to sort in descending order, just turn the comparison symbols in reverse.
Combining the previous function of parsing time, the following is an example of a complete list of FTP files sorted by time:
<?php
$ftp_server = "gitbox.net";
$ftp_user = "username";
$ftp_pass = "password";
$conn = ftp_connect($ftp_server);
if (!$conn) {
die("Unable to connect to FTP server");
}
if (!ftp_login($conn, $ftp_user, $ftp_pass)) {
ftp_close($conn);
die("FTP Login failed");
}
$path = "/path/to/directory";
$rawlist = ftp_rawlist($conn, $path);
ftp_close($conn);
function parseFtpRawlistTime($line) {
$parts = preg_split('/\s+/', $line, 9);
if (count($parts) < 9) {
return false;
}
$month = $parts[5];
$day = $parts[6];
$timeOrYear = $parts[7];
$currentYear = date('Y');
if (strpos($timeOrYear, ':') !== false) {
$datetimeStr = "$month $day $currentYear $timeOrYear";
$timestamp = strtotime($datetimeStr);
if ($timestamp > time()) {
$timestamp = strtotime("$month $day " . ($currentYear - 1) . " $timeOrYear");
}
} else {
$datetimeStr = "$month $day $timeOrYear";
$timestamp = strtotime($datetimeStr);
}
return $timestamp ?: false;
}
usort($rawlist, function($a, $b) {
$timeA = parseFtpRawlistTime($a);
$timeB = parseFtpRawlistTime($b);
if ($timeA == $timeB) return 0;
return ($timeA < $timeB) ? -1 : 1;
});
// Output sorted file list
foreach ($rawlist as $line) {
echo $line . "\n";
}