如何获取php资源的大小

I have a common php function that accecpts a resource and downloads it as a csv file, the resoure is either a file or php://memory. How do I find the size of this resource so that I can set the header content length

  /** Download a file as csv and exit */
  function downloadCsv($filName, $fil) {
    header("Content-Type: application/csv");
    header("Content-Disposition: attachement; filename=$filName");
    // header("Content-Length: ".......
    fpassthru($fil);
    exit;
  }

I can see how to get a file size from the file name, using filesize($filename) but in this function I don't know/have a file name, justa resource

Just use the php://temp/ OR php://memory/

php:// Reference

/** Download a file as csv and exit */
function downloadCsv($filName, $fil) {
  header("Content-Type: application/csv");
  header("Content-Disposition: attachement; filename=$filName");

  //here the code
  $tmp_filename = 'php://temp/'.$filName;
  $fp = fopen($tmp_filename,'r+');
  fwrite($fp, $fil);
  fclose($fp);

  header("Content-Length: ".filesize($tmp_filename));  //use temp filename
  readfile($fil);
  exit;
}

fstat() could do the job:

$stat = fstat($fil);
header('Content-Length: '.$stat['size']);