Home  >  Article  >  Backend Development  >  Sample code for php to read and download csv files

Sample code for php to read and download csv files

WBOY
WBOYOriginal
2016-07-25 08:55:25942browse
  1. $fileName = "prefs.csv";
  2. header('Content-Type: application/octet-stream');
  3. header('Content-Disposition: attachment; filename=' . $ fileName);
  4. header('Content-Transfer-Encoding: binary');
  5. header('Content-Length: ' . filesize($fileName));
  6. readfile($fileName);
Copy code

Example 2. Download the csv file in string form.

  1. $fileName = "pref_" . date("YmdHis") . ".csv";
  2. header('Content-Type: application/octet-stream');
  3. header(' Content-Disposition: attachment; filename=' . $fileName);
  4. echo $csv;
Copy code


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