如何让一个PHP资源的大小

问题描述 投票:3回答:2

我有一个共同的PHP函数,accecpts资源,并将其下载为CSV文件,该resoure是一个文件或php://内存。如何找到这个资源的大小,这样我可以设置页眉内容长度

  /** 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;
  }

我可以看到如何从文件名,文件大小,文件大小使用($文件名),但在这个功能我不知道/有一个文件名,胡斯塔资源

php download
2个回答
3
投票

fstat()可以做的工作:

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

-3
投票

只需使用PHP://临时/或php://内存/

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.