Get the contents of a directory in PHP

Right now, will figure out how to get the substance of a catalogue in PHP? It can help to check if the document is now present in the index or the quantity of the substance in the registry.

PHP is the server-side scripting language that is utilized to create dynamic HTML content. While it fills this need well indeed, we can likewise utilize it to list the substance of a registry. It can valuable to check if the record is as of now present in the registry or the quantity of the substance in the index.

Right now, will figure out how to get the substance of an index in PHP. Following is the code in php:

function list_files($dir) {  
    if(is_dir($dir)) {  
        if($handle = opendir($dir))  {  
            while(($file = readdir($handle)) !== false)  
                if($file != ".") {  
                    echo '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br>'."\n";  

Right now, make a function named list_files which expects the dir as contention, which is the index to list. Inside this function, we first check on the off chance that it is a registry utilizing is_dir() technique. Assuming genuine, at that point inside we open the registry utilizing opendir() technique and allow the outcome to deal with variable, which is utilized to allude to this index.

Next, we run some time circle until there are no more records utilizing the readdir() which peruses the substance of the catalogue. Inside this, we basically check if the document isn’t equivalent to. (period) that is utilized for expansions, we can print the name of the record alongside its connection utilizing $dir.$file. After the circle is finished, we close the registry utilizing closedir() strategy.

Expectation you like the article. Offer your considerations in the remarks underneath.

Leave a Comment

error: Alert: Content is protected!!