Home  >  Article  >  Backend Development  >  Implementation code for php exporting and reading csv files

Implementation code for php exporting and reading csv files

WBOY
WBOYOriginal
2016-07-25 08:56:13914browse
  1. $users = file("./demoCSV.csv");
  2. foreach ($users as $user) {
  3. list($name, $email, $phone) = explode(" ,", $user);
  4. echo "

    $name ($email) Tel. $phone

    ";
  5. } //edit by bbs.it-home.org
  6. ?>
  7. < !--
  8. A,a@example.com,11111
  9. B,b@example.com,22222
  10. -->
Copy code

Example 2, fgetcsv reads a csv file

  1. $fh = fopen("./demoCSV.csv", "r");
  2. while (list($name, $email, $phone) = fgetcsv ($fh, 1024, ",")) {
  3. echo "

    $name ($email) Tel. $phone

    ";
  4. } //edit by bbs.it-home.org
  5. ? >

Copy code

Example 3, fopen reads a csv file

  1. $file = "./demoCSV.csv";
  2. $fh = fopen($file, "rt");
  3. $userdata = fread($fh, filesize($file));
  4. fclose($fh);

  5. echo $userdata;

  6. ?>

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