Home  >  Article  >  Backend Development  >  Summary of php outputting non-html format files

Summary of php outputting non-html format files

WBOY
WBOYOriginal
2016-07-25 09:05:311289browse
  1. $file = 'a.pdf';
  2. if (file_exists($file)) {
  3. header('Content-Description: File Transfer');
  4. header('Content-Type : application/octet-stream');
  5. header('Content-Disposition: attachment; filename='.basename($file));
  6. header('Content-Transfer-Encoding: binary');
  7. header('Expires: 0');
  8. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  9. header('Pragma: public');
  10. b_clean();
  11. flush();
  12. readfile($file);
  13. exit;
  14. }
  15. ?>
Copy code

2. Output the generated file (such as: csv pdf, etc.) Sometimes the system will output the generated files, mainly generating csv, pdf, or packaging multiple files for download in zip format. For this part, some implementation methods are to output the generated files into files and then download them through files, and finally delete them. To generate a file, you can actually directly output the generated file through php://output. The following uses csv output as an example.

  1. header('Content-Description: File Transfer');
  2. header('Content-Type: application/octet-stream');
  3. header('Content-Disposition: attachment; filename =a.csv');
  4. header('Content-Transfer-Encoding: binary');
  5. header('Expires: 0');
  6. header('Cache-Control: must-revalidate, post-check=0, pre -check=0');
  7. header('Pragma: public');
  8. ob_clean();
  9. flush();
  10. $rowarr=array(array('1','2','3'),array( '1','2','3'));
  11. $fp=fopen('php://output', 'w');
  12. foreach($rowarr as $row){
  13. fputcsv($fp, $ row);
  14. }
  15. fclose($fp);
  16. exit;
  17. ?>
Copy code

3. Get the content of the generated file, process it and output it To obtain the content of a generated file, you usually generate the file first, then read it, and finally delete it. In fact, you can use php://temp to do this. The following is still using csv as an example.

  1. header('Content-Description: File Transfer');
  2. header('Content-Type: application/octet-stream');
  3. header('Content-Disposition: attachment; filename =a.csv');
  4. header('Content-Transfer-Encoding: binary');
  5. header('Expires: 0');
  6. header('Cache-Control: must-revalidate, post-check=0, pre -check=0');
  7. header('Pragma: public');
  8. ob_clean();
  9. flush();
  10. $rowarr=array(array('1','2','Chinese'),array( '1','2','3'));
  11. $fp=fopen('php://temp', 'r+');
  12. foreach($rowarr as $row){
  13. fputcsv($fp, $ row);
  14. }
  15. rewind($fp);
  16. $filecontent=stream_get_contents($fp);
  17. fclose($fp);
  18. //Process $filecontent content
  19. $filecontent=iconv('UTF-8','GBK ',$filecontent);
  20. echo $filecontent; //Output
  21. exit;
  22. ?>
Copy code

The input/output streams function in php is very powerful. If used well, it can simplify coding and improve Efficiency, I suggest you focus on it.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn