tags:

views:

59

answers:

4

hey guys, i wonder how i can solve the following problem: I'm reading a folder on my server and i'm displaying the contents as follows. if there's an image i'll print an image, if there's folder i'll print a div with a folder icon. i wonder if it's possible click on one of those subfolders and then display the contents of this folder the exact same way I'm currently displaying the contents of the parent folder. it should work as kind of an endless loop if there are folders inside of folders.

$path = 'files';

if (($retval = scandir($path)) !== false) {
    $retval = array_filter($retval, 'filter_files');
    shuffle($retval);
}

function filter_files($file) {
    return ($file != '.' && $file != '..' && $file != '.DS_Store' && $file != 'Thumbs.db');
}

//loop through shuffled files
foreach ($retval as $value) {
    $ext = pathinfo($value, PATHINFO_EXTENSION); //file extension
    if ($ext == "jpg" || $ext == "jpeg" || $ext == "gif" || $ext == "png") {
        print "<img class='thumb' src='$path/$value'/>";
    } else if ($ext == "") { //must be a folder
        print "<div class='thumb folder'><a href=''>link_to_subfolder</a></div>";
    } else {
        //not supported
    }
}

is that even possible?

A: 

You may want to have an AJAXified page, where if the user clicks on a folder it sends an AJAX request to the server and gets the contents of that folder and displays them. That would make the PHP simpler but you'd have to write some JavaScript for the page.

Also, you may want to look into using is_dir() to decide whether something is a directory. Testing whether there is a file extension isn't foolproof (files do not have to have extensions).

Hammerite
+1  A: 

For folder, put a GET parameter in the link:

echo '<a href="myfile.php?dir=' . $value .'">foo</a>';

Then in your script, read the $_GET['dir'] variable to know which folder to use. You would paste this variable to your $path.

Note that you must keep in mind that if you let anyone use the script, they will be able to open any folder by changing the dir parameter. E.g. if they pass ../../../../etc, they may access the servers passwd file.

Sjoerd
A: 

On your folders where you have the link set a get variable for the folder path, then if that path exists set the load path to it:

print "<div class='thumb folder'><a href='yourfile.php?path=".$value."'>link_to_subfolder</a></div>";

In file:

if(isset($_GET['path'])) $path = $_GET['path'];
else $path = 'files';

That example is very basic, you need to consider whether the user can view the folder they are requesting. For example, if I knew you just used the code above I could enter the folder path to the default password directory and view the files. You need add some validation in to check.

Ashley
awesome, thank you. can i prevent it from saying /index.php?path=name_of_folder in the adressbar and change that to just /name_of_folder … that would be the coolest thing ever. i guess that's not possible with mor_rewrite is it?
You could... if you knew that your app would only ever have file paths as the url you could simple check for anything after the domain and send that as the get 'path' parameter. If you knew you were going to have other pages being served you'd have to add a prefix, something like '/files/path to be looked at' and then send that. You're probably best off opening a new question for that
Ashley
A: 

Without going into too much detail about recursion, here's a minor edit that'll do what you want something akin to what you want (assuming your code works).

$base = 'files';

function filter_files($file) {
    return ($file != '.' && $file != '..' && $file != '.DS_Store' && $file != 'Thumbs.db');
}

function show_directory($path) {
    if (($retval = scandir($path)) !== false) {
        $retval = array_filter($retval, 'filter_files');
        shuffle($retval);
    }

    //loop through shuffled files
    foreach ($retval as $value) {
        $ext = pathinfo($value, PATHINFO_EXTENSION); //file extension
        if ($ext == "jpg" || $ext == "jpeg" || $ext == "gif" || $ext == "png") {
            print "<img class='thumb' src='$path/$value'/>";
        } else if (is_dir($path . '/' . $value)) { // *is* a folder
            print "<div class='thumb folder'><a href=''>link_to_subfolder</a>";
            show_directory($path . '/' . $value);
            print "</div>";
        } else {
            //not supported
        }
    }
}

show_directory($base);

Some reference material:

EDIT: I think I misread the question, though this is still useful information for the OP, I'm sure, so I'll leave it for now.

Matthew Scharley