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

?>
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)
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]
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]
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
<?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
}
?>
[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
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
}
?>
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)
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>
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)
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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.