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

Create CSV file in the custom path using VFS #266

Open
jmdeepak007git opened this issue Sep 6, 2021 · 3 comments
Open

Create CSV file in the custom path using VFS #266

jmdeepak007git opened this issue Sep 6, 2021 · 3 comments
Labels

Comments

@jmdeepak007git
Copy link

HI Guys,

I have a symfony php command where the parameter expects to send a csv file path. I would like to create a csv in a custom path using VFSStream. How is it possible to achieve this please ?

I can see when I create a csv file in the root directory. I see vfs embed path like this vfs:/root/foo.csv.
Instead of the above path, I would like to create foo.csv in a custom local directory.

Thanks alot.

@jmdeepak007git jmdeepak007git changed the title Create csv file to custom path in VFS Create CSV file in the custom path using VFS Sep 6, 2021
@allejo allejo added the question label Sep 7, 2021
@allejo
Copy link
Member

allejo commented Sep 7, 2021

I would like to create foo.csv in a custom local directory.

Are you looking for file_put_contents? Just give it a local path instead of a VFS path.

@jmdeepak007git
Copy link
Author

But I would like to use VFS file system for storing data in a virtual file.

@bizurkur
Copy link
Contributor

If you mean "custom local directory" INSIDE vfs, then:

$root = vfsStream::setup();
$myCustomDir = $root->url() . '/some/custom/path';
// vfs://root/some/custom/path
mkdir($myCustomDir, 0777, true);

$myCsvFile = $myCustomDir . '/some.csv';
// vfs://root/some/custom/path/some.csv
file_put_contents($myCsvFile, "a,b,c\r\n1,2,3\r\n");

echo file_get_contents($myCsvFile);

If you don't want root as the first folder:

$root = vfsStream::setup('my_folder'); // <-- give a custom name for "root"
echo $root->url();
// vfs://my_folder

However, once that php process exits that file no longer exists. If you're trying to run a symfony command like bin/console some:command vfs://path/to/some.csv it's not going to work.

If you're creating the file in-memory within the command, you don't need vfs for that.

// write CSV to memory
$fh = fopen('php://temp', 'w+');
fputcsv($fh, ['a', 'b', 'c']);
fputcsv($fh, ['1', '2', '3']);

// rewind to start of file
rewind($fh);

// read the file and do whatever you want with the data
while (($data = fgetcsv($fh, 1000, ",")) !== false) {
    var_dump($data);
}
fclose($fh);

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

3 participants