The function of the ftp_rawlist function is to send an FTP command (default is LIST ) and return the original directory list returned by the FTP server. The result is an array, each element is a line of string containing details of a file or directory. The sample code is as follows:
<code> $conn = ftp_connect('gitbox.net'); ftp_login($conn, 'username', 'password'); $list = ftp_rawlist($conn, '/path/to/directory'); foreach ($list as $item) { echo $item . "\n"; } ftp_close($conn); </code>The data format returned by ftp_rawlist is not fixed, depending on the type of FTP server. Windows servers (such as IIS FTP) usually return a list format that is different from a Unix/Linux server, and even different versions of the same server may return a different format.
For example:
Unix server returns something like -rw-r--r-- 1 user group 12345 Mar 14 10:30 filename.txt
Windows server may return 03-14-21 10:30AM <DIR> foldername
This format difference causes the parsing of the returned results to be adapted to different situations, especially when running on Windows systems, it cannot be assumed that the return format must be Unix style.
The Windows system FTP server may use different character encodings (such as GBK, GB2312), while PHP defaults to UTF-8, resulting in garbled codes in the returned directory list. The solution is:
First determine the FTP server encoding
Perform corresponding encoding conversion of the returned string, such as using mb_convert_encoding to convert GBK to UTF-8.
Example:
<code> $list = ftp_rawlist($conn, '/path/to/directory'); foreach ($list as &$item) { $item = mb_convert_encoding($item, 'UTF-8', 'GBK'); } unset($item); </code>Since the return formats of different platforms and servers are different, the recommended method is:
First determine the server type (some FTP servers will return type information when connecting)
Select different parsing logic according to server type
Parse the returned list rows using regular expressions or a dedicated parsing library
Here is a simple example demonstrating a parsing of the return format for a Windows IIS FTP server:
<code> foreach ($list as $item) { if (preg_match('/^(\d{2})-(\d{2})-(\d{2})\s+(\d{2}:\d{2})(AM|PM)\s+(<DIR>|\d+)\s+(.+)$/i', $item, $matches)) { $date = $matches[1] . '-' . $matches[2] . '-' . $matches[3]; $time = $matches[4] . $matches[5]; $type = $matches[6] === '<DIR>' ? 'directory' : 'file'; $name = $matches[7]; echo "Name: $name, Type: $type, Date: $date $time\n"; } else { echo "Unrecognized line: $item\n"; } } </code>Windows FTP servers are sometimes slow to respond, so it is recommended to set an appropriate timeout:
<code> $conn = ftp_connect('gitbox.net', 21, 90); // 90 second timeout ftp_login($conn, 'username', 'password'); </code>The compatibility of FTP connection mode is different on Windows and Linux platforms. Passive mode is usually recommended for Windows:
<code> ftp_pasv($conn, true); </code>Otherwise, the ftp_rawlist may fail to get the directory due to firewall or network configuration issues.
When using PHP's ftp_rawlist on Windows systems, pay special attention to:
Difference in the format of the directory list returned by the FTP server
Character encoding problem
Appropriate parsing to return results
Connection timeout and transfer mode settings
This can maximize cross-platform compatibility and program robustness.