Jump to content


Photo

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


  • Please log in to reply
23 replies to this topic

#1 madspof

madspof
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 30 August 2006 - 11:11 AM

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

?>

#2 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 30 August 2006 - 11:39 AM

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.
<?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 
} 
?>

Ronald  8)
RTFM is an almost extinct art form, it should be subsidized.

#3 madspof

madspof
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 30 August 2006 - 11:52 AM

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

#4 joking

joking
  • Members
  • PipPip
  • Member
  • 29 posts
  • LocationLebanon

Posted 30 August 2006 - 12:00 PM

JUST ADD THE LINK in the echo ...........

instead of
echo "<strong>$spaces $file</strong><br />";
write:
echo "<a href=playertest.php?song=$file target=banner2><strong>$spaces $file</strong></a><br />";

Just JoKinG

#5 madspof

madspof
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 30 August 2006 - 12:43 PM

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


#6 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 30 August 2006 - 12:50 PM

Well you'll have to enclose it then, msuch as:
echo "<a href='playertest.php?song=$file&target=banner2'><strong>$spaces $file</strong></a><br />";

Ronald   8)
RTFM is an almost extinct art form, it should be subsidized.

#7 joking

joking
  • Members
  • PipPip
  • Member
  • 29 posts
  • LocationLebanon

Posted 30 August 2006 - 12:53 PM

try
str_replace("", "%20", $URL);

Go to this link and view more info about this functio:

http://www.php.net/m...str-replace.php
Just JoKinG

#8 madspof

madspof
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 30 August 2006 - 01:11 PM

were would i enter that replace code


#9 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 30 August 2006 - 01:13 PM

madspof. I will cook your food, serve it on a plate, put it in your mouth, but YOU'LL HAVE TO CHEW IT!
RTFM is an almost extinct art form, it should be subsidized.

#10 madspof

madspof
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 30 August 2006 - 01:34 PM

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

#11 madspof

madspof
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 30 August 2006 - 01:49 PM

so is there any chance you chould show me i rly need to get this part of my site up

#12 joking

joking
  • Members
  • PipPip
  • Member
  • 29 posts
  • LocationLebanon

Posted 30 August 2006 - 05:03 PM

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
echo "<a href=playertest.php?song=".str_replace("", "%20", $file)." target=banner2><strong>$spaces $file</strong></a><br />";

Just JoKinG

#13 madspof

madspof
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 30 August 2006 - 09:03 PM

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


#14 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 30 August 2006 - 09:14 PM

Now we can go on like this forever and not getting anywhere.

madspof: just show all code you have sofar.

Ronald  8)
RTFM is an almost extinct art form, it should be subsidized.

#15 madspof

madspof
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 30 August 2006 - 09:24 PM

<?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
}
?>

#16 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 30 August 2006 - 09:26 PM

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


Use urldecode to get rid of the %20 and such...http://www.php.net/urldecode

#17 madspof

madspof
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 30 August 2006 - 09:31 PM

i dnt need to get rid of the %20 i need to put one in place of every space in the file name

#18 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 30 August 2006 - 09:31 PM

http://www.php.net/urlencode

#19 madspof

madspof
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 30 August 2006 - 09:41 PM

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
}
?>


#20 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 30 August 2006 - 09:47 PM

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.

echo "<a href=playertest.php?song=".str_replace(" ", "%20", $file)." target=main><strong>$spaces $file</strong><br />

Ronald   8)
RTFM is an almost extinct art form, it should be subsidized.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users