Page 1 of 1
need help in symfony project
Posted: Wed Mar 23, 2016 11:07 am
by sobha
Hi,
I am new to php and need help in :
1.Loop through a folder of files.
- Each filename is built up as: customerid_invoiceid.pdf
- Extract these id's
For example:files are stored in "c:/PDF/ customerid1_invoiceid1.pdf and so on
Can you please help me on how to do this using symfony?
Re: need help in symfony project
Posted: Wed Mar 23, 2016 12:08 pm
by Celauran
What have you tried so far and where are you getting stuck?
Re: need help in symfony project
Posted: Wed Mar 23, 2016 2:21 pm
by Christopher
Yes, please any code you have tried? A list of files in a folder would be glob(). And string functions and explode() to get the IDs from the file names.
Re: need help in symfony project
Posted: Wed Mar 23, 2016 2:51 pm
by Celauran
Something like
Flysystem could also be handy.
Re: need help in symfony project
Posted: Thu Mar 24, 2016 2:32 am
by sobha
<?php
$dir = 'C:/temp/invoice';
$files1 = scandir($dir,1);
$files2 = scandir($dir, 1);
$user = explode("_", $files1[0]);
print_r($user);
?>
How do I loop this?Also is there any better way to do this in symfony?
Re: need help in symfony project
Posted: Thu Mar 24, 2016 3:09 am
by sobha
Code: Select all
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("Begin!");
$finder = new Finder();
//$finder->files()->in($path);
$finder->files()->in('/home/invoice');
// $finder->in('invoice');
foreach ($finder as $file) {
$user=explode("_", $file->getRelativePathname());
}
$output->writeln("End");
}
Is there any better way to do this in symfony?
Re: need help in symfony project
Posted: Thu Mar 24, 2016 6:30 am
by Celauran
Seems pretty good to me. I would recommend allowing the path to be a variable rather than hard coded, and I'd try to find a better name than execute(), but on the whole it seems fine. Is it not working?
Re: need help in symfony project
Posted: Thu Mar 24, 2016 10:01 am
by sobha
Yeah it is working...