Jump to content

Archived

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

madspof

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

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

?>

Share this post


Link to post
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)

Share this post


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

Share this post


Link to post
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]

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
madspof. I will cook your food, serve it on a plate, put it in your mouth, but YOU'LL HAVE TO CHEW IT!

Share this post


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

Share this post


Link to post
Share on other sites
so is there any chance you chould show me i rly need to get this part of my site up

Share this post


Link to post
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]

Share this post


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

Share this post


Link to post
Share on other sites
Now we can go on like this forever and not getting anywhere.

madspof: just show all code you have sofar.

Ronald  8)

Share this post


Link to post
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
}
?>

Share this post


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

Share this post


Link to post
Share on other sites
i dnt need to get rid of the %20 i need to put one in place of every space in the file name

Share this post


Link to post
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
}
?>

Share this post


Link to post
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)

Share this post


Link to post
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>

Share this post


Link to post
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)

Share this post


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

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

×

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.