note 101071 modified in function.ftp-rawlist by danbrown

From: Date: Wed, 24 Nov 2010 15:46:33 +0000
Subject: note 101071 modified in function.ftp-rawlist by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-174327@lists.php.net to get a copy of this message
In case anybody wants to get a detailed listing using the MLSD command over a passive connection, the following function might be helpful as a starting point for your own implementation: <?php function ftp_mlsd($ftp, $directory) { $ok = @ftp_chdir($ftp, $directory); if (!$ok) { return false; } $ret = ftp_raw($ftp, 'PASV'); if (preg_match( '/^227.*\(([0-9]+,[0-9]+,[0-9]+,[0-9]+),([0-9]+),([0-9]+)\)$/', $ret[0], $matches)) { $controlIP = str_replace(',', '.', $matches[1]); $controlPort = intval($matches[2])*256+intval($matches[3]); $socket = fsockopen($controlIP, $controlPort); ftp_raw($ftp, 'MLSD'); $s = ''; while (!feof($socket)) { $s .= fread($socket, 4096); } fclose($socket); $files = array(); foreach (explode("\n", $s) as $line) { if (!$line) { continue; } $file = array(); foreach (explode(';', $line) as $property) { list($key, $value) = explode('=', $property); if ($value) { $file[$key] = $value; } else { $filename = trim($key); } } $files[$filename] = $file; } return $files; } return false; } ?> Please note that this function ignores the setting of ftp_pasv(). Making the function to work universally for both active and passive connections is left as an exercise to the reader ;-) --was-- In case anybody wants to get a detailed listing using the MLSD command over a passive connection, the following function might be helpful as a starting point for your own implementation: function ftp_mlsd($ftp, $directory) { $ok = @ftp_chdir($ftp, $directory); if (!$ok) { return false; } $ret = ftp_raw($ftp, 'PASV'); if (preg_match( '/^227.*\(([0-9]+,[0-9]+,[0-9]+,[0-9]+),([0-9]+),([0-9]+)\)$/', $ret[0], $matches)) { $controlIP = str_replace(',', '.', $matches[1]); $controlPort = intval($matches[2])*256+intval($matches[3]); $socket = fsockopen($controlIP, $controlPort); ftp_raw($ftp, 'MLSD'); $s = ''; while (!feof($socket)) { $s .= fread($socket, 4096); } fclose($socket); $files = array(); foreach (explode("\n", $s) as $line) { if (!$line) { continue; } $file = array(); foreach (explode(';', $line) as $property) { list($key, $value) = explode('=', $property); if ($value) { $file[$key] = $value; } else { $filename = trim($key); } } $files[$filename] = $file; } return $files; } return false; } Please note that this function ignores the setting of ftp_pasv(). Making the function to work universally for both active and passive connections is left as an exercise to the reader ;-) http://php.net/manual/en/function.ftp-rawlist.php

« previous php.notes (#174327) next »