0

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++

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
katya.lee
  • 83
  • 2
  • 12
  • lookout for unicode – Notepad Dec 13 '16 at 06:51
  • Wouldn't changing `$filepath="Data/Snap Pitzaa/design.pdf \r\n";` to `$filepath="Data/Snap Pitzaa/design.pdf";` fix it, i.e. remove the explicit CR/LF you are including within the string? – YowE3K Dec 13 '16 at 06:52
  • thanks for suggestion, can you plz read my below comment cos both of you suggest me same thing and in certain way its correct. – katya.lee Dec 13 '16 at 23:01

2 Answers2

1

fputcsv is terminated by a newline (docs). Therefore, you don't need to add the \r\n to your $filepath variable.

This:

$filepath="Data/Snap Pitzaa/design.pdf \r\n";

Should be changed to this:

$filepath='Data/Snap Pitzaa/design.pdf';

That should remove the "CRLF" and move the quote to where you want it.

Edit 12/13/16

From your comment, it sounds like you need to replace the unix-style line endings that fputcsv outputs by default with windows-style line endings. The most elegant way I've seen to do this was from this SO question:

// Writes an array to an open CSV file with a custom end of line.
//
// $fp: a seekable file pointer. Most file pointers are seekable, 
//   but some are not. example: fopen('php://output', 'w') is not seekable.
// $eol: probably one of "\r\n", "\n", or for super old macs: "\r"
function fputcsv_eol($fp, $array, $eol) {
  fputcsv($fp, $array);
  if("\n" != $eol && 0 === fseek($fp, -1, SEEK_CUR)) {
    fwrite($fp, $eol);
  }
}
Community
  • 1
  • 1
Jesse Bunch
  • 6,651
  • 4
  • 36
  • 59
  • Thanks Jesse for your answer. If I use just string, line feed added automatically at the end and this is correct but this new generated file picked up automatically by other program (which I don't have control) once it goes in CSV folder and it gives error without CR and LF at the end and if that file path is not surrounded by double quote. – katya.lee Dec 13 '16 at 23:00
  • @katya.lee see my edit. I think I know what you're asking for. – Jesse Bunch Dec 14 '16 at 03:52
  • or to just add the \r to it, just use `if (fseek($fp, -1, SEEK_CUR) === 0) fwrite($fp, "\r\n");` – Erick Robertson Mar 09 '17 at 16:51
0

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.

YowE3K
  • 23,852
  • 7
  • 26
  • 40
  • Thanks @Jesse Bunch you both showed me right thing and it solved my problem. Thanks guys, spend almost 3 hours to figure this out and without your help wouldn't be possible. – katya.lee Dec 14 '16 at 04:50