fputcsv
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
fputcsv — 行を CSV 形式にフォーマットし、ファイルポインタに書き込む
説明
resource
$stream
,array
$fields
,string
$separator
= ",",string
$enclosure
= "\"",string
$escape
= "\\",string
$eol
= "\n"): int|false
fputcsv() は、行(fields
配列として渡されたもの)を CSV としてフォーマットし、それを
stream
で指定したファイルに書き込みます
(いちばん最後に改行を追加します)。
パラメータ
stream
-
ファイルポインタは、有効なファイルポインタである必要があり、 fopen() または fsockopen() で正常にオープンされた (そしてまだ fclose() でクローズされていない) ファイルを指している必要があります。
fields
-
文字列の配列。
separator
-
オプションの
separator
はフィールド区切り文字 (シングルバイト文字 一文字だけ) を指定します。 enclosure
-
オプションの
enclosure
はフィールドを囲む文字 (シングルバイト文字 一文字だけ) を指定します。 escape
-
オプションの
escape
は、エスケープ文字 (シングルバイト文字 最大で一文字) を指定します。 空文字(""
) を指定すると、(RFC 4180 に準拠していない) 独自仕様のエスケープ機構が無効になります。 eol
-
eol
は、 カスタムの行末シーケンスを設定するオプションの引数です。
escape
が空の文字列(""
)以外に設定されているとき、
» RFC 4180
に準拠しない CSV が生成されたり、PHP の CSV
関数を介してラウンドトリップ(往復変換)でデータが壊れる可能性があります。
escape
のデフォルト値は"\\"
なので、明示的に空の文字列を指定することを推奨します。デフォルト値は、PHP 9.0
以降の将来のバージョンで変更予定です。
注意:
enclosure
がフィールド内に含まれる場合は、同じ文字を二度続けることでエスケープします。 ただし、その直前にescape
がある場合は別です。
戻り値
書き込んだ文字列の長さを返します。失敗した場合に false
を返します。
変更履歴
バージョン | 説明 |
---|---|
8.1.0 |
オプションの引数 eol が追加されました。
|
7.4.0 |
escape パラメータが、
(RFC 4180 に準拠していない) 独自仕様のエスケープ機構を無効にするため、
空文字列を受け入れるようになりました。
|
例
例1 fputcsv() の例
<?php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
上の例が書き出す file.csv
の内容は、このようになります。
aaa,bbb,ccc,dddd 123,456,789 """aaa""","""bbb"""
注意
注意: マッキントッシュコンピュータ上で作成されたファイルを読み込む際に、
PHP
が行末を認識できないという問題が発生した場合、 実行時の設定オプションauto_detect_line_endings を有効にする必要が生じるかもしれません。
User Contributed Notes 25 notes
If you need to send a CSV file directly to the browser, without writing in an external file, you can open the output and use fputcsv on it..
<?php
$out = fopen('php://output', 'w');
fputcsv($out, array('this','is some', 'csv "stuff", you know.'));
fclose($out);
?>
If you need to save the output to a variable (e.g. for use within a framework) you can write to a temporary memory-wrapper and retrieve it's contents:
<?php
// output up to 5MB is kept in memory, if it becomes bigger it will automatically be written to a temporary file
$csv = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');
fputcsv($csv, array('blah','blah'));
rewind($csv);
// put it all in a variable
$output = stream_get_contents($csv);
?>
Sometimes it's useful to get CSV line as string. I.e. to store it somewhere, not in on a filesystem.
<?php
function csvstr(array $fields) : string
{
$f = fopen('php://memory', 'r+');
if (fputcsv($f, $fields) === false) {
return false;
}
rewind($f);
$csv_line = stream_get_contents($f);
return rtrim($csv_line);
}
?>
if you want make UTF-8 file for excel, use this:
$fp = fopen($filename, 'w');
//add BOM to fix UTF-8 in Excel
fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
Please note, that fputcsv ist not always enclosing strings with the enclosure character.
<?php
$fh = fopen('file.csv', 'w');
$a = [ 'One 1', 'Two', 'Three 3' ];
fputcsv($fh, $a, "\t");
fclose($fh);
?>
results in a file containing the line:
"One 1" Two "Three 3"
It seems that only strings containing at least one of the following characters are enclosed:
- the delimiter character
- the enclosure character
- the escape character
- \n (new line)
- \r (line feed)
- \t (tab)
- blank
I hope this saves you the hour it took me to get to the bottom of this behaviour.
For maximum compatibility with standard (RFC-4180) CSV files, remember that the proprietary-escape mechanism should be disabled. i.e. set the optional 5th parameter to "" (the empty string).
In all those cases you need a bug free csv writer with custom record seperating capability:
<?php
/**
* Custom fputcsv
* @param int $handle filehandle
* @param mixed[] $fields array of values to write
* @param string $delimiter field delimiter
* @param string $enclosure field enclosures
* @param string $escape_char escape enclosure chars in fields
* @param string $record_seperator
* @return int characters written
*/
function _fputcsv($handle, $fields, $delimiter = ",", $enclosure = '"', $escape_char = "\\", $record_seperator = "\r\n")
{
$result = [];
foreach ($fields as $field) {
$result[] = $enclosure . str_replace($enclosure, $escape_char . $enclosure, $field) . $enclosure;
}
return fwrite($handle, implode($delimiter, $result) . $record_seperator);
}
?>
TAB delimiting.
Using fputcsv to output a CSV with a tab delimiter is a little tricky since the delimiter field only takes one character.
The answer is to use the chr() function. The ascii code for tab is 9, so chr(9) returns a tab character.
<?php
fputcsv($fp, $foo, '\t'); //won't work
fputcsv($fp, $foo, ' '); //won't work
fputcsv($fp, $foo, chr(9)); //works
?>
==================
it should be:
<?php
fputcsv($fp, $foo, "\t");
?>
you just forgot that single quotes are literal...meaning whatever you put there that's what will come out so '\t' would be same as 't' because \ in that case would be only used for escaping but if you use double quotes then that would work.
the solution for how to solve the encoding problem while converting an array to csv file is below.
$fp = fopen('php://memory', 'w');
//add BOM to fix UTF-8 in Excel
fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
// output the column headings
//fputcsv($fp, array('Topic', 'Title', 'URL', 'Keywords', 'Score', 'FB_count', 'TW_count', '|'));
if(isset($trend)){
foreach ( $trend as $myField ){
fputcsv($fp, $myField, '|');
}
}
Utility function to output a mysql query to csv with the option to write to file or send back to the browser as a csv attachment.
<?php
function query_to_csv($db_conn, $query, $filename, $attachment = false, $headers = true) {
if($attachment) {
// send response headers to the browser
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);
$fp = fopen('php://output', 'w');
} else {
$fp = fopen($filename, 'w');
}
$result = mysql_query($query, $db_conn) or die( mysql_error( $db_conn ) );
if($headers) {
// output header row (if at least one row exists)
$row = mysql_fetch_assoc($result);
if($row) {
fputcsv($fp, array_keys($row));
// reset pointer back to beginning
mysql_data_seek($result, 0);
}
}
while($row = mysql_fetch_assoc($result)) {
fputcsv($fp, $row);
}
fclose($fp);
}
// Using the function
$sql = "SELECT * FROM table";
// $db_conn should be a valid db handle
// output as an attachment
query_to_csv($db_conn, $sql, "test.csv", true);
// output to file system
query_to_csv($db_conn, $sql, "test.csv", false);
?>
I've created a function for quickly generating CSV files that work with Microsoft applications. In the field I learned a few things about generating CSVs that are not always obvious. First, since PHP is generally *nix-based, it makes sense that the line endings are always \n instead of \r\n. However, certain Microsoft programs (I'm looking at you, Access 97), will fail to recognize the CSV properly unless each line ends with \r\n. So this function changes the line endings accordingly. Secondly, if the first column heading / value of the CSV file begins with uppercase ID, certain Microsoft programs (ahem, Excel 2007) will interpret the file as being in the SYLK format rather than CSV, as described here: http://support.microsoft.com/kb/323626
This function accommodates for that as well, by forcibly enclosing that first value in quotes (when this doesn't occur automatically). It would be fairly simple to modify this function to use another delimiter if need be and I leave that as an exercise to the reader. So quite simply, this function is used for outputting CSV data to a CSV file in a way that is safe for use with Windows applications. It takes two parameters + one optional parameter: the location of where the file should be saved, an array of data rows, and an optional array of column headings. (Technically you could omit the headings array and just include it as the first row of the data, but it is often useful to keep this data stored in different arrays in practice.)
<?php
function mssafe_csv($filepath, $data, $header = array())
{
if ( $fp = fopen($filepath, 'w') ) {
$show_header = true;
if ( empty($header) ) {
$show_header = false;
reset($data);
$line = current($data);
if ( !empty($line) ) {
reset($line);
$first = current($line);
if ( substr($first, 0, 2) == 'ID' && !preg_match('/["\\s,]/', $first) ) {
array_shift($data);
array_shift($line);
if ( empty($line) ) {
fwrite($fp, "\"{$first}\"\r\n");
} else {
fwrite($fp, "\"{$first}\",");
fputcsv($fp, $line);
fseek($fp, -1, SEEK_CUR);
fwrite($fp, "\r\n");
}
}
}
} else {
reset($header);
$first = current($header);
if ( substr($first, 0, 2) == 'ID' && !preg_match('/["\\s,]/', $first) ) {
array_shift($header);
if ( empty($header) ) {
$show_header = false;
fwrite($fp, "\"{$first}\"\r\n");
} else {
fwrite($fp, "\"{$first}\",");
}
}
}
if ( $show_header ) {
fputcsv($fp, $header);
fseek($fp, -1, SEEK_CUR);
fwrite($fp, "\r\n");
}
foreach ( $data as $line ) {
fputcsv($fp, $line);
fseek($fp, -1, SEEK_CUR);
fwrite($fp, "\r\n");
}
fclose($fp);
} else {
return false;
}
return true;
}
?>
Alright, after playing a while, I'm confident the following replacement function works in all cases, including the ones for which the native fputcsv function fails. If fputcsv fails to work for you (particularly with mysql csv imports), try this function as a drop-in replacement instead.
Arguments to pass in are exactly the same as for fputcsv, though I have added an additional $mysql_null boolean which allows one to turn php null's into mysql-insertable nulls (by default, this add-on is disabled, thus working identically to fputcsv [except this one works!]).
<?php
function fputcsv2 ($fh, array $fields, $delimiter = ',', $enclosure = '"', $mysql_null = false) {
$delimiter_esc = preg_quote($delimiter, '/');
$enclosure_esc = preg_quote($enclosure, '/');
$output = array();
foreach ($fields as $field) {
if ($field === null && $mysql_null) {
$output[] = 'NULL';
continue;
}
$output[] = preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field) ? (
$enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure
) : $field;
}
fwrite($fh, join($delimiter, $output) . "\n");
}
// the _EXACT_ LOAD DATA INFILE command to use
// (if you pass in something different for $delimiter
// and/or $enclosure above, change them here too;
// but _LEAVE ESCAPED BY EMPTY!_).
/*
LOAD DATA INFILE
'/path/to/file.csv'
INTO TABLE
my_table
FIELDS TERMINATED BY
','
OPTIONALLY ENCLOSED BY
'"'
ESCAPED BY
''
LINES TERMINATED BY
'\n'
*/
?>
I converted this from the PHP source code. This replicates PHP5 functionality exactly, whereas the other examples here do not.
<?php
if (!function_exists('fputcsv')) {
function fputcsv(&$handle, $fields = array(), $delimiter = ',', $enclosure = '"') {
$str = '';
$escape_char = '\\';
foreach ($fields as $value) {
if (strpos($value, $delimiter) !== false ||
strpos($value, $enclosure) !== false ||
strpos($value, "\n") !== false ||
strpos($value, "\r") !== false ||
strpos($value, "\t") !== false ||
strpos($value, ' ') !== false) {
$str2 = $enclosure;
$escaped = 0;
$len = strlen($value);
for ($i=0;$i<$len;$i++) {
if ($value[$i] == $escape_char) {
$escaped = 1;
} else if (!$escaped && $value[$i] == $enclosure) {
$str2 .= $enclosure;
} else {
$escaped = 0;
}
$str2 .= $value[$i];
}
$str2 .= $enclosure;
$str .= $str2.$delimiter;
} else {
$str .= $value.$delimiter;
}
}
$str = substr($str,0,-1);
$str .= "\n";
return fwrite($handle, $str);
}
}
?>
To produce RFC 4180 conforming output, do not use fputcsv but encode manually, like this:
function rfccsv($arr){
foreach($arr as &$a){
$a=strval($a);
if(strcspn($a,",\"\r\n")<strlen($a))$a='"'.strtr($a,array('"'=>'""')).'"';
}
return implode(',',$arr);
}
echo rfccsv(array(.....))."\n";