943,549 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 730
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 18th, 2009
0

ReGRRtion!

Expand Post »
Can someone please help me with this recursive function! It's messing up the incrementing of $x. I don't know what's wrong and I've been looking at it for so long now.

php Syntax (Toggle Plain Text)
  1. function list_dir($handle, $path) {
  2. // Loop over the directory and don\"t display certain files
  3. global $x;
  4. $x = 0;
  5. echo "<ul>";
  6. //running the while loop
  7. while (false !== ($file = readdir($handle))) {
  8. $extension = strtolower(substr(strrchr($file, '.'), 1));
  9. $dir =$path.'/'.$file;
  10. global $file_path;
  11. $file_path[$x] = $path;
  12. if(is_dir($dir) && $file != '.' && $file !='..' ) {
  13. $handle = opendir($dir) or die("unable to open file $file");
  14. echo "<li><b>$file</b></li>";
  15. list_dir($handle, $dir);
  16. }
  17.  
  18. elseif($extension == "zip") {
  19. unlink($file);
  20. }
  21.  
  22. elseif($extension == "mp3" || $extension == "mpa" && $file != '.' && $file !='..') {
  23. echo "<li><input type = \"checkbox\" name = \"file$x\" value = \"$file\" /><input type = \"hidden\" name = \"file".$x."_path\" value = \"".$file_path[$x]."\" /><a href=\"".$file_path[$x]."/$file\">$file</a></li>";
  24. }
  25. $x++;
  26. }
  27.  
  28. echo "</ul>";
  29. }
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 don't think there is a need to use $x. In general it is not a good idea to use globals in recursive functions. I have no possibility to test my code, but you could try this:
php Syntax (Toggle Plain Text)
  1. function list_dir($handle, $path, $x = 0) {
  2. // Loop over the directory and don\"t display certain files
  3. echo "<ul>";
  4. //running the while loop
  5. while (false !== ($file = readdir($handle))) {
  6. $extension = strtolower(substr(strrchr($file, '.'), 1));
  7. $dir =$path.'/'.$file;
  8. if(is_dir($dir) && $file != '.' && $file !='..' ) {
  9. $handle = opendir($dir) or die("unable to open file $file");
  10. echo "<li><b>$file</b></li>";
  11. $x = list_dir($handle, $dir, $x);
  12. }
  13.  
  14. elseif($extension == "zip") {
  15. unlink($file);
  16. }
  17.  
  18. elseif($extension == "mp3" || $extension == "mpa" && $file != '.' && $file !='..') {
  19. echo "<li><input type = \"checkbox\" name = \"file$x\" value = \"$file\" /><input type = \"hidden\" name = \"file".$x."_path\" value = \"".$path."\" /><a href=\"".$path."/$file\">$file</a></li>";
  20. }
  21. $x++;
  22. }
  23. echo "</ul>";
  24. return $x;
  25. }
Last edited by pritaeas; Sep 18th, 2009 at 9:26 am.
Sponsor
Featured Poster
Reputation Points: 549
Solved Threads: 722
Bite my shiny metal ass!
pritaeas is offline Offline
4,156 posts
since Jul 2006
Sep 18th, 2009
0

Re: ReGRRtion!

I am not sure what the point of $x is in that function besides being a counter. But this should solve your recursion issue.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. //Path to starting point
  4. $path = '/your/path/goes/here';
  5.  
  6. //Create a recursive directory iterator
  7. $iterator = new RecursiveDirectoryIterator($path);
  8.  
  9. echo "<ul>\n";
  10. $x=0;
  11.  
  12. //Foreach iteration do something.
  13. foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST) as $file)
  14. {
  15.  
  16. $extension = strtolower(end(explode('.',$file->getBasename())));
  17.  
  18. if( $file->isDir() )
  19. {
  20. echo "<li><b>{$file->getBasename()}</b></li>\n";
  21.  
  22. }
  23. else if( $file->isFile() && $extension == 'zip' )
  24. {
  25. //unlink($file->getPathname());
  26. }
  27. else if( $file->isFile() && ($extension == 'mp3' || $extension == 'mpa') )
  28. {
  29. echo "<li>
  30. <input type = \"checkbox\" name = \"file$x\" value = \"{$file->getBasename()}\" />
  31. <input type = \"hidden\" name = \"file".$x."_path\" value = \"{$file->getPathname()}\" />
  32. <a href=\"{$file->getPathname()}\">{$file->getBasename()}</a>
  33. </li>\n";
  34. }
  35. ++$x;
  36. }
  37. echo "</ul>";

It appears $x is just being used so there is a unique number to put into the checkboxes. However with php if you are using this to build a form that you can select music files to delete or move or whatever, using a checkbox name of `anything[]` will create a php array of checkbox values on submit.

Then your checkbox value can be the actual path to the file including the filename. No need to have the hidden input then.
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!

Awesome! Thanks for the help. That wasn't the exact solution, but it helped tremendously and pointed out a fatal flaw in my code. Every time list_dir was called it would reset $x to 0. So I defined $x outside the function, and passed it in as a variable. My code for that portion now looks like this.

php Syntax (Toggle Plain Text)
  1. $x = 0;
  2. function list_dir($handle, $path,$x) {
  3. // Loop over the directory and don\"t display certain files
  4. echo "<ul>";
  5. //running the while loop
  6. while (false !== ($file = readdir($handle))) {
  7. $extension = strtolower(substr(strrchr($file, '.'), 1));
  8. $dir =$path.'/'.$file;
  9. global $file_path;
  10. $file_path[$x] = $path;
  11. if(is_dir($dir) && $file != '.' && $file !='..' ) {
  12. $handle = opendir($dir) or die("unable to open file $file");
  13. echo "<li><b>$file</b></li>";
  14. list_dir($handle, $dir,$x);
  15. }
  16.  
  17. elseif($extension == "zip") {
  18. unlink($file);
  19. }
  20.  
  21. elseif($extension == "mp3" || $extension == "mpa" && $file != '.' && $file !='..') {
  22. echo "<li><input type = \"checkbox\" name = \"file$x\" value = \"$file\" /><input type = \"hidden\" name = \"file".$x."_path\" value = \"".$file_path[$x]."\" /><a href=\"".$file_path[$x]."/$file\">$file</a></li>";
  23. }
  24. $x++;
  25. }
  26.  
  27. echo "</ul>";
  28. return $x;
  29. }
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 was writing my reply as you were adding your last reply. Thank you. I will try this simpler method out. I did not know it automatically saved them in an array.
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'm trying mschroeder's method, and it's coming up with this error for each list item.

PHP Syntax (Toggle Plain Text)
  1. Strict Standards: Only variables should be passed by reference in /opt/lampp/htdocs/Music/index.php on line 97
Last edited by baudday; Sep 18th, 2009 at 10:45 am.
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!

Also, it is no longer indenting the filenames as appropriate. For instance Dir, subdir indented from that, filenames indented from that.
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!

post what is on line 97 of your file. There is nothing in my code that is passed by reference.

The indentation will be a little more tricky but i'll see what i can do.
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!

This would be line 97
php Syntax (Toggle Plain Text)
  1. $extension = strtolower(end(explode('.',$file->getBasename())));

I suppressed the error for now. And thanks, I really appreciate the help.
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!

If at all possible, could you help me remove . and .. from the list of files shown? Thought I knew how, but I couldn't figure it out.
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