Reputation: 44323
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?
Upvotes: 0
Views: 174
Reputation: 132344
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.
Upvotes: 0
Reputation: 5947
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.
Upvotes: 0
Reputation: 75619
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.
Upvotes: 1
Reputation: 22340
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).
Upvotes: 0