PHP File Tree Script Help

They have: 68 posts

Joined: Jun 2006

Hey all,

I'm trying to customize a script called PHP File Tree. The documentation is here:
http://abeautifulsite.net/notebook.php?article=21

In that documentation, I think it's explained how to make it so when clicking on a file it actually opens the files -- but I'm having trouble with that part.

Here's my code related to that area:

<?php
echo php_file_tree($_SERVER['DOCUMENT_ROOT'].\"/downloadpdfs/\", \"http://www.foothillsbaptist.org/?file=[link]/\");
?>

But when I test that, it gives me the following links:
http://www.foothillsbaptist.org/?file=/home/USER/public_html/downloadpdfs/FILE.EXT/

I can't see to fix this.... any help would be appreciated.

Thanks.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I clicked the link you gave to your site, and am not too sure which links you are having problems with

I tried a few of them and they successfully open the PDF file in my browser
Can you say which links exactly you are having difficulty with

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Hmmm, as with Greg, when clicking on a file my browser it seemed to behave as normal.

If you want to force the PDF to open on the client machine then you're not going to have much luck I'm afraid. Whether the file is opened or saved is up to the user. Smiling

a Padded Cell our articles site!

They have: 68 posts

Joined: Jun 2006

Sorry, I probably didn't go into enough detail. Here's the test page I'm working with:

http://www.foothillsbaptist.org/php_file_tree/test.php

When I click the actual files, I simply get an Apache Directory. And when I look at the links attached, it includes the root on the end of the link.

Here's my code:

<?php
// PHP File Tree Demo
// For documentation and updates, visit <a href="http://abeautifulsite.net/notebook.php?article=21
//" title="http://abeautifulsite.net/notebook.php?article=21
//">http://abeautifulsite.net/notebook.php?article=21
//</a> Main function file
include(\"php_file_tree.php\");


<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">

<html xmlns=\"http://www.w3.org/1999/xhtml\">
    <head>
        <title>PHP File Tree Demo</title>
        <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />
        <link href=\"styles/default/default.css\" rel=\"stylesheet\" type=\"text/css\" media=\"screen\" />
       
        <!-- Makes the file tree(s) expand/collapsae dynamically -->
        &lt;script src=\"php_file_tree.js\" type=\"text/javascript\"&gt;&lt;/script&gt;
    </head>

       
       
        // This links the user to <a href="
http://example.com/?file=filename.ext
" title="http://example.com/?file=filename.ext
">http://example.com/?file=filename.ext
</a>        echo php_file_tree(
$_SERVER['DOCUMENT_ROOT'].\"/downloadpdfs/\", \"http://www.foothillsbaptist.org/downloadpdfs/?file=[link]\");

        // This links the user to <a href="
http://example.com/?file=filename.ext" title="http://example.com/?file=filename.ext">http://example.com/?file=filename.ext</a> and only shows image files
        //$allowed_extensions = array(\"gif\", \"jpg\", \"jpeg\", \"png\");
        //echo php_file_tree($_SERVER['DOCUMENT_ROOT'], \"http://example.com/?file=[link]/\", $allowed_extensions);
       
        // This displays a JavaScript alert stating which file the user clicked on
        //echo php_file_tree(\"downloadpdfs/\", \"http://www.foothillsbaptist.org/downloadpdfs/?file=[link]/\";, $allowed_extensions);
       
       
       
   
</body>
   
</
html>
?>

Thanks for your help

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I see what you mean now!

This code you have (from your post above)
echo php_file_tree($_SERVER['DOCUMENT_ROOT']."/downloadpdfs/", "http://www.foothillsbaptist.org/downloadpdfs/?file=[link]");

Makes a link like this:

http://www.foothillsbaptist.org/downloadpdfs/?file=/home/footh4/public_html/downloadpdfs/general/giving07.pdf'
now my browser wont really know what to do with that. It can't take the "file=..." and display the PDF file from it
It will only do that from a hard link, for example this is what you want your final link to end up looking like

http://www.foothillsbaptist.org/downloadpdfs/general/giving07.pdf'
And if you copy and paste that link into your browsers URL it will load the PDF file - if your browser has PDF viewer built in
Otherwise it wont know what to do with it and ask if you want to open it with another program or save it to your PC
(This is the normal/correct behaviour)

But the way that link is made is obviously in the include file

So sould you show us the code for this file:
php_file_tree.php

They have: 68 posts

Joined: Jun 2006

Here's the include file...

<?php
/*
   
    == PHP FILE TREE ==
   
        Let's call it...oh, say...version 1?
   
    == AUTHOR ==
   
        Cory S.N. LaViska
        <a href="http://abeautifulsite.net/
" title="http://abeautifulsite.net/
">http://abeautifulsite.net/
</a>       
    == DOCUMENTATION ==
   
        For documentation and updates, visit <a href="http://abeautifulsite.net/notebook.php?article=21
" title="http://abeautifulsite.net/notebook.php?article=21
">http://abeautifulsite.net/notebook.php?article=21
</a>       
*/


function php_file_tree($directory, $return_link, $extensions = array()) {
   
// Generates a valid XHTML list of all directories, sub-directories, and files in $directory
    // Remove trailing slash
   
if( substr($directory, -1) == \"/\" ) $directory = substr($directory, 0, strlen($directory) - 1);
   
$code .= php_file_tree_dir($directory, $return_link, $extensions);
    return
$code;
}

function php_file_tree_dir(
$directory, $return_link, $extensions = array(), $first_call = true) {
    // Recursive function called by php_file_tree() to list directories/files
   
    // Get and sort directories/files
    if( function_exists(\"scandir\") )
$file = scandir($directory); else $file = php4_scandir($directory);
    natcasesort(
$file);
    // Make directories first
   
$files = $dirs = array();
    foreach(
$file as $this_file) {
        if( is_dir(\"
$directory/$this_file\" ) ) $dirs[] = $this_file; else $files[] = $this_file;
    }
   
$file = array_merge($dirs, $files);
   
    // Filter unwanted extensions
    if( !empty(
$extensions) ) {
        foreach( array_keys(
$file) as $key ) {
            if( !is_dir(\"
$directory/$file[$key]\") ) {
               
$ext = substr($file[$key], strrpos($file[$key], \".\") + 1);
                if( !in_array(
$ext, $extensions) ) unset($file[$key]);
            }
        }
    }
   
    if( count(
$file) > 2 ) { // Use 2 instead of 0 to account for . and .. \"directories\"
       
$php_file_tree = \"<ul\";
        if(
$first_call ) { $php_file_tree .= \" class=\\"php-file-tree\\"\"; $first_call = false; }
       
$php_file_tree .= \">\";
        foreach(
$file as $this_file ) {
            if(
$this_file != \".\" && $this_file != \"..\" ) {
                if( is_dir(\"
$directory/$this_file\") ) {
                    // Directory
                   
$php_file_tree .= \"<li class=\\"pft-directory\\"><a href=\\"#\\">\" . htmlspecialchars($this_file) . \"</a>\";
                   
$php_file_tree .= php_file_tree_dir(\"$directory/$this_file\", $return_link ,$extensions, false);
                   
$php_file_tree .= \"</li>\";
                } else {
                    // File
                    // Get extension (prepend 'ext-' to prevent invalid classes from extensions that begin with numbers)
                   
$ext = \"ext-\" . substr($this_file, strrpos($this_file, \".\") + 1);
                   
$link = str_replace(\"[link]\", \"$directory/\" . urlencode($this_file), $return_link);
                   
$php_file_tree .= \"<li class=\\"pft-file \" . strtolower($ext) . \"\\"><a href=\\"$link\\">\" . htmlspecialchars($this_file) . \"</a></li>\";
                }
            }
        }
       
$php_file_tree .= \"</ul>\";
    }
    return
$php_file_tree;
}

// For PHP4 compatibility
function php4_scandir(
$dir) {
   
$dh  = opendir($dir);
    while( false !== (
$filename = readdir($dh)) ) {
       
$files[] = $filename;
    }
    sort(
$files);
    return(
$files);
}
?>

Thanks for your help!

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I don't have time tonight unfortunately to have a good look for you (someone else might reply for you before tomorrow)
But at first glance, for the end result you are trying to accomplish that seems like an awful lot of PHP code
The Javascript I can understand as it displays folder lists/file tree views - although that of course can be done alternatively with simple href images.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.