php save to csv file

前端 未结 3 1700
失恋的感觉
失恋的感觉 2021-01-28 03:10

I\'m wanting to store basic data from a single form box and I\'ve created this php code base, but it doesn\'t seem to be working. Can someone take a glance and see if anything

相关标签:
3条回答
  • 2021-01-28 03:44

    This part will not behave like you expect, the variables are not evaluated when inside single quotes:

    $cvsData ='"$name","$date"'.PHP_EOL;
    

    You will need to use double quotes:

    $cvsData ="\"$name\",\"$date\"".PHP_EOL; 
    
    0 讨论(0)
  • 2021-01-28 03:56

    Use the nicer way in php : fputcsv

    Otherwise you need to do lot of error handling to achieve in your case.

    $list = array (
        array('First Name', 'Last Name', 'Age'),
        array('Angelina ', 'Jolie', '37'),
        array('Tom', 'Cruise', '50')
    );
    
    $fp = fopen('file.csv', 'w');
    
    foreach ($list as $fields) {
        fputcsv($fp, $fields);
    }
    
    fclose($fp);
    
    0 讨论(0)
  • 2021-01-28 04:00

    You should look into fputcsv. This will add CSV to you file and take care of fields and line ends.

    fputcsv($fp,array(array($name,$date)));
    

    You can also specify delimiters and such if you want.

    0 讨论(0)
提交回复
热议问题