In PHP development, especially when it comes to tasks that automate file management or remote server interaction, ftp_rawlist() is a very useful function. It allows developers to obtain the original list information of directories through the FTP protocol, which is very critical for fine-grained processing of files on FTP servers. This article will explore in-depth skills of using the ftp_rawlist() function, and combine practical cases to show its application in command-line scripts.
ftp_rawlist() is one of the functions provided by the PHP FTP extension, and the prototype is as follows:
array ftp_rawlist ( FTP\Connection $ftp_stream , string $directory [, bool $recursive = false ] )
This function returns the detailed information of all files and subdirectories in the specified directory, and the format is usually the same as the ls -l command. This means that you can extract permissions, file size, modification time, file name and other information from the returned string.
In CLI (command line interface) scripts, the role of ftp_rawlist() is particularly obvious, and the main reasons include:
Automatically process remote directory structure without GUI interaction
Suitable for periodic tasks such as checking the FTP directory via Cron timed
Supports custom logical parsing directory structure
<?php
$ftp = ftp_connect('ftp.gitbox.net');
ftp_login($ftp, 'username', 'password');
$rawList = ftp_rawlist($ftp, '/public_html/files');
$recentFiles = [];
$now = time();
foreach ($rawList as $line) {
$parts = preg_split("/\s+/", $line, 9);
if (count($parts) < 9 || $parts[0][0] === 'd') {
continue; // Ignore the directory
}
$month = $parts[5];
$day = $parts[6];
$timeOrYear = $parts[7];
$filename = $parts[8];
$filetime = strtotime("$month $day $timeOrYear");
if ($filetime !== false && ($now - $filetime) <= 7 * 24 * 60 * 60) {
$recentFiles[] = $filename;
}
}
print_r($recentFiles);
ftp_close($ftp);
This script will connect to the FTP server on ftp.gitbox.net , obtain all files in the /public_html/files directory, and filter out the file names modified in the last 7 days.
Recursive support is not a built-in feature of ftp_rawlist() , but we can implement it ourselves through recursive functions:
<?php
function listAllFiles($ftp, $dir = '.', $base = '') {
$files = [];
$list = ftp_rawlist($ftp, $dir);
foreach ($list as $item) {
$info = preg_split("/\s+/", $item, 9);
$name = $info[8];
$fullPath = "$dir/$name";
if ($info[0][0] === 'd') {
$files = array_merge($files, listAllFiles($ftp, $fullPath, "$base$name/"));
} else {
$files[] = "$base$name";
}
}
return $files;
}
$ftp = ftp_connect('ftp.gitbox.net');
ftp_login($ftp, 'username', 'password');
$allFiles = listAllFiles($ftp, '/public_html/downloads/');
foreach ($allFiles as $file) {
$local = __DIR__ . '/downloads/' . $file;
if (!is_dir(dirname($local))) {
mkdir(dirname($local), 0777, true);
}
ftp_get($ftp, $local, '/public_html/downloads/' . $file, FTP_BINARY);
echo "Downloaded: $file\n";
}
ftp_close($ftp);
This script can recursively obtain all file paths and download them from the /public_html/downloads/ directory of ftp.gitbox.net to the local directory, maintaining the original directory structure.
The parsing format is not fixed : the list formats returned by different FTP servers may differ slightly, and flexible regular or split logic should be used when parsing.
Connection and timeout : It is recommended to set reasonable timeout and retry logic to prevent scripts from getting stuck.
Permission check : Make sure the FTP account has permission to read directories and files.
Although the ftp_rawlist() function outputs the original string, through appropriate parsing and logical combination, it is possible to build a powerful command line tool for monitoring, backup and automated management of FTP directories. Whether in daily operation and maintenance or automation scripts, it is a powerful tool worth mastering.