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 will discuss, how to create server file directories on the web page using PHP.

    Generally, the directory is structured in the form of a tree in our system or in a live server. It has a root directory, every directory should have sub folders and files and every file in the system has a unique path. For example, you may have seen this type of tool as part of file managers that Internet Service Providers (ISPs) use when providing you a website control panel.

    If you are aware of WordPress or other cms there is an option for theme editing and you may see there a list of theme core files that you can edit and save it.

    So, the only purpose of this article is to list all files and folders on a web page using PHP. in the further article we will discuss how to edit this file and update it.

    <!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 special variable, which can hold multiple values at a time.

    The scandir() function returns an array of files and directories of the specified directory.

    Syntax:
    scandir(directory, order, context) is_dir();

    Simply it checks whether the specified filename is a directory or not.

    Returns true if the filename exists and is a directory, false otherwise.

    Syntax
    is_dir(file) pathinfo()

    The pathinfo() function returns information array about a file path like [dirname],

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

    The in_array() function searches an array for a specific value.

    Syntax
    in_array(search, array, type);

    Now we will create a function for generating the file directory just copy-paste the below code in your file where you want to create it and be sure to change the function parameter in my case my theme folder is in the root directory.




    Comments



    UFllRXRaZDJpLytvYUpFSzZoL1c3NWRTbkltV2VJeEgwMUo1Y3ZvNEU0bz01