How to get the size of a php resource

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

  /** 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 the file size from the filename using file size ($ filename), but in this function I don't know / have the filename, justa resource

+3


source to share


3 answers


fstat () can do the job:



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

      

0


source


You can get the file size using the filesize () function.

Example:



header('Content-Length: '.filesize($fileName));

      

-2


source


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

php: // Link

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

      

-2


source







All Articles