note 91120 added to function.ftp-rawlist
| From: | tmp at gmx dot de | Date: | Tue, 26 May 2009 11:20:09 +0000 |
| Subject: | note 91120 added to function.ftp-rawlist | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156017@lists.php.net to get a copy of this message | ||
To format the _recrusive_ result of this function I use this:
<?php
$conn_id = ftp_connect(FTP_SERVER);
$login_result = ftp_login($conn_id, FTP_USR, FTP_PASS);
$rawfiles = ftp_rawlist($conn_id, '/', true);
ftp_close($conn_id);
// here the magic begins!
$structure = array();
$arraypointer = &$structure;
foreach ($rawfiles as $rawfile) {
if ($rawfile[0] == '/') {
$paths = array_slice(explode('/', str_replace(':', '',
$rawfile)), 1);
$arraypointer = &$structure;
foreach ($paths as $path) {
foreach ($arraypointer as $i => $file) {
if ($file['text'] == $path) {
$arraypointer = &$arraypointer[ $i ]['children'];
break;
}
}
}
} elseif(!empty($rawfile)) {
$info = preg_split("/[\s]+/", $rawfile, 9);
$arraypointer[] = array(
'text' => $info[8],
'isDir' => $info[0]{0} == 'd',
'size' => byteconvert($info[4]),
'chmod' => chmodnum($info[0]),
'date' => strtotime($info[6] . ' ' . $info[5] . ' ' .
$info[7]),
'raw' => $info
// the 'children' attribut is automatically added if the folder contains at
least one file
);
}
}
// in $structure is all the data
print_r($structure);
// little helper functions
function byteconvert($bytes) {
$symbol = array('B', 'KB', 'MB', 'GB', 'TB',
'PB', 'EB', 'ZB', 'YB');
$exp = floor( log($bytes) / log(1024) );
return sprintf( '%.2f ' . $symbol[ $exp ], ($bytes / pow(1024, floor($exp))) );
}
function chmodnum($chmod) {
$trans = array('-' => '0', 'r' => '4', 'w'
=> '2', 'x' => '1');
$chmod = substr(strtr($chmod, $trans), 1);
$array = str_split($chmod, 3);
return array_sum(str_split($array[0])) . array_sum(str_split($array[1])) .
array_sum(str_split($array[2]));
}
?>
----
Server IP: 216.92.131.65
Probable Submitter: 193.174.59.117
----
Manual Page -- http://www.php.net/manual/en/function.ftp-rawlist.php
Edit -- https://master.php.net/note/edit/91120
Del: integrated -- https://master.php.net/note/delete/91120/integrated
Del: useless -- https://master.php.net/note/delete/91120/useless
Del: bad code -- https://master.php.net/note/delete/91120/bad+code
Del: spam -- https://master.php.net/note/delete/91120/spam
Del: non-english -- https://master.php.net/note/delete/91120/non-english
Del: in docs -- https://master.php.net/note/delete/91120/in+docs
Del: other reasons-- https://master.php.net/note/delete/91120
Reject -- https://master.php.net/note/reject/91120
Search -- https://master.php.net/manage/user-notes.php