如何获取php资源的大小

and*_*o42 4 php download

我有一个通用的 php 函数,可以接收资源并将其下载为 csv 文件,资源是文件或 php://memory。我如何找到这个资源的大小,以便我可以设置标题内容长度

  /** 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;
  }
Run Code Online (Sandbox Code Playgroud)

我可以看到如何从文件名中获取文件大小,使用 filesize($filename) 但在这个函数中我不知道/有一个文件名,只是一个资源

Sic*_*caa 5

fstat()可以完成这项工作:

$stat = fstat($fil);
header('Content-Length: '.$stat['size']);
Run Code Online (Sandbox Code Playgroud)