Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwanted additional line at end of CSV file #53

Closed
garethellis36 opened this issue Oct 22, 2014 · 5 comments
Closed

Unwanted additional line at end of CSV file #53

garethellis36 opened this issue Oct 22, 2014 · 5 comments
Labels

Comments

@garethellis36
Copy link

I'm trying to create a new CSV file and write it to disk so that it can be emailed. This may not be an issue with the package but may be an issue with the way I am doing things.

Code:

    //create empty file to store CSV data (is there a better way of doing this?)
$handle = fopen($filepath, "w");
fwrite($handle, "");

    //create new writer instance
$csv = League\Csv\Writer::createFromPath(new SplFileObject($filepath));

    //insert data from array
$csv->insertAll($csv_data);

The resulting CSV has all the data in it as expected but there is an additional line with no entry on it.

This is a problem because the resulting CSV has to be uploaded into an Oracle-based system and it does not like empty lines in CSV files.

@nyamsprod
Copy link
Member

Ok as per the documentation there are several ways to instantiate the object I think you've just combined many of them. The problem may be related to that. I would suggest, simplify your code like below:

$filepath = '/path/to/my/csv/file.csv';
$csv = League\Csv\Writer::createFromPath($filepath, 'w');
$csv->insertAll($csv_data);

Nothing more... it will work and the empty line should disappear. You do not need to use fopen first. by using the w open mode the library will try to open or create the file if it does not succeed a RuntimeException will be thrown.

@garethellis36
Copy link
Author

Thanks for your response. I was trying something similar but the missing piece was the "w" in the second argument to create the file.

Anyway, it's still putting an empty line at the end of the CSV file.

Code is now:

$csv = League\Csv\Writer::createFromPath($filepath, "w");
$csv->insertAll($csv_data);

@nyamsprod
Copy link
Member

The end line is put because of SplFileObject::fputcsv as per documentation:

fputcsv() formats a line (passed as a fields array) as CSV and write it (terminated by a newline) to the specified file handle.

@garethellis36
Copy link
Author

Is it worth doing a PR to have an option which doesn't include the newline?

@nyamsprod
Copy link
Member

No need to PR this, there's a quick workaround for that:

function outputWithoutEndingEOL(League\Csv\AbstractCSV $csv)
{
    return  rtrim($csv->newReader());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants