PHP fputcsv insert into a new line in csv file
I expect that you are opening the CSV file on Windows.
Windows only shows a new line for \r\n
whereas Linux shows a new line for \n
.
See fputcsv and newline codes for someone else who has had a similar issue.
Line-Breaks on PHP fputcsv
I ran the code and the result is exactly what you want.
Which is:
field1,field2,field3
1,2,3
There is a line break (0x0A) after "field3" and another line break after "3".
IncredibleHat is correct, there is absolutely no comma after "field3".
See the output at http://codepad.org/k6d0t8nU
fputcsv ignore newline from database
Actually line feeds are allowed by CSV standard while they are within quotes.
When reading the file, fgetcsv
can handle them properly.
as a workaround, you can just remove newlines from the items (e.g. replacing them by spaces):
...
$data = array($no,$value->name,$value->email,$value->phone, $redeem_at,
$value->voucher_code,$solve,isset($value->score)?$value->score:0,$value->published, $urlQr,
($value->is_online_selling == 1 ? 'Sudah' : 'Belum'), $value->selling_desc,
$value->instagram_selling_acc,$value->marketplace_acc, $value->online_selling_website
);
foreach ($data as $k => $v) {
$data[$k] = preg_replace('/[\r\n]/', ' ', $v); // replace newlines by spaces
}
fputcsv($output, $data);
...
fputcsv generates line without newline
There was unknown bug, when you put fputcsv line by line it's not important where you write it, however, if you do it in loop, loop must be AFTER all headers!
header("Cache-Control:maxage=1");
header("Pragma: public");
header("Content-Type:text\csv; charset=UTF-8" );
header('Content-Disposition: attachment; filename=csv.csv');
for($i = 0; $i < count($csv); $i++){
fputcsv($fp, $csv[$i]);
}
Unwanted new line string / fputcsv
OK so I finally found a solution, I've been looking since 2 days ago. It appears that there is some hidden character, because when I make PHP use the function preg_replace and put in the condition "\n-\x0B-\r" the problem is solved.
Thought I'd share it, if someone happens to have this issue in the future.
Carriage return and line feed
Having just given myself an introduction to PHP (I've never used it before), I think the following code will correctly write a CR/LF to the end of each record you write:
foreach ($data as $row)
{
// write out the normal output, with a LF automatically created by fputcsv
fputcsv($file, $row);
// move the file pointer back to the end of the data
fseek($file, -1, SEEK_CUR);
// write out a CR/LF
fwrite($file, "\r\n");
}
You should also change
$filepath="Data/Snap Pitzaa/design.pdf \r\n";
to
$filepath="Data/Snap Pitzaa/design.pdf";
as there shouldn't be a CR/LF inside that field.
Add a new line to a CSV file
Open the CSV file for appending (fopen
Docs):
$handle = fopen("test.csv", "a");
Then add your line (fputcsv
Docs):
fputcsv($handle, $line); # $line is an array of strings (array|string[])
Then close the handle (fclose
Docs):
fclose($handle);
Related Topics
Symfony2 - Assetic - CSS Font Icons
Composer Killed While Updating
Why Is Textarea Filled with Mysterious White Spaces
Sanitizing User's Data in Get by PHP
How to Trim White Spaces of Array Values in PHP
How to Resolve "Must Be an Instance of String, String Given" Prior to PHP 7
How to Send Email with Attachment Using PHP
Query_Posts() Should Be Avoided
PHP Upload, Extract and Progressbar
How to Use PHP to Dynamically Publish an Ical File to Be Read by Google Calendar
Avoiding Form Resubmit in PHP When Pressing F5
How to Find the Socket Transport "Ssl" - Did You Forget to Enable It When You Configured PHP