List all folders in directory (PHP) [duplicate] - php

This question already has answers here:
PHP Get all subdirectories of a given directory
(16 answers)
Closed 5 years ago.
How can I make my code only display links to the folders and not the files in the directory?
$d = dir(".");
echo "<ul>";
while(false !== ($entry = $d->read())) {
echo "<li><a href='{$entry}'>{$entry}</a></li>";
}
echo "</ul>";
$d->close();

$d = dir(".");
echo "<ul>";
while (false !== ($entry = $d->read()))
{
if (is_dir($entry) && ($entry != '.') && ($entry != '..'))
echo "<li><a href='{$entry}'>{$entry}</a></li>";
}
echo "</ul>";
$d->close();

You should just be able to wrap your current code with a call to is_dir:
while(false !== ($entry = $d->read())) {
if (is_dir($entry)) {
echo "<li><a href='{$entry}'>{$entry}</a></li>";
}
}
If you want to remove the "dot" directories (. and ..), use the following:
if (is_dir($entry) && !in_array($entry, ['.', '..'])) {
...

Just check if the $entry is a directory:
$d = dir(".");
echo "<ul>";
while(false !== ($entry = $d->read())) {
if(is_dir($entry))
echo "<li><a href='{$entry}'>{$entry}</a></li>";
}
echo "</ul>";
$d->close();

Related

How do I sort pictures alphabetically in gallery?

I have this php gallery and I need to sort pictures alphabetically. How can I do that? Thank you.
<?php
$dir = htmlspecialchars($_GET["dir"]);
$className = htmlspecialchars($_GET["className"]);
if ($handle = opendir($dir)) {
echo "<div class='photogallery $className'><ul>";
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && preg_match('/(jpg)|(JPG)$/', $entry) == 1) {
$path = $dir."/".$entry;
$pathThumb = $dir."/thumbs/".$entry;
echo "<li><a href='/$path' data-lightbox='gallery1' data-title=''><img src='/$pathThumb' alt=''></a>
</li>";
}
}
closedir($handle);
echo "</ul><div class='both'></div></div>";
}
?>
glob returns the directory sorted unless you specify otherwise.
echo "<div class='photogallery $className'><ul>";
foreach (glob($dir . "/*.*") as $entry) {
if ($entry != "." && $entry != ".." && preg_match('/(jpg)|(JPG)$/', $entry) == 1) {
$path = $dir . "/" . $entry;
$pathThumb = $dir . "/thumbs/" . $entry;
echo "<li><a href='/$path' data-lightbox='gallery1' data-title=''><img src='/$pathThumb' alt=''></a>
</li>";
}
}
echo "</ul><div class='both'></div></div>";

unlink a file in a lower directory

I am trying to find a solution to remove files from a directory listing. I found an example that works bery well for files within the same directory. However I assume that I would need to change directories but I am not understanding how to accomplish this.
Here is the example that works for files in the current directory
<?php
if(isset($_GET['delete'])){
$delurl=$_GET['delete'];
unlink($delurl);
}
?>
<?php
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<br><b>$entry - Delete<br></b>";
}
}
closedir($handle);
}
?>
Ok got it.
<?php
if(isset($_GET['delete'])){
$delurl=$_GET['delete'];
unlink($delurl);
}
?>
<?php
if ($handle = opendir('../videos/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<br><b>$entry - Delete<br></b>";
}
}
closedir($handle);
}
?>
I just added the path after href=\?delete= and before $entry

how can i add child node to the parents nodes in php?

I am reading a rootpath and listing all the folder. I have to add some child node to every folder programatically ?
<?php
$rootpath = 'D:/Storage/';
if ($handle = opendir($rootpath)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<li><a href=/hi.php?dev=$entry&action=viewcam>$entry</a>";
}
}
closedir($handle);
?>
I am getting like
- folder1
- folder2
but I want like
- folder1
Appple
orange
- folder2
Appple
orange
Any suggestion ?
There is no need to specifically "create a node" on the server side for this. All you have to do is output the html markup you want to have:
<?php
$rootpath = 'D:/Storage/';
if ($handle = opendir($rootpath)) {
echo "<ul>\n";
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<li>$entry</li>\n";
echo "<ul>\n";
foreach (['Apple', 'orange'] as $fruit) {
echo "<li>".$fruit."</li>\n";
}
echo "</ul>\n";
}
}
echo "</ul>\n";
}
closedir($handle);
?>
(I fixed a few minor issues with your code on-the-fly...)
Obviously this is just an example to show the basic approach.

How to change the style of a few echoed pieces of text?

<?php
$blacklist = array("no.html", "one.php");
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
echo "<a href='$entry'><li id='file'>$entry\n</li></a>";
}
}
closedir($handle);
}
?>
What happens here is that every file in the directory (excluding those in the blacklist) get echoed out like so:
1.html
2.html
3.html
4.html
5.html
6.html
7.html
8.html
However, is there any way to style certain elements? For example, every 3 echo's, make the text bigger and the color red.
E.g.
1.html
2.html
3.html
4.html
5.html
6.html
7.html
8.html
Try this. I think it will help you
$i = 1;
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
if($i%3 == 0){
echo "<a href='$entry'><strong><li id='file'>$entry</li></strong></a>";
}else{
echo "<a href='$entry'><li id='file'>$entry</li></a>";
}
}
$i++;
}
Something like this with the counter initialized will do the job.
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
if ($i%3==0)
{
//the echo with the style
}
else
{
echo "<a href='$entry'><li id='file'>$entry\n</li></a>";
}
}
$i++
}

No such directory while using a variable string in url (PHP)

Well, the title is probably not very helpful. I'm not sure how to describe this error. I'm sure it's caused by something stupid on my part. For the life of me though, I can't figure it out.
Here's what I'm trying to do: There's a list of folders in my directory "img/gallery"
Within each of those folders are images
There's also a folder called "img/thumbs" which is structured exactly the same but with thumbnail images
I am writing a script that does two things: it displays the names of the folders within "img/gallery" and displays a list of thumbnails with links to the images in that folder.
These two components, when run separately, work fine. Put together, I get an error at
if ($handle = opendir('img/gallery/$file/'))
saying that $file is not a valid directory.
Here's the code:
`
if ($handle = opendir('img/gallery/')) {
$blacklist = array('.', '..', 'somedir', 'somefile.php');
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $blacklist)) {
echo "<h2>$file</h2>\n";
}
}
closedir($handle);
}
echo "<P class='gallery'>\n";
if ($handle = opendir('img/gallery/$file/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<a class='fancybox' href='img/gallery/$file/$entry' data-fancybox-group='gallery'>\n<img src='img/thumbs/$file/$entry' alt='' /></a>\n";
}
}
echo "</p>\n";
closedir($handle);
}
`
And advice would be wonderfully helpful. Thanks!
opendir should return true or false but it may not be playing nicely with the if statement Try this:
$handle = opendir('img/gallery/'.$file.'/');
if ($handle) {
Would also be good to var_dump out the $file value to make sure it is correct.
Surely you will need to nest the second while loop in the first if you are to iterate over each file in each folder...?
Did you try quotes for variables? I always quote out my variables, just good practice.
if ($handle = opendir('img/gallery/')) {
$blacklist = array('.', '..', 'somedir', 'somefile.php');
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $blacklist)) {
echo "<h2>$file</h2>\n";
}
}
closedir($handle);
}
echo "<P class='gallery'>\n";
if ($handle = opendir('img/gallery/' . $file . '/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<a class='fancybox' href='img/gallery/" . $file . "/" . $entry . "' data-fancybox-group='gallery'>\n<img src='img/thumbs/" . $file . "/" . $entry . "' alt='' /></a>\n";
}
}
echo "</p>\n";
closedir($handle);
}

Categories