943,649 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 730
  • PHP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 18th, 2009
0

Re: ReGRRtion!

change that line to:
PHP Syntax (Toggle Plain Text)
  1. $parts = explode('.',$file->getBasename());
  2. $extension = strtolower(end($parts));

apparently explode returns its value by reference.

Are . and .. showing? In my quick testing they didn't show up at all.
Last edited by mschroeder; Sep 18th, 2009 at 12:24 pm.
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
624 posts
since Jul 2008
Sep 18th, 2009
0

Re: ReGRRtion!

Yeah they're showing up several times actually scattered throughout the files.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
baudday is offline Offline
99 posts
since Oct 2008
Sep 18th, 2009
0

Re: ReGRRtion!

I've now tested it on both linux (centos) and windows (xp) and both php 5.2.10 and php 5.3.0 and don't see the dot directories showing up.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. error_reporting(E_ALL | E_STRICT);
  3.  
  4. $path = '/path/to/file';
  5.  
  6. $iterator = new RecursiveDirectoryIterator($path);
  7. $recurse = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
  8. echo "<ul>\n";
  9. $x=0;
  10. foreach ( $recurse as $file)
  11. {
  12. $parts = explode('.',$file->getBasename());
  13. $extension = strtolower(end($parts));
  14.  
  15. if( $file->isDir() && !$recurse->isDot() )
  16. {
  17. echo "<li><b>{$file->getBasename()}</b></li>\n";
  18.  
  19. }
  20. else if( $file->isFile() && $extension == 'zip' )
  21. {
  22. //unlink($file->getPathname());
  23. }
  24. else if( $file->isFile() && ($extension == 'mp3' || $extension == 'mpa') )
  25. {
  26. echo "<li>
  27. <input type = \"checkbox\" name = \"file$x\" value = \"{$file->getBasename()}\" />
  28. <input type = \"hidden\" name = \"file".$x."_path\" value = \"{$file->getPathname()}\" />
  29. <a href=\"{$file->getPathname()}\">{$file->getBasename()}</a>
  30. </li>\n";
  31. }
  32. ++$x;
  33. }
  34. echo "</ul>";
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
624 posts
since Jul 2008
Sep 18th, 2009
-1

Re: ReGRRtion!

PHP Syntax (Toggle Plain Text)
  1. $op = "<select name='opts' id='opts'>";
  2. $dir = new DirectoryIterator( '/www/test/' );
  3. foreach($dir as $file ){
  4. if(!$file->isDot() && !$file->isDir() && preg_match("/.mp3$/",$file->getFilename())) {
  5. $op .= "<option value='{$file->getFilename()}'>{$file->getFilename()}</option>";
  6. }
  7. }
  8. $op .= "<select>";
  9. echo $op;

The above should print out all mp3 files to a dropdown. If you want to include other extensions, just change the regex in the preg_match parameter. 9 lines - should be nice. Cannibalize to your needs (ul list, etc).
Last edited by ardav; Sep 18th, 2009 at 2:41 pm. Reason: doh!
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 943
Sarcastic Poster
ardav is offline Offline
6,671 posts
since Oct 2006
Sep 18th, 2009
0

Re: ReGRRtion!

So that solved some of the dots. The problem comes in when I get to a 3rd/4th level directory, Music->Artist->Album->Disc1 for instance. Then the dots start showing up again.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
baudday is offline Offline
99 posts
since Oct 2008
Sep 18th, 2009
0

Re: ReGRRtion!

OOPS! Forgot to change one small part to what mschroeder suggested. The dots are gone. Any ideas on the indentation?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
baudday is offline Offline
99 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Best compression
Next Thread in PHP Forum Timeline: Quick foreach question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC