Using FTP and PHP to get an array of file details such as size and permissions.
This uses the ftp_rawlist() method and splits the string into an array for each item in the directory, this includes files and folders.
This method also does just one FTP call per directory, instead of having to call ftp_size() on each file in the directory.
if (is_array($children = @ftp_rawlist($conn, "."))) { $items = []; foreach ($children as $child) { $split = preg_split("/\s+/", $child); [$item['rights'], $item['number'], $item['user'], $item['group'], $item['size'], $item['month'], $item['day'], $item['time'], $item['name']] = $split; $item['type'] = $split[0][0] === 'd' ? 'directory' : 'file'; $items[] = $item; } }
A sample output of $items
[ { "rights": "-rw-r--r--", "number": "1", "user": "ftp", "group": "ftp", "size": "83886104", "month": "July", "day": "26", "time": "2023", "name": "sample.txt", "type": "file" }, { "rights": "-rw-r--r--", "number": "1", "user": "ftp", "group": "ftp", "size": "1821", "month": "July", "day": "26", "time": "2023", "name": "other.txt", "type": "file" } ]