| | |
ReGRRtion!
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2008
Posts: 69
Reputation:
Solved Threads: 0
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)
function list_dir($handle, $path) { // Loop over the directory and don\"t display certain files global $x; $x = 0; echo "<ul>"; //running the while loop while (false !== ($file = readdir($handle))) { $extension = strtolower(substr(strrchr($file, '.'), 1)); $dir =$path.'/'.$file; global $file_path; $file_path[$x] = $path; if(is_dir($dir) && $file != '.' && $file !='..' ) { $handle = opendir($dir) or die("unable to open file $file"); echo "<li><b>$file</b></li>"; list_dir($handle, $dir); } elseif($extension == "zip") { unlink($file); } elseif($extension == "mp3" || $extension == "mpa" && $file != '.' && $file !='..') { 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>"; } $x++; } echo "</ul>"; }
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)
function list_dir($handle, $path, $x = 0) { // Loop over the directory and don\"t display certain files echo "<ul>"; //running the while loop while (false !== ($file = readdir($handle))) { $extension = strtolower(substr(strrchr($file, '.'), 1)); $dir =$path.'/'.$file; if(is_dir($dir) && $file != '.' && $file !='..' ) { $handle = opendir($dir) or die("unable to open file $file"); echo "<li><b>$file</b></li>"; $x = list_dir($handle, $dir, $x); } elseif($extension == "zip") { unlink($file); } elseif($extension == "mp3" || $extension == "mpa" && $file != '.' && $file !='..') { 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>"; } $x++; } echo "</ul>"; return $x; }
Last edited by pritaeas; Sep 18th, 2009 at 9:26 am.
"If it is NOT source, it is NOT software."
-- NASA
-- NASA
•
•
Join Date: Jul 2008
Posts: 148
Reputation:
Solved Threads: 25
I am not sure what the point of $x is in that function besides being a counter. But this should solve your recursion issue.
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.
PHP Syntax (Toggle Plain Text)
<?php //Path to starting point $path = '/your/path/goes/here'; //Create a recursive directory iterator $iterator = new RecursiveDirectoryIterator($path); echo "<ul>\n"; $x=0; //Foreach iteration do something. foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST) as $file) { $extension = strtolower(end(explode('.',$file->getBasename()))); if( $file->isDir() ) { echo "<li><b>{$file->getBasename()}</b></li>\n"; } else if( $file->isFile() && $extension == 'zip' ) { //unlink($file->getPathname()); } else if( $file->isFile() && ($extension == 'mp3' || $extension == 'mpa') ) { echo "<li> <input type = \"checkbox\" name = \"file$x\" value = \"{$file->getBasename()}\" /> <input type = \"hidden\" name = \"file".$x."_path\" value = \"{$file->getPathname()}\" /> <a href=\"{$file->getPathname()}\">{$file->getBasename()}</a> </li>\n"; } ++$x; } 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
-- Code I post is usually but not always tested. If it is tested it will be against 5.2.11 or 5.3.0
•
•
Join Date: Oct 2008
Posts: 69
Reputation:
Solved Threads: 0
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)
$x = 0; function list_dir($handle, $path,$x) { // Loop over the directory and don\"t display certain files echo "<ul>"; //running the while loop while (false !== ($file = readdir($handle))) { $extension = strtolower(substr(strrchr($file, '.'), 1)); $dir =$path.'/'.$file; global $file_path; $file_path[$x] = $path; if(is_dir($dir) && $file != '.' && $file !='..' ) { $handle = opendir($dir) or die("unable to open file $file"); echo "<li><b>$file</b></li>"; list_dir($handle, $dir,$x); } elseif($extension == "zip") { unlink($file); } elseif($extension == "mp3" || $extension == "mpa" && $file != '.' && $file !='..') { 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>"; } $x++; } echo "</ul>"; return $x; }
•
•
Join Date: Oct 2008
Posts: 69
Reputation:
Solved Threads: 0
I'm trying mschroeder's method, and it's coming up with this error for each list item.
PHP Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jul 2008
Posts: 148
Reputation:
Solved Threads: 25
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.
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
-- Code I post is usually but not always tested. If it is tested it will be against 5.2.11 or 5.3.0
•
•
Join Date: Oct 2008
Posts: 69
Reputation:
Solved Threads: 0
This would be line 97
I suppressed the error for now. And thanks, I really appreciate the help.
php Syntax (Toggle Plain Text)
$extension = strtolower(end(explode('.',$file->getBasename())));
I suppressed the error for now. And thanks, I really appreciate the help.
![]() |
Other Threads in the PHP Forum
- Previous Thread: I need to embed stock live charts in website
- Next Thread: Quick foreach question
| Thread Tools | Search this Thread |
apache api array basic beginner binary broken cache cakephp checkbox class cms code computing confirm cron curl customizableitems database date delete display dynamic echo email error external file files filter folder form forms forum function functions gc_maxlifetime google headmethod host howtowriteathesis href htaccess html iframe image include insert ip javascript joomla limit link login mail malfunction memmory memory menu mlm multiple mysql navigation oop parsing paypal pdf php phpmysql problem query question radio random recursion remote script search select server sessions sms snippet source space sql syntax system table thesishelp trouble tutorial update upload url validator variable video web youtube





