note 95053 added to function.ftp-rawlist
| From: | MatthewPotter at osu1 dot php dot net | Date: | Fri, 11 Dec 2009 01:43:38 +0000 |
| Subject: | note 95053 added to function.ftp-rawlist | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163836@lists.php.net to get a copy of this message | ||
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]));
}
----
Server IP: 69.147.83.197
Probable Submitter: 173.177.97.59
----
Manual Page -- http://www.php.net/manual/en/function.ftp-rawlist.php
Edit -- https://master.php.net/note/edit/95053
Del: integrated -- https://master.php.net/note/delete/95053/integrated
Del: useless -- https://master.php.net/note/delete/95053/useless
Del: bad code -- https://master.php.net/note/delete/95053/bad+code
Del: spam -- https://master.php.net/note/delete/95053/spam
Del: non-english -- https://master.php.net/note/delete/95053/non-english
Del: in docs -- https://master.php.net/note/delete/95053/in+docs
Del: other reasons-- https://master.php.net/note/delete/95053
Reject -- https://master.php.net/note/reject/95053
Search -- https://master.php.net/manage/user-notes.php