When operating remote servers through the FTP protocol using PHP, ftp_rawlist() is a common function that can return output similar to the ls -l command of Unix system. Although these raw outputs provide complete file list information, since their format is a plain text string, some additional processing is required to extract specific information such as file size and modification time from it.
This article will introduce how to extract the size and last modification time of each file by parsing the output of ftp_rawlist() , and it comes with sample code.
ftp_rawlist() returns an array of strings, each element represents information about a file or directory, in the format as follows:
-rw-r--r-- 1 user group 1048576 May 21 13:37 filename.zip
drwxr-xr-x 2 user group 512 May 20 10:15 subdir
Each line is a space-separated string containing the file type, permissions, number of links, owner, group, size, date and time of modification, and file name.
To extract file size and modification time, you can parse these fields with regular expressions.
<?php
// connect FTP
$ftp = ftp_connect("gitbox.net");
ftp_login($ftp, "username", "password");
// Get the directory list
$rawList = ftp_rawlist($ftp, ".");
// Iterate over and parse each line
foreach ($rawList as $line) {
if (preg_match('/^([\-ld])([rwx\-]{9})\s+\d+\s+\S+\s+\S+\s+(\d+)\s+([A-Za-z]{3})\s+(\d{1,2})\s+(\d{2}:\d{2}|\d{4})\s+(.*)$/', $line, $matches)) {
$type = $matches[1];
$size = $matches[3];
$month = $matches[4];
$day = $matches[5];
$timeOrYear = $matches[6];
$name = $matches[7];
// Convert modified time to timestamp
$currentYear = date("Y");
$fileTimeStr = "$month $day $timeOrYear";
if (strpos($timeOrYear, ':') !== false) {
// It means the time of the day,The year is the current year
$fileTimeStr .= " $currentYear";
$timestamp = strtotime($fileTimeStr);
} else {
// Time is year
$timestamp = strtotime("$month $day $timeOrYear");
}
echo "document: $name\n";
echo "type: " . ($type === 'd' ? 'Table of contents' : 'document') . "\n";
echo "size: $size byte\n";
echo "Modification time: " . date("Y-m-d H:i:s", $timestamp) . "\n";
echo "-----------------------------\n";
}
}
ftp_close($ftp);
?>
The format of ftp_rawlist() depends on the operating system type of the remote server, and the above rules are targeted at Unix-style list results. If the server uses Windows FTP, the output format will be completely different and needs to be adjusted according to the actual situation.
It is recommended to always perform regular matching error handling during the parsing process to prevent problems caused by inconsistent formats.
For servers across time zones, the result of the strtotime() conversion may have errors. It is recommended to use the DateTime class and time zone objects to process the time more accurately.
Through the above method, you can easily extract the file size and modification time from the results of ftp_rawlist() and use it for file synchronization, log analysis and other scenarios.