Display Root Directory Via Php

Discussion in 'PHP' started by batgirl_sa, May 2, 2007.

  1. #1
    hello,
    i have a space where we discuss and upload files many times a day.Its quite hard for me to update html page with links to all files soo i currently deleted index.html so all can view the files on server is there anyway an html or PHP file show up files on refresh because it is quite odd to look at files from the root


    HELP!!!!! !


    thanks
     
    batgirl_sa, May 2, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    <?php
    $handle = opendir( 'directorywithfiles' );
    while( $file = readdir( $handle ) ):
    if( $file != "." and $file != ".." )
    {
    printf( 'Filename : %s<br />', $file );
    }
    endwhile;
    closedir( $handle );
    
    PHP:
     
    krakjoe, May 2, 2007 IP
  3. pepsipunk

    pepsipunk Well-Known Member

    Messages:
    208
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #3
    pepsipunk, May 2, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    You asked me a question via pm, which I'll answer here for everyones enjoyment.

    You asked if you could block certain files from being shown, there are a few decent ways of doing this, here they are :

    Way 1 :
    we'll create an array of disallowed files and check them as we loop over directory contents
    
    <?
    $disallowed = array
    (
    	'secret.php',
    	'mine.php',
    	'noshow.php'
    );
    $handle = opendir( "I:/xampp/xampp/htdocs" );
    while( $file = readdir( $handle )):
    	if(!in_array( $file, $disallowed ) and $file != "." and $file != ".." and !is_dir( $file ) )
    	{
    		printf( "Filename : %s<br />\n", $file );
    	}
    endwhile;
    closedir($handle);
    ?>
    
    PHP:
    Way 2 :
    We'll match the filenames with a pattern
    
    <?
    $pattern = '#\.html$#si';
    $handle = opendir( "I:/xampp/xampp/htdocs" );
    while( $file = readdir( $handle )):
    	if( preg_match( $pattern, $file ) and $file != "." and $file != ".." and !is_dir( $file ) )
    	{
    		printf( "Filename : %s<br />\n", $file );
    	}
    endwhile;
    ?>
    
    PHP:
    Way 3 :
    We'll match the filenames by an extension
    
    <?
    $ext = ".php";
    $handle = opendir( "I:/xampp/xampp/htdocs" );
    while( $file = readdir( $handle )):
    	if( substr( $file, strlen( $file ) - strlen( $ext ) ) == $ext and $file != "." and $file != ".." and !is_dir( $file ) )
    	{
    		printf( "Filename : %s<br />\n", $file );
    	}
    endwhile;
    
    ?>
    
    PHP:
    There are probably more but one of those is bound to suit your purpose, all these example do not read directories as files
     
    krakjoe, May 2, 2007 IP