ReGRRtion!

Reply

Join Date: Oct 2008
Posts: 69
Reputation: baudday is an unknown quantity at this point 
Solved Threads: 0
baudday baudday is offline Offline
Junior Poster in Training

ReGRRtion!

 
0
  #1
Sep 18th, 2009
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 854
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 138
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: ReGRRtion!

 
0
  #2
Sep 18th, 2009
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:
  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.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 148
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

Re: ReGRRtion!

 
0
  #3
Sep 18th, 2009
I am not sure what the point of $x is in that function besides being a counter. But this should solve your recursion issue.

  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.
If you're question/problem is solved don't forget to mark the thread as Solved!

-- Code I post is usually but not always tested. If it is tested it will be against 5.2.11 or 5.3.0
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 69
Reputation: baudday is an unknown quantity at this point 
Solved Threads: 0
baudday baudday is offline Offline
Junior Poster in Training

Re: ReGRRtion!

 
0
  #4
Sep 18th, 2009
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 69
Reputation: baudday is an unknown quantity at this point 
Solved Threads: 0
baudday baudday is offline Offline
Junior Poster in Training

Re: ReGRRtion!

 
0
  #5
Sep 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 69
Reputation: baudday is an unknown quantity at this point 
Solved Threads: 0
baudday baudday is offline Offline
Junior Poster in Training

Re: ReGRRtion!

 
0
  #6
Sep 18th, 2009
I'm trying mschroeder's method, and it's coming up with this error for each list item.

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 69
Reputation: baudday is an unknown quantity at this point 
Solved Threads: 0
baudday baudday is offline Offline
Junior Poster in Training

Re: ReGRRtion!

 
0
  #7
Sep 18th, 2009
Also, it is no longer indenting the filenames as appropriate. For instance Dir, subdir indented from that, filenames indented from that.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 148
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

Re: ReGRRtion!

 
0
  #8
Sep 18th, 2009
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.
If you're question/problem is solved don't forget to mark the thread as Solved!

-- Code I post is usually but not always tested. If it is tested it will be against 5.2.11 or 5.3.0
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 69
Reputation: baudday is an unknown quantity at this point 
Solved Threads: 0
baudday baudday is offline Offline
Junior Poster in Training

Re: ReGRRtion!

 
0
  #9
Sep 18th, 2009
This would be line 97
  1. $extension = strtolower(end(explode('.',$file->getBasename())));

I suppressed the error for now. And thanks, I really appreciate the help.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 69
Reputation: baudday is an unknown quantity at this point 
Solved Threads: 0
baudday baudday is offline Offline
Junior Poster in Training

Re: ReGRRtion!

 
0
  #10
Sep 18th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC