madspof Posted August 30, 2006 Share Posted August 30, 2006 i need this scritpt to search sub folders aswell i would be very apperictive if some one could reply with a script that could or give me a explanation on how to do it madspof<?php$directory = "music"; $fp = opendir($directory);while ($file = readdir($fp)) { if (!ereg("^\.", $file)) { $file_url = "$directory/" . $file;?><body bgproperties="fixed" background="back%20(Custom).jpg"><div align="center"><a href="<?php echo("playertest.php?song=$file"); ?>" target="banner2"><?php echo($file); ?></a><br> <?php }}closedir($fp);?> Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 30, 2006 Share Posted August 30, 2006 You need to have a recursive function. Following is some code I picked up from one of the numerous FREE directory functions from the web. Do with it what you want.[code]<?php getDirectory('.');// SAMPLES:// getDirectory( "." ); // Get the current directory // getDirectory( "./files/includes" ); // Get contents of the "files/includes" folder */function getDirectory( $path = '.', $level = 0 ){ $ignore = array( 'cgi-bin', '.', '..' ); // Directories to ignore when listing output. Many hosts // will deny PHP access to the cgi-bin. $dh = @opendir( $path ); // Open the directory to the handle $dh while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory if( !in_array( $file, $ignore ) ){ // Check that this file is not to be ignored $spaces = str_repeat( ' ', ( $level * 4 ) ); // Just to add spacing to the list, to better // show the directory tree. if( is_dir( "$path/$file" ) ){ // Its a directory, so we need to keep reading down... echo "<strong>$spaces $file</strong><br />"; getDirectory( "$path/$file", ($level+1) ); // Re-call this same function but on a new directory. // this is what makes function recursive. } else { echo "$spaces $file<br />"; // Just print out the filename } } } closedir( $dh ); // Close the directory handle } ?>[/code]Ronald 8) Quote Link to comment Share on other sites More sharing options...
madspof Posted August 30, 2006 Author Share Posted August 30, 2006 wit the script you have jsut sent me is there any way of making every file it finds give it a href back to the file Quote Link to comment Share on other sites More sharing options...
joking Posted August 30, 2006 Share Posted August 30, 2006 JUST ADD THE LINK in the echo ........... instead of [code]echo "<strong>$spaces $file</strong><br />";[/code]write:[code]echo "<a href=playertest.php?song=$file target=banner2><strong>$spaces $file</strong></a><br />";[/code] Quote Link to comment Share on other sites More sharing options...
madspof Posted August 30, 2006 Author Share Posted August 30, 2006 when i click one on the links it can not handle the space and only link the first part of the file i.;e james when it should be james%20brown.mp3 Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 30, 2006 Share Posted August 30, 2006 Well you'll have to enclose it then, msuch as:[code]echo "<a href='playertest.php?song=$file&target=banner2'><strong>$spaces $file</strong></a><br />";[/code]Ronald 8) Quote Link to comment Share on other sites More sharing options...
joking Posted August 30, 2006 Share Posted August 30, 2006 try str_replace("", "%20", $URL);Go to this link and view more info about this functio:[url=http://www.php.net/manual/en/function.str-replace.php]http://www.php.net/manual/en/function.str-replace.php[/url] Quote Link to comment Share on other sites More sharing options...
madspof Posted August 30, 2006 Author Share Posted August 30, 2006 were would i enter that replace code Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 30, 2006 Share Posted August 30, 2006 madspof. I will cook your food, serve it on a plate, put it in your mouth, but YOU'LL HAVE TO CHEW IT! Quote Link to comment Share on other sites More sharing options...
madspof Posted August 30, 2006 Author Share Posted August 30, 2006 sorry i seem a bit lyk dat am jsut new to the whoel php and am trying to place my feet on the gorund with it all Quote Link to comment Share on other sites More sharing options...
madspof Posted August 30, 2006 Author Share Posted August 30, 2006 so is there any chance you chould show me i rly need to get this part of my site up Quote Link to comment Share on other sites More sharing options...
joking Posted August 30, 2006 Share Posted August 30, 2006 madspof you should really try to search and find answers for urself.The part of the URL that is causing u trouble is $file So u should replace the " " in $file with "%20" ......it should be like this [code]echo "<a href=playertest.php?song=".str_replace("", "%20", $file)." target=banner2><strong>$spaces $file</strong></a><br />";[/code] Quote Link to comment Share on other sites More sharing options...
madspof Posted August 30, 2006 Author Share Posted August 30, 2006 i have tried so many different things but i crnt get it to work the script cannot handle the space in the file name so if a file was called joe blogs.mp3 it come out like this joe on it own when it should be joe%20blogs.mp3 Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 30, 2006 Share Posted August 30, 2006 Now we can go on like this forever and not getting anywhere.madspof: just show all code you have sofar.Ronald 8) Quote Link to comment Share on other sites More sharing options...
madspof Posted August 30, 2006 Author Share Posted August 30, 2006 <?php getDirectory('music');// SAMPLES:// getDirectory( "." ); // Get the current directory // getDirectory( "./files/includes" ); // Get contents of the "files/includes" folder */function getDirectory( $path = 'music', $level = 0 ){ $ignore = array( 'cgi-bin', '.', '..' ); // Directories to ignore when listing output. Many hosts // will deny PHP access to the cgi-bin. $dh = @opendir( $path ); // Open the directory to the handle $dh while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory if( !in_array( $file, $ignore ) ){ // Check that this file is not to be ignored $spaces = str_repeat( ' ', ( $level * 4 ) ); // Just to add spacing to the list, to better // show the directory tree. if( is_dir( "$path/$file" ) ){ // Its a directory, so we need to keep reading down... echo "<strong>$spaces $file</strong><br />"; getDirectory( "$path/$file", ($level+1) ); // Re-call this same function but on a new directory. // this is what makes function recursive. } else { echo "<a href=playertest.php?song=".str_replace("", "%20", $file)." target=main><strong>$spaces $file</strong></a><br />"; // Just print out the filename } } } closedir( $dh ); // Close the directory handle } ?> Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 30, 2006 Share Posted August 30, 2006 [quote author=madspof link=topic=106220.msg425000#msg425000 date=1156971789]so if a file was called joe blogs.mp3 it come out like this joe on it own when it should be joe%20blogs.mp3[/quote]Use urldecode to get rid of the %20 and such...http://www.php.net/urldecode Quote Link to comment Share on other sites More sharing options...
madspof Posted August 30, 2006 Author Share Posted August 30, 2006 i dnt need to get rid of the %20 i need to put one in place of every space in the file name Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 30, 2006 Share Posted August 30, 2006 http://www.php.net/urlencode Quote Link to comment Share on other sites More sharing options...
madspof Posted August 30, 2006 Author Share Posted August 30, 2006 okay this is wot i got and i cannot get it to work have you got any ideas<?php getDirectory('music');// SAMPLES:// getDirectory( "." ); // Get the current directory // getDirectory( "./files/includes" ); // Get contents of the "files/includes" folder */function getDirectory( $path = 'music', $level = 0 ){ $ignore = array( 'cgi-bin', '.', '..' ); // Directories to ignore when listing output. Many hosts // will deny PHP access to the cgi-bin. $dh = @opendir( $path ); // Open the directory to the handle $dh while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory if( !in_array( $file, $ignore ) ){ // Check that this file is not to be ignored $spaces = str_repeat( ' ', ( $level * 4 ) ); // Just to add spacing to the list, to better // show the directory tree. if( is_dir( "$path/$file" ) ){ // Its a directory, so we need to keep reading down... echo "<strong>$spaces $file</strong><br />"; getDirectory( "$path/$file", ($level+1) ); // Re-call this same function but on a new directory. // this is what makes function recursive. } else { echo "<a href="mycgi?foo=', urlencode($file), target=main><strong>$spaces $file</strong></a><br />"; // Just print out the filename } } } closedir( $dh ); // Close the directory handle } ?> Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 30, 2006 Share Posted August 30, 2006 Now that wasn't that hard. If you had only done what you were told. I removed the [/url] because it has no purpose at all.I changed the str_replace to a " " instead of "" (you'd better use url_encode, but you can find thatb out for yourself).I added a <br /> to the end of the line.So here is the only line you have to replace.[code]echo "<a href=playertest.php?song=".str_replace(" ", "%20", $file)." target=main><strong>$spaces $file</strong><br />[/code]Ronald 8) Quote Link to comment Share on other sites More sharing options...
madspof Posted August 30, 2006 Author Share Posted August 30, 2006 yer that works but it some how has bypassed the whole point of this all as it will not list sub directories and i already have a script that can do what we have jsut done see:<?php$directory = "music"; $fp = opendir($directory);while ($file = readdir($fp)) { if (!ereg("^\.", $file)) { $file_url = "$directory/" . $file;?><body bgproperties="fixed" background="back%20(Custom).jpg"><div align="center"><a href="<?php echo("playertest.php?song=$file"); ?>" target="banner2"><?php echo($file); ?></a><br> <?php }}closedir($fp);?></div> Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 30, 2006 Share Posted August 30, 2006 I am sorry, but I have to quit!I sent you code that was perfectly allright. It displayed all the subdirectories and all files therein.You changed that code. And suddenly it does not function any more as intended. You can't blame me for that. So, as I said: I quit.Ronald 8) Quote Link to comment Share on other sites More sharing options...
madspof Posted August 30, 2006 Author Share Posted August 30, 2006 bare with am sry but am trying to get my head round all this and i rly wnt to try and get this to work now i entered the last correction you sent me but it is missing out hte sub directories plz can anyone help Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 30, 2006 Share Posted August 30, 2006 Now for the last time then. But I must caution you: this scripts works, but I have not sorted the directories. For the time being: if you make sure that the music directory contains no files, just sub-directories, you are fine. Here is the code:[code]<?php getDirectory('./music');// SAMPLES:// getDirectory( "." ); // Get the current directory // getDirectory( "./files/includes" ); // Get contents of the "files/includes" folder */function getDirectory( $path = '.', $level = 0 ){ $ignore = array( 'cgi-bin', '.', '..' ); // Directories to ignore when listing output. Many hosts // will deny PHP access to the cgi-bin. $dh = @opendir( $path ); // Open the directory to the handle $dh while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory if( !in_array( $file, $ignore ) ){ // Check that this file is not to be ignored $spaces = str_repeat( ' ', ( $level * 4 ) ); // Just to add spacing to the list, to better // show the directory tree. if( is_dir( "$path/$file" ) ){ // Its a directory, so we need to keep reading down... echo "<strong>$spaces"."Directory: $file</strong><br />"; getDirectory( "$path/$file", ($level+1) ); // Re-call this same function but on a new directory. // this is what makes function recursive. } else { echo "$spaces<a href=playertest.php?song=".str_replace(" ", "%20", $file)." target=main>$file</a><br />"; // Just print out the filename } } } closedir( $dh ); // Close the directory handle } ?>[/code]Good luck with it. And don't forget NOT to put any normal files in the start directory, just sub-directories with all your mp3 files.Ronald 8) 8) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.