PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Aug 11, 2020 2:19 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Wed Mar 23, 2016 11:07 am 
Offline
Forum Commoner

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


Top
 Profile  
 
PostPosted: Wed Mar 23, 2016 12:08 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
What have you tried so far and where are you getting stuck?

_________________


Top
 Profile  
 
PostPosted: Wed Mar 23, 2016 2:21 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
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)


Top
 Profile  
 
PostPosted: Wed Mar 23, 2016 2:51 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Something like could also be handy.

_________________


Top
 Profile  
 
PostPosted: Thu Mar 24, 2016 2:32 am 
Offline
Forum Commoner

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


Top
 Profile  
 
PostPosted: Thu Mar 24, 2016 3:09 am 
Offline
Forum Commoner

Joined: Wed Jul 15, 2009 9:08 pm
Posts: 56
Syntax: [ Download ] [ Hide ]
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?


Top
 Profile  
 
PostPosted: Thu Mar 24, 2016 6:30 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
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?

_________________


Top
 Profile  
 
PostPosted: Thu Mar 24, 2016 10:01 am 
Offline
Forum Commoner

Joined: Wed Jul 15, 2009 9:08 pm
Posts: 56
Yeah it is working...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 47 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group