Luckmoshy
By Luckmoshy · Published:
Last updated: · No comments
  • ARTICLE: PHP LESSON

  • How to make tree directory structure script with PHP

    How to make  tree directory structure script with PHP
     

    In this article, we'll talk about using PHP to make server file directories on a web page.

    In our system or on a live server, the directory is typically organized as a tree. Every directory should contain subfolders and files, and each file in the system has a specific path. It also has a root directory. This kind of tool, for instance, may be present in file managers that Internet Service Providers (ISPs) use to offer you a website control panel.

    If you are familiar with WordPress or other content management systems, you may see a list of the theme core files there that you can edit and save.

    Therefore, the sole goal of this article is to use PHP to list every file and folder on a web page. We will cover how to update and edit this file in the following article.

    <!doctype html>
    <html lang="en">
      <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    
        <!-- Bootstrap CSS -->
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    
        <title>How to make tree directory structure script with PHP</title>
      </head>
      <body>
        
    	<div class="container py-2">
      <div class="row m-0">
      <div class="col-md-5">
        <?php
    
        //define('SITE_URL', '');
    	
        function listFolderFiles($dir)
        {
            $allowed = array('php', 'html', 'css', 'js', 'txt');
    
            $fileFolderList = scandir($dir);
            echo '<ul class="drop" id="menu">';
            foreach ($fileFolderList as $fileFolder) {
                if ($fileFolder != '.' && $fileFolder != '..') {
                    if (!is_dir($dir.'/'.$fileFolder)) {
                        $ext = pathinfo($fileFolder, PATHINFO_EXTENSION);
                        if (in_array($ext, $allowed)) {
                            echo '<li><a href="'.SITE_URL.'/index.php?page='.ltrim($dir.'/'.$fileFolder, './').'"><i class="fas fa-file-alt"></i>'.$fileFolder.'</a>';
                        }
                    } else {
                        echo '<li><a href="#"><i class="fas fa-folder-open"></i>'.$fileFolder.'</a>';
                    }
                    if (is_dir($dir.'/'.$fileFolder)) {
                        listFolderFiles($dir.'/'.$fileFolder);
                    }
                    echo '</li>';
                }
            }
            echo '</ul>';
        }
    
        listFolderFiles('theme');
    
    
        ?>
        </div>
    
    
      </div>
    </div>
        <!-- Option 1: Bootstrap Bundle with Popper -->
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    
      </body>
    </html>
    
    • array();
    • scandir();
    • is_dir();
    • pathinfo();
    • in_array();
    array()

    An array is a unique type of variable that can store multiple values simultaneously.

    An array of files and directories from the specified directory are returned by the scandir() function.

    Simply put, the syntax of scandir(directory, order, context) is_dir() determines whether or not the specified filename is a directory.

    If the filename is a directory and exists, it returns true; otherwise, it returns false.

    Syntax
    is_dir(file) pathinfo()

    A file path, such as [dirname], is returned by the pathinfo() function as an array of information.

    [basename] and [extension] of the file
    Syntax
    pathinfo(path, options) in_array();

    The in_array() function looks up a particular value within an array.

    Syntax
    in_array(search, array, type);

    Now that the function has been created, we can generate the file directory. Simply copy and paste the code below into the file you want to create the function in, making sure to change the function parameter in my case because my theme folder is located in the root directory.

    Comments



    What to Learn

    In business and tips are here

    UFllRXRaZDJpLytvYUpFSzZoL1c3NWRTbkltV2VJeEgwMUo1Y3ZvNEU0bz01