title: How to get the user Group of logged in FTP user in php?
body: <p>I want to download some files from a FTP server using PHP. Now some files are not readable by the logged in user and I get this message:</p>
  
  <pre><code>ftp_get(): Failed to open file.
  </code></pre>
  
  <p>I know how to read the remote file permissions. Now I want to check if the currently logged in FTP user is the file owner and if he has the right to read that file. This is what I got so far (simplified):</p>
  
  <pre><code>&lt;?php
  
  $userName = 'vincent';
  
  $conn = ftp_connect($host, $port);
  ftp_login($conn, $userName, $password);
  
  // Get user info ($userGroup)
  // -&gt; How ???
  
  $list = ftp_rawlist($conn, $directory);
  
  foreach ($list as $file) {
  
      // -r----x--x 3 vincent vincent 4096 Jul 12 12:16 index.html
      $chunks = preg_split('/\s+/', $file);
      list($item['rights'], $item['number'], $item['user'], $item['group'], $item['size'], $item['month'], $item['day'], $item['time']) = $chunks;
      array_splice($chunks, 0, 8);
      $item['name'] = trim(implode(' ', $chunks));
  
      if ($item['type'] === 'f') {
  
          $mode    = substr($item['rights'], 1);
          $trans   = array(
              '-' =&gt; 0,
              'r' =&gt; 4,
              'w' =&gt; 2,
              'x' =&gt; 1
          );
          $mode       = strtr($mode, $trans);
          $newmode    = array(0, 0, 0);
          $newmode[0] = $mode[0] + $mode[1] + $mode[2];
          $newmode[1] = $mode[3] + $mode[4] + $mode[5];
          $newmode[2] = $mode[6] + $mode[7] + $mode[8];
  
          $canRead = false;
  
          // is logged in user equal file owner and has the owner read-rights?
          if ($userName === $item['user'] &amp;&amp; $newmode[0] &gt; 3) {
              $canRead = true;
          }
          // has the logged in user same group and has the file group-read-rights?
          else if ($userGroup === $item['group'] &amp;&amp; $newmode[1] &gt; 3) {
              $canRead = true;
          }
          // has the file public-read-rights?
          else if ($newmode[2] &gt; 3) {
              $canRead = true;
          }
  
          if ($canRead) {
              ftp_get($conn, 'xxx/' . $item['name'], $directory . '/' . $item['name'], FTP_BINARY);
          }
      }
  }
  </code></pre>