I got stuck with carriage return and line feed problem. When I run my script it shows CR,LF inside double quote of last field and there is another LF after than, realistically CR,LF should be outside of double quote of text and there shouldn't be another LF. Can somebody tell me what am I doing wrong?
Here is my code
$jobno = 5285;
$directory = "../CSV/";
$filename = $jobno.'.csv';
if( is_dir($directory) === false )
{
mkdir($directory); // Create new directory
}
$newFname = $directory.$filename;
$file = fopen($newFname, 'w');
$jobdetails="2,000 Items Supplied";
$customerName="Snap Pitzaa Ltd";
$workflow="CSV_wise";
$jobqty=50;
$filepath="Data/Snap Pitzaa/design.pdf \r\n";
$data = array(
array($jobno, $jobdetails, $customerName, $workflow, $jobqty, $filepath),
array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25', 'Data 26'),
);
// save each row of the data
foreach ($data as $row)
{
fputcsv($file, $row);
}
// Close the file
fclose($file);
I've tried all the things like single quote, outside doubleheader of $filepath
but none of this is working. Here is output in notepad++
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.