I am working on something which uses FTP but I need to get the existing chmod permissions of each file. How can I achieve this?
I have tried the fileperms()
function, but that seems to only work with local files.
I am working on something which uses FTP but I need to get the existing chmod permissions of each file. How can I achieve this?
I have tried the fileperms()
function, but that seems to only work with local files.
If you are using PHP 7.2 and newer and your FTP server supports MLSD
command, it's easy, as you can use ftp_mlsd
function.
$conn_id = ftp_connect("ftp.example.com") or die("Cannot connect");
ftp_login($conn_id, "username", "password") or die("Cannot login");
ftp_pasv($conn_id, true) or die("Cannot change to passive mode");
$entries = ftp_mlsd($conn_id, "/remote/path") or die("Cannot list directory");
foreach ($entries as $entry)
{
if (($entry["type"] != "cdir") && ($entry["type"] != "pdir"))
{
echo $entry["name"] . " - " . $entry["UNIX.mode"] . "
";
}
}
If not, you have to use LIST
command using ftp_rawlist
function and parse a proprietary format that the server returns.
The following code assumes a common *nix format.
$entries = ftp_rawlist($conn_id, "/remote/path") or die("Cannot list directory");
foreach ($entries as $entry)
{
$tokens = explode(" ", $entry);
$name = $tokens[count($tokens) - 1];
$permissions = $tokens[0];
echo $name . " - " . $permissions . "
";
}