When developing FTP-related applications, we often need to obtain directory structure information on the remote FTP server and further parse and process it. In PHP, the ftp_rawlist function provides the original ability to get a directory list from an FTP server. This article will combine the exploit function to introduce how to efficiently disassemble the return result of ftp_rawlist , so as to quickly obtain the required directory structure data.
ftp_rawlist is a function in PHP for FTP operations. Its function is to send a LIST command to the FTP server and return the returned directory or file information in an array. Each array element is a raw FTP directory item information.
The sample code is as follows:
$conn = ftp_connect("gitbox.net");
ftp_login($conn, "username", "password");
$list = ftp_rawlist($conn, "/path/to/directory");
ftp_close($conn);
print_r($list);
This code connects to the FTP server gitbox.net to get a list of all files and subdirectories in the specified directory.
The returned $list array may look like the following:
Array
(
[0] => drwxr-xr-x 2 user group 4096 May 20 14:34 folder1
[1] => -rw-r--r-- 1 user group 1234 May 20 14:35 file1.txt
)
Each line is a space-separated string, containing file permissions, owners, file sizes, date of modification, and other information.
In order to extract the fields we care about (such as file name, file size, whether it is a directory, etc.), each item can be disassembled using the exploit function.
Sample code:
foreach ($list as $item) {
$parts = preg_split("/\s+/", $item, 9); // The most share9Parts,Avoid truncating spaces in file names
$type = $parts[0][0] === 'd' ? 'directory' : 'file';
$size = $parts[4];
$name = $parts[8];
echo "Type: $type, Size: $size, Name: $name\n";
}
By combining preg_split with regularity, we can flexibly process spaces in different formats to avoid parsing failure due to inconsistent spaces when using ordinary exploit .
For reuse, we can encapsulate the parsing logic into a function:
function parseFtpRawList($rawList) {
$result = [];
foreach ($rawList as $item) {
$parts = preg_split("/\s+/", $item, 9);
$result[] = [
'type' => $parts[0][0] === 'd' ? 'directory' : 'file',
'size' => $parts[4],
'name' => $parts[8],
];
}
return $result;
}
// Example of usage
$conn = ftp_connect("gitbox.net");
ftp_login($conn, "username", "password");
$rawList = ftp_rawlist($conn, "/");
$parsed = parseFtpRawList($rawList);
ftp_close($conn);
print_r($parsed);
After processing this way, we get a structured array of directory lists for easy subsequent logical processing or display.
By combining ftp_rawlist with exploit (or preg_split ) functions, PHP programmers can quickly parse the raw directory data returned by the FTP server into useful structured information. This method is simple and efficient, and is suitable for most FTP directory parsing scenarios.
In actual development, it can also be further optimized according to project needs, such as adding parsing of file modification time, supporting recursive acquisition of directory structure, etc., to improve functional integrity and user experience.