need help in symfony project

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sobha
Forum Commoner
Posts: 56
Joined: Wed Jul 15, 2009 9:08 pm

need help in symfony project

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: need help in symfony project

Post by Celauran »

What have you tried so far and where are you getting stuck?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: need help in symfony project

Post 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.
(#10850)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: need help in symfony project

Post by Celauran »

Something like Flysystem could also be handy.
sobha
Forum Commoner
Posts: 56
Joined: Wed Jul 15, 2009 9:08 pm

Re: need help in symfony project

Post 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?
sobha
Forum Commoner
Posts: 56
Joined: Wed Jul 15, 2009 9:08 pm

Re: need help in symfony project

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: need help in symfony project

Post 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?
sobha
Forum Commoner
Posts: 56
Joined: Wed Jul 15, 2009 9:08 pm

Re: need help in symfony project

Post by sobha »

Yeah it is working...
Post Reply