Help - Search - Members - Calendar
Full Version: Automatic Link Generation Through Directory Scan
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
past-papers
Ok so I have many many files hosted in my server space.

For example, I have a DIR called Maths, which in itself contains further sub DIR's and these also contain other DIR's and files (PDF).

What would I use on my web page to list all these files with links to each file instead of having to add a link to each file by HTML.

So then even if I were to add more files, the web page would automatically change to give a link to this new file.

e.g. If I had a file located at http://keffler.uuuq.com/Maths/2005%20Edexc...2%Mock%20MS.pdf

then I would like it to show up on my web page as S2 Mock MS which is the name of the file. Ofcourse this would be in a list with all the other files there too.

Thanks a lot.

Jetteh22
http://www.laughing-buddha.net/jon/php/dirlist/

Not sure if that's what you're looking for or not.

I think that just reads what is in a single directory, I don't think it delves into the sub-directories too. I generally use mysql for multiple file handling so I don't know much about this.
past-papers
QUOTE(Jetteh22 @ Dec 13 2008, 11:48 PM) *
http://www.laughing-buddha.net/jon/php/dirlist/

Not sure if that's what you're looking for or not.

I think that just reads what is in a single directory, I don't think it delves into the sub-directories too. I generally use mysql for multiple file handling so I don't know much about this.

Yes but doing that gives http://www.keffler.uuuq.com/Downloads.html

Sorry if I've done it wrong. I have no idea how to do this kind of stuff and it's my first time.

Thanks
Jetteh22
You have to name it downloads.php, not .html
past-papers
It still does nothing sad.gif
past-papers
Ok so I'm learning somewhat quickly here smile.gif

And have sorted what code to use.

The only thing is, how can I get it to scan sub-directories??

Because the code I'm using is

CODE
<?

//define the path as relative
$path = "Maths";

//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");

echo "Directory Listing of $path<br/>";

//running the while loop
while ($file = readdir($dir_handle))
{
   echo "<a href='$file'>$file</a><br/>";
}

//closing the directory
closedir($dir_handle);

?>


and this produces the following: http://www.keffler.uuuq.com/Downloads.php

and as can be seen, when clicking a sub-directory, it won't open it!!!

**Edit by Trippin7464 - Please use [code] tags for any code. Thanks!
Andrew
Put this code in the files directory. It will list all files in that directory and also will list all files in the sub-directories as Category: sub-directory
Link

CODE
<?php
// grab a full file listing from the current and sub directories and show them as anchored
//(linked) files.

// pull a full file listing - requires the Unix 'find' and 'sort commands. 'find' will retrieve a
//list of all files from the current directory, 'sort' will sort the listing, and 'explode' will split
//all files into an array passed into $filelist.

$filelist = explode("\n",`find .|sort`);

// for each item (file) in the array...

for ($count=0;$count<count($filelist);$count++) {

// get the filename (including preceding directory, ie: ./software/gth1.0.9.tar.gz)

$filename=$filelist[$count];

// if it's not a directory, display linked

if (!is_dir($filename))
printf("<a href=\"%s\">%s</a><br>\n",$filename,$filename);

// otherwise tell the user it's a "category"

else printf("<p>Category: %s<p>\n",$filename);
}

?>


Source: http://www.weberdev.com/get_example-1456.html
past-papers
QUOTE(Trippin7464 @ Dec 14 2008, 03:07 AM) *
Put this code in the files directory. It will list all files in that directory and also will list all files in the sub-directories as Category: sub-directory
Link

CODE
<?php
// grab a full file listing from the current and sub directories and show them as anchored
//(linked) files.

// pull a full file listing - requires the Unix 'find' and 'sort commands. 'find' will retrieve a
//list of all files from the current directory, 'sort' will sort the listing, and 'explode' will split
//all files into an array passed into $filelist.

$filelist = explode("\n",`find .|sort`);

// for each item (file) in the array...

for ($count=0;$count<count($filelist);$count++) {

// get the filename (including preceding directory, ie: ./software/gth1.0.9.tar.gz)

$filename=$filelist[$count];

// if it's not a directory, display linked

if (!is_dir($filename))
printf("<a href=\"%s\">%s</a><br>\n",$filename,$filename);

// otherwise tell the user it's a "category"

else printf("<p>Category: %s<p>\n",$filename);
}

?>


Source: http://www.weberdev.com/get_example-1456.html

Yes I know about that site. Even tried the code out however, it seems that zymic doesn't allow one of the functions on the script to run:

and thus I get the message:

QUOTE
Warning: shell_exec() has been disabled for security reasons in /www/uuuq.com/k/e/f/keffler/htdocs/Downloads.php on line 9


Hence my posting of the problem in here.

Thanks

Sohan
Andrew
I just made a very crappppy script, it works but it's not pretty. I pieced it together from php.net by reading up on some things and using code people have posted in comments. I take no credit, but I read so many different comments I don't know who's code I used.

CODE
<?php
function scanDirectories($rootDir, $allData=array()) {
    // set filenames invisible if you want
    $invisibleFileNames = array(".", "..", ".htaccess", ".htpasswd");
    // run through content of root directory
    $dirContent = scandir($rootDir);
    foreach($dirContent as $key => $content) {
        // filter all files not accessible
        $path = $rootDir.'/'.$content;
        if(!in_array($content, $invisibleFileNames)) {
            // if content is file & readable, add to array
            if(is_file($path) && is_readable($path)) {
                // save file name with path
                $allData[] = $path;
            // if content is a directory and readable, add path and name
            }elseif(is_dir($path) && is_readable($path)) {
                // recursive callback to open new directory
                $allData = scanDirectories($path, $allData);
            }
        }
    }
    return $allData;
}

function print_ar($array, $count=0) {
    foreach($array as $key=>$value){
        if(is_array($value)){
            echo "[<strong><u>$key</u></strong>]<br />";
            $count++;
            print_ar($value, $count);
            $count--;
        }
        else{
            $tab2 = substr($tab, 0, -12);
            echo "$tab2 <a href='/$value'>$value</a><br />";
        }
        $k++;
    }
    $count--;
}

print_ar(scanDirectories("test"));
?>


Change the last line from test to whatever directory your files are in
past-papers
QUOTE(Trippin7464 @ Dec 14 2008, 04:27 AM) *
I just made a very crappppy script, it works but it's not pretty. I pieced it together from php.net by reading up on some things and using code people have posted in comments. I take no credit, but I read so many different comments I don't know who's code I used.

CODE
<?php
function scanDirectories($rootDir, $allData=array()) {
    // set filenames invisible if you want
    $invisibleFileNames = array(".", "..", ".htaccess", ".htpasswd");
    // run through content of root directory
    $dirContent = scandir($rootDir);
    foreach($dirContent as $key => $content) {
        // filter all files not accessible
        $path = $rootDir.'/'.$content;
        if(!in_array($content, $invisibleFileNames)) {
            // if content is file & readable, add to array
            if(is_file($path) && is_readable($path)) {
                // save file name with path
                $allData[] = $path;
            // if content is a directory and readable, add path and name
            }elseif(is_dir($path) && is_readable($path)) {
                // recursive callback to open new directory
                $allData = scanDirectories($path, $allData);
            }
        }
    }
    return $allData;
}

function print_ar($array, $count=0) {
    foreach($array as $key=>$value){
        if(is_array($value)){
            echo "[<strong><u>$key</u></strong>]<br />";
            $count++;
            print_ar($value, $count);
            $count--;
        }
        else{
            $tab2 = substr($tab, 0, -12);
            echo "$tab2 <a href='/$value'>$value</a><br />";
        }
        $k++;
    }
    $count--;
}

print_ar(scanDirectories("test"));
?>


Change the last line from test to whatever directory your files are in

Works a treat.

It's exactly how I wanted it.

I thank you so so much.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.