note 124880 deleted from function.ftp-rawlist by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 19:11:42 +0000 |
| Subject: | note 124880 deleted from function.ftp-rawlist by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217893@lists.php.net to get a copy of this message | ||
Note Submitter: elamrani dot sv dot laza at gmail dot com
----
This function will give you directly the ftp file information whether if it a file or directory or a
link.
/**
* Retrieve file information from rawlist result function.
*
* @param $ftpStream
* @param $fileName
*
* @return array
*/
public static function extractRawList($ftpStream, $fileName)
{
$info = [];
# Check if the file is directory.
$isDir = ftp_size($ftpStream, $fileName) === -1 ? true : false;
# Throwing exception if rawlist fail to getting the file data.
# If the file is a directory we back to the previous folder and lopping over it to get the
dir info.
if (empty($list = ftp_rawlist($ftpStream, $isDir ? $fileName . '/..' :
$fileName)))
throw new \RuntimeException("Unreachable server content.");
# Extract date from rawlist result array.
foreach ($list as $item) {
# Clean the spaces.
$clean = preg_replace('/\s+/', ' ', $item);
# rawlist string to an array.
$split_string = explode(" ", $clean);
# file name.
$name = $split_string[count($split_string) - 1];
# Compare the file name from rawlist with the file name that we want information from.
if ($name === ($isDir ? pathinfo($fileName, PATHINFO_BASENAME) : $fileName))
{
$info['chmod'] = $split_string[0];
$info['num'] = $split_string[1];
$info['owner'] = $split_string[2];
$info['group'] = $split_string[3];
$info['size'] = $split_string[4];
$info['Mtime'] = sprintf("%s %s %s", $split_string[5],
$split_string[6], $split_string[7]);
$info['name'] = $split_string[8];
}
}
return $info;
}