How PHP exports a CSV file (code sample)

Source: Internet
Author: User
Tags explode throw exception

This article is about how PHP export CSV file (code example), there is a certain reference value, the need for friends can refer to, I hope to help you.

/** If a large number of data exports support paging write * [downloaddate public export CSV] * @param string $name [file name] * @param array $header [Header] * @param array $data [data set] * @param $is _header [true or False table Header resolution Loop write problem] * @return array name[file name] filepath[file path] */public static functio      N Downloaddata ($name = ", $header =array (), $data =array (), $is _header=true) {set_time_limit (0);       Ini_set (' Memory_limit ', ' 2048M ');      Header ("Content-type:application/vnd.ms-excel");      Header ("Content-disposition:filename=downloaded.pdf");      try {if (! $name | |! $data) {throw new badrequesthttpexception (' parameter not nullable ');      } $filePath = "./temp/download/{$name}.csv";      $header = Implode (",", $header);      $header = Iconv (' UTF-8 ', ' Gbk//ignore ', $header);      $header = Explode (",", $header);         $fp = fopen ($filePath, ' A + '); if (!empty ($header) && Is_array ($header) && $is _heAder) {fputcsv ($fp, $header);      } foreach ($data as $row) {$str = Implode ("", $row);      $str = Iconv (' UTF-8 ', ' Gbk//ignore ', $str);      $str = Str_replace (",", "|", $STR);      $row = Explode ("", $str);      Fputcsv ($fp, $row);        } unset ($data);      if (Ob_get_level () >0) {Ob_flush ();       } flush (); } catch (Exception $e) {throw new Badrequesthttpexception ($e->getmessage ());//Throw exception} return [' Fi Lepath ' =>ltrim ($filePath, "./"), ' name ' and ' = ' $name. CSV ',]; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.