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

the output method can not be used for saving CSV document to the underlying filesystem #434

Closed
mitmelon opened this issue Aug 13, 2021 · 4 comments

Comments

@mitmelon
Copy link

mitmelon commented Aug 13, 2021

When using

$csv = Writer::createFromString();
$csv->insertOne($records);
$csv->output(DIR.'/../users.csv');

Error popups below... Why cant it support path?

Fri Aug 13 15:59:01.224064 2021] [php:error] [pid 4580:tid 1716] [client 127.0.0.1:52529] PHP Fatal error: Uncaught League\Csv\InvalidArgument: The filename C:\\xampp\\htdocs\\project\\src\\Register/../users.csv cannot contain the "/" and "\" characters. in vendor\league\csv\src\InvalidArgument.php:40\nStack trace:\n#0 vendor\league\csv\src\AbstractCsv.php(392): League\Csv\InvalidArgument::dueToInvalidHeaderFilename('C:\\xampp\\htdocs...')\n#1 vendor\league\csv\src\AbstractCsv.php(367): League\Csv\AbstractCsv->sendHeaders('C:\\xampp\\htdocs...')\n#2

@nyamsprod
Copy link
Member

@mitmelon thanks for using the library. As explain by the exception message your $filename argument is invalid. You can refer to the library documentation where it is stated:

starting with version 9.1.0, the output method will throw an Exception if the provided $filename does not comply with RFC6266

@mitmelon
Copy link
Author

@mitmelon thanks for using the library. As explain by the exception message your $filename argument is invalid. You can refer to the library documentation where it is stated:

starting with version 9.1.0, the output method will throw an Exception if the provided $filename does not comply with RFC6266

Which means the library does not support locations with path like file/filename.csv because have tried this too and does not work... It can only save files to current directory.

And again it will be good if the library can just write directly inside a file and save the file inside the desired destinations... Instead of forcing it to store files in same director... As the directory might be public and people could access the file...

@thephpleague thephpleague deleted a comment from mitmelon Aug 14, 2021
@nyamsprod
Copy link
Member

nyamsprod commented Aug 14, 2021

Which means the library does not support locations with path like file/filename.csv because have tried this too and does not work... It can only save files to current directory.

No as a matter a fact, I believe you are using the wrong method for your use case. Again if you refer to examples in the documentation You will see that:

  • first you define the path where you want you CSV document to be saved to the underlying filesystem
  • then you use the Writer::insert* method of your choice

So in your example you should do the following instead:

<?php
use League\Csv\Writer;

$csv = Writer::createFromPath(__DIR__.'/../users.csv');
$csv->insertOne($header);
$csv->insertAll($records); 

The ::output method as stated in the documentation (ie see the link in the previous comment) is for downloading your CSV document using the HTTP protocol hence the validation on the filename prior to it being added to the created HTTP response header.

Hopes this clarify your behaviour better.

@nyamsprod nyamsprod changed the title $csv->output('users.csv'); Not accept path the output method can not be used for saving CSV document to the underlying filesystem Aug 14, 2021
@nyamsprod nyamsprod pinned this issue Aug 14, 2021
@mitmelon
Copy link
Author

Which means the library does not support locations with path like file/filename.csv because have tried this too and does not work... It can only save files to current directory.

No as a matter a fact, I believe you are using the wrong method for your use case. Again if you refer to examples in the documentation You will see that:

  • first you define the path where you want you CSV document to be saved to the underlying filesystem
  • then you use the Writer::insert* method of your choice

So in your example you should do the following instead:

<?php
use League\Csv\Writer;

$csv = Writer::createFromPath(__DIR__.'/../users.csv');
$csv->insertOne($header);
$csv->insertAll($records); 

The ::output method as stated in the documentation (ie see the link in the previous comment) is for downloading your CSV document using the HTTP protocol hence the validation on the filename prior to it being added to the created HTTP response header.

Hopes this clarify your behaviour better.

Thank you very much... I was using createFromString(); that's where the issue was...

Thanks for the clearifications...

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

No branches or pull requests

2 participants