文本内容未完全写入csv文件

时间:2012-11-29 10:40:40

标签: php html file file-upload file-io

我正在通过html表单编写 CSV 文件,该表格通过 textarea 发布大量文字内容。

然而,整篇文章并未在 csv 中创建,使用此PHP代码创建 csv 文件:

$fp = fopen('file.csv', 'w');
        foreach($_POST as $value)
        {
            fputcsv($fp, $value);
        }
        fclose($fp); 

2 个答案:

答案 0 :(得分:0)

如果您的textarea的名称是“content”,请使用此

if( isset($_POST['content']) ) {
    file_put_contents( 'file.csv', $_POST['content'] );
}

如果$ _POST ['content']是有效的CSV内容

答案 1 :(得分:0)

此代码将生成CSV文件

 <?php
 /* If file has not been created this will form title header(Use only once)
 file_put_contents('file.csv',"Title,Description\r\n", FILE_APPEND);
 */

每次发布​​表单时,剩下的代码都会添加一个新行

 if( isset($_POST['Title'],$_POST['desc']) ) {
 file_put_contents('file.csv',$_POST['Title'].",". $_POST['desc']."\r\n", FILE_APPEND); 
 }
 ?>