[thelist] writing to a file PHP (csv)

Carl J Meyer cjmeyer at npcc.net
Mon Dec 16 16:13:01 CST 2002


On Mon, 2002-12-16 at 13:12, Andrew Maynes wrote:
> have so far.  However it isn't putting the data in csv format (tab)?
> $FileName="admin/email_list.txt";
> 		$fp = fopen($FileName, "a+");
> 		fputs($fp, "$cc,\n");
> 		fclose($fp);

Without knowing the contents of the variable '$cc', this doesn't tell us
anything at all.  /You/ have to put the data in the right format,
fputs() just writes whatever you give it to the file.  'CSV' format is
just a regular old text file with one-record-per-line, fields
comma-separated.  Here's a quickie function that returns a CSV string
(which you could save to file using your code above), given data in a 2d
array (optionally you can specify the separator, if you want
tab-separated use "\t"):

function makeCSV($data, $sep = ',')
    if(!is_array($data)) { return ''; }
    $ret = '';
    foreach($data as $record)
            { $ret .= join($sep, $record) . "\n"; }
    return $ret;


More information about the thelist mailing list