Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy code to show folder contents on web page

    Is there a way to create a PHP page that will show the contents of a folder with-in my site without any hard coding. The contents of this folder change frequently and it is impossible to maintain if it was HTML page with links to the documents (which have name changes often as well). I would like something dynamic. I know I can do this in HTML, but I cannot stand the look of it. I would like it to look something like the user opened a folder off of her desktop. I've explored alot of avenues and keep hitting walls. Thanks!


  • #2
    New Coder
    Join Date
    May 2002
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes

    I took this directly from php.net ( http://www.php.net/manual/en/function.readdir.php )

    PHP Code:
    <?php 
    if ($handle opendir('.')) {
        while (
    false !== ($file readdir($handle))) { 
            if (
    $file != "." && $file != "..") { 
                echo 
    "$file\n"
            } 
        }
        
    closedir($handle); 
    }
    ?>
    currently it reads the current directory but you can change the directory it reads $handle = opendir('.') there change the . to the dir you want

  • #3
    New Coder
    Join Date
    Aug 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    I see how this works, but it does not return the option for links to these forms. In echence, this directory is a virtual directory of our company forms. To make these forms accisible company wide I need to have a link to this folder and I really need it to work like any other folder, with the ability to open the document and the directories contained in this folder, without having to hard code this because it changes frequently. Thanks.

    Last edited by bflo_chick; 08-23-2002 at 04:13 PM.

  • #4
    New Coder
    Join Date
    May 2002
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php 
    if ($handle opendir($dir)) {
        while (
    false !== ($file readdir($handle))) { 
            if (
    $file != "." && $file != "..") { 
                echo 
    "$file\n"
            } 
        }
        
    closedir($handle); 
    }
    ?>
    ok I made the dir in opendir a variable so you can change it every page. now the part where it is echoing out $file you can even use is_dir() and is_file() to determine whther it is a file or a directory and write links for each file and directory name. you can actually build something to traverse through directories that way and open files

  • #5
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy make it clear

    I am really impressed with your answer "Stunt Boy"

    Can you plz help me a little bit more....

    what you said in aforementioned script I could not understand it completely.

    Can you plz tell me how I can make a code which displays the contents in a folder as directories on webpage and when they are clicked they open up new page which shows files contained in these directories so they can be downloaded.

    I will be really thankful to you. plz help me I need itttttttt............

  • #6
    New Coder
    Join Date
    Jul 2008
    Posts
    91
    Thanks
    4
    Thanked 9 Times in 9 Posts
    Use some basic HTML:
    PHP Code:
    <?php  
    if ($handle opendir($dir)) { 
        while (
    false !== ($file readdir($handle))) {  
            if (
    $file != "." && $file != "..") {  
                echo 
    "<a href=\"".$file."\">".$file."</a><br />";  
            }  
        } 
        
    closedir($handle);  

    ?>

  • #7
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    sorry!

    i am sorry i am new to coding and dont know where should I enter the path (e.g. "www.google.com/downloads")to directory in this code.

  • #8
    New Coder
    Join Date
    Jul 2008
    Posts
    91
    Thanks
    4
    Thanked 9 Times in 9 Posts
    right, first of, opendir will most likely only work on local files (might be wrong though). Say you have a folder (directory) named "archives" in the same place as that file, then you edit where it states "$dir" and change it to: "archives" (remember and ADD the quotes)

    Couldnt be much harder

  • #9
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sorry for the "zombie" threat...

    I have tried this script. I am trying to make a script that will always read what is in one of my directories/folders.

    The direct link is... http://www.treeroots.ca/images/ and I have created a PHP at http://www.treeroots.ca/images/dir.php

    As you can tell, there are no results despite the fact that there are several jpeg and gif files in the "images" directory.

  • #10
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    I am thinking $dir has not been defined.

    That is some pretty antique code anyhow. Put this code in your dir.php:
    PHP Code:
    <?php

    $files 
    glob'./*.*' );

    print_r$files );
    ...any output?
    Last edited by kbluhm; 11-06-2010 at 08:28 AM.

  • #11
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    So it should look like...

    Code:
    <?php
    
    $files = glob( './*.*' );
    
    print_r( $files ); 
     
    if ($handle = opendir("images")) {  
        while (false !== ($file = readdir($handle))) {   
            if ($file != "." && $file != "..") {   
                echo "<a href=\"".$file."\">".$file."</a><br />";   
            }   
        }  
        closedir($handle);   
    }  
    ?>

    ...now I some sort of a strange string showing up??
    Last edited by Jhazon; 11-06-2010 at 09:48 PM. Reason: Result

  • #12
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    No, that will be th entire file, nothing after the `print_r( $files );` line.

    That is an array of all files within the folder. You can now loop over them with a foreach() and display them however you'd like:
    PHP Code:
    $files glob'./*.*' );

    foreach ( 
    $files as $file )
    {
        echo
            
    '<a href="./' basename$file ) . '">'
            
    basename$file ) . '</a><br />';


  • #13
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Bingo!
    Many thanks kbluhm, you are a scholar!

  • #14
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi

    I found this snippet of code while looking to do (apparently) the very same thing.
    I clipped it and pasted within the body area of a blank (index.htm) file on a folder on a site I'm setting up, where there is an access-id and pw for the folder .. That file has content as between brackets === below, but only output I get is at bottom.

    Can you tell me what I am doing wrong? Thanks

    ====
    <head>
    </head>

    <body>
    Here is the directory listing ...

    <?php

    $files = glob( './*.*' );

    foreach ( $files as $file )
    {
    echo
    '<a href="./' . basename( $file ) . '">'
    . basename( $file ) . '</a><br />';
    }

    ?>

    </body>

    ====

    What I get is between ---- brackets

    ------
    Here is the directory listing ... ' . basename( $file ) . '
    '; } ?>
    ------


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •