PDA

View Full Version : code to show folder contents on web page


bflo_chick
08-22-2002, 09:27 PM
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!

:)

stuntboy
08-22-2002, 10:03 PM
yes :)

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


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

bflo_chick
08-23-2002, 01:49 PM
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.

:confused:

stuntboy
08-23-2002, 06:49 PM
<?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

sohaib
07-23-2008, 08:12 PM
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............

scoop_987
07-23-2008, 09:06 PM
Use some basic HTML:

<?php
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "<a href=\"".$file."\">".$file."</a><br />";
}
}
closedir($handle);
}
?>

sohaib
07-24-2008, 05:41 PM
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.

scoop_987
07-24-2008, 09:13 PM
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

Jhazon
11-06-2010, 12:39 AM
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.

kbluhm
11-06-2010, 06:42 AM
I am thinking $dir has not been defined.

That is some pretty antique code anyhow. Put this code in your dir.php:

<?php

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

print_r( $files );

...any output?

Jhazon
11-06-2010, 08:46 PM
So it should look like...

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

kbluhm
11-06-2010, 10:57 PM
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:

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

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

Jhazon
11-06-2010, 11:25 PM
Bingo!
Many thanks kbluhm, you are a scholar!

gist4me
03-27-2011, 02:39 PM
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 ) . '
'; } ?>
------