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.
Related
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();
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
<?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++
}
I have a script file. that list files and folder in a directory.. I want to hide certain files and folder. How do I do that?
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if (($file != ".")
&& ($file != ".."))
{
$thelist .= '<LI>'.$file.'';
}
}
closedir($handle);
}
?>
<P>List of files:</p>
<UL>
<P><?=$thelist?></p>
</UL>
<?php
$files_to_hide = array('file1.txt', 'file2.txt');
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if (($file != ".") && ($file != "..") && !in_array($file, $files_to_hide))
{
$thelist .= '<LI>'.$file.'';
}
}
closedir($handle);
}
?>
<P>List of files:</p>
<UL>
<P><?=$thelist?></p>
</UL>
Put the list of filenames you want to exclude in an array.
After that, you check whether the filename exists in the array before adding it to $thelist.
You can add that as part of the if() statement that checks if the filename is . or ...
Something like this:
<?php
$bannedFiles = Array(".", "..", "example");
if ($handle = opendir('.')){
while (false !== ($file = readdir($handle)))
{
$banned = false;
foreach ($bannedFiles as $bFile){
if ($bFile == $file){
$banned = true;
}
}
if (!$banned){
$thelist .= '<LI>'.$file.'</LI>';
}
}
closedir($handle);
}
?>
<P>List of files:</p>
<UL>
<P><? echo $thelist;?></p>
</UL>
If you know the names of the files/directories you want to hide, you can maintain a set-map of such entries, and filter them out within the while loop.
Your set-map would look like this:
$items_to_hide = [ "/home/me/top_secret" => 1, "/home/me/passwords.txt" => 1, ... ]
And then you would modfiy your while loop like this:
while (false !== ($file = readdir($handle)))
{
// check map if said file is supposed to be hidden, if so skip current loop iteration
if($items_to_hide[$file]) {
continue;
}
if (($file != ".")
&& ($file != ".."))
{
$thelist .= '<LI>'.$file.'';
}
}
Hope this helps.
EDIT:
Also wanted to mention, using a php ordered array as your "blacklist" is quite efficient, as a single lookup will occur in almost constant time. Hence you can grow your blacklist as large as you want and still see decent performance.
I am very much a beginner when it comes to using PHP. I was given this code, to try and output the contents of a files on a folder, onto a server, but my issue is I do not know how to read and alter this code to fit my specific file path. Can someone help me out with this, and lets just use the name folder as an arbitrary pathname.
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
<?php
$dir_path = '.'; // '.' = current directory.
// '..' = parent directory.
// '/foo' = directory foo in the root file system
// 'folder' = a dir called 'folder' inside the current dir
// 'a/b' = folder 'b' inside 'a' inside the current dir
// '../a' = folder 'a' inside the parent directory
if ($handle = opendir($dir_path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
<?php
$path = '.';
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
Detailed explanation and examples: http://www.php.net/function.opendir