Created
February 26, 2020 13:19
-
-
Save ameenross/f5189a11bd31146ec477f4ff1fa9fa85 to your computer and use it in GitHub Desktop.
ftp_rawlist result parse function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param string $row | |
* @return array | |
* An assoc with the following properties: | |
* - name | |
* - type: one of directory, file, link, unknown | |
* - size | |
* - owner | |
* - group | |
* - mask: file permission mask as an octal string | |
* - modified: DateTime object | |
*/ | |
function parseFileListRow($row) | |
{ | |
preg_match('/^(.)(.{9})\s+\S+\s+(\S+)\s+(\S+)\s+(\S+)\s+(.{12})\s(.+)$/', $row, $matches); | |
// The date can be in 2 formats. Month-date hour:minute or Month-date year. | |
// The dates should always be in the past, but PHP does not do that by | |
// default for relative dates (without year), so subtract a year if | |
// necessary. | |
$date = new DateTime($matches[6]); | |
if (preg_match('/\d\d:\d\d$/', $matches[6]) && $date > new DateTime) { | |
$date->modify('-1 year'); | |
} | |
return [ | |
'name' => $matches[7], | |
'type' => [ | |
'd' => 'directory', | |
'l' => 'link', | |
'-' => 'file', | |
][$matches[1]] ?? 'unknown', | |
'size' => $matches[5], | |
'owner' => $matches[3], | |
'group' => $matches[4], | |
'mask' => base_convert(strtr($matches[2], 'rwx-', '1110'), 2, 8), | |
'modified' => $date, | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment