title: Php check size of remote folder
body: <p>I have a user folder on remote server (other than page files). I need check a size of whole "example" folder, not one file. I think i should do it with use a ftp, but I can't.</p>
  
  <p>I have something like this but not working:</p>
  
  <pre><code>function dirFTPSize($ftpStream, $dir) {
  $size = 0;
  $files = ftp_nlist($ftpStream, $dir);
  
  foreach ($files as $remoteFile) {
      if(preg_match('/.*\/\.\.$/', $remoteFile) || preg_match('/.*\/\.$/', $remoteFile)){
          continue;
      }
      $sizeTemp = ftp_size($ftpStream, $remoteFile);
      if ($sizeTemp &gt; 0) {
          $size += $sizeTemp;
      }elseif($sizeTemp == -1){//directorio
          $size += dirFTPSize($ftpStream, $remoteFile);
      }
  }
  
  return $size;
  }
  
  $hostname = '127.0.0.1';
  $username = 'username';
  $password = 'password';
  $startdir = '/public_html'; // absolute path
  $files = array();
  $ftpStream = ftp_connect($hostname);
  $login = ftp_login($ftpStream, $username, $password);
  if (!$ftpStream) {
  echo 'Wrong server!';
  exit;
  } else if (!$login) {
  echo 'Wrong username/password!';
  exit;
  } else {
  $size = dirFTPSize($ftpStream, $startdir);
  }
  echo number_format(($size / 1024 / 1024), 2, '.', '') . ' MB';
  ftp_close($ftpStream);
  </code></pre>
  
  <p>Whole time script displays 0.00 MB, what can I do to fix it?</p>