note 95053 deleted from function.ftp-rawlist by danbrown
| From: | danbrown@php.net | Date: | Fri, 11 Dec 2009 16:22:22 +0000 |
| Subject: | note 95053 deleted from function.ftp-rawlist by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163865@lists.php.net to get a copy of this message | ||
Note Submitter: Matthew Potter
----
A quick modification to "tmp at gmx dot de":
I've made a class instead of going through and parsing the original large list, this retrieves
the files from each subfolder when constructed.
/********************
PHP Connection and call
*********************/
$ftp = ftp_login($ftpConnection, $serverInformation["USERNAME"],
$serverInformation["PASSWORD"]);
$rawFiles = ftp_rawlist($ftpConnection, $serverInformation["REMOTE_ROOT"], false);
$root = array();
foreach($rawFiles as $rawData){
$data = new _ftpItem($rawData, $ftpConnection, $serverInformation["REMOTE_ROOT"]);
$root[] = $data;
}
ftp_close($ftpConnection);
echo json_encode($root);
/********************
_ftpItem Class
*********************/
class _ftpItem {
var $name = "";
var $type = "folder";
var $size = "";
var $permissions = "";
var $dateModified = "";
var $parentFolder = "";
var $children = null;
function __get($attr){
if (isset($this->$attr)){return $this->$attr;}
else{return(NULL);}
}
function __set($attr, $value){ $this->$attr = $value; }
function __construct($rawData = false, $serverConnection, $rootFolder){if($rawData){
$info = preg_split("/[\s]+/", $rawData, 9);
$this->parentFolder = $rootFolder."/";
$this->name = $info[8];
$this->size = byteconvert($info[4]);
$this->permissions = chmodnum($info[0]);
$this->dateModified = strtotime($info[6] . ' ' . $info[5] . ' ' .
$info[7]);
if($info[0]{0} != 'd'){
$fileName = split("\.", $info[8]);
$this->type = $fileName[count($fileName)-1];
}
if($this->name == "." || $this->name == ".."){$this->type =
"controller";}
if($this->type == "folder"){
$rawFiles = ftp_rawlist($serverConnection, $this->parentFolder.$this->name, false);
foreach($rawFiles as $rawData){
$this->addChild($data = new _ftpItem($rawData, $serverConnection,
$this->parentFolder.$this->name));
}
}
return $data;
}}
function &addChild($child){
$this->children[] = $child;
}
}
/********************
Functions thanks to "tmp at gmx dot de"
*********************/
function byteconvert($bytes) {
if($bytes == 0){$bytes=1;}
$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]));
}