Jump to content

can anyone help me edit this php to make it search sub folders aswell


madspof

Recommended Posts

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);

?>
Link to comment
Share on other sites

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( '&nbsp;', ( $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)
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

<?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( '&nbsp;', ( $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
}
?>
Link to comment
Share on other sites

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( '&nbsp;', ( $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
}
?>
Link to comment
Share on other sites

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 &lt;br /&gt; 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)
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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)
Link to comment
Share on other sites

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( '&nbsp;', ( $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)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.