Brownman Revival
Brownman Revival

Reputation: 3850

Getting direct path of directory which contain pages using PHP

How can I get the direct path on the folder with names containing Pages using jQuery or PHP

Expected Output is like: ../../First_Folder/First_Folder_Pages or ../../Second_Folder/Second_Folder_Pages

Upvotes: 0

Views: 76

Answers (1)

This function w'd be enough for file list. Just make an ajax call to it, json_encode to output before send back to javascript

function scan($dir)
    {
        $output = array();
        $handle = opendir($dir);

        if ($handle != false) {
            while ($file = readdir($handle)) {
                if ($file == '.' || $file == '..')
                    continue;

                $path = $dir . '/' . $file;

                if (is_dir($path))
                    $output += scan($path);
                else
                    $output[] = $path;
            }
        }

        return $output;
    }

İf you want to add folders to array too, just change the if statement like;

if (is_dir($path))
    $output += scan($path);

$output[] = $path;

Also you can use RecursiveDirectoryIterator class too: stackoverflow

UPDATE

Just erase the "else" keyword if you want to add folders too in output.

If you call function like;

scan('/root/');

The output will give you results like;

'/root/First_Folder/First_Folder_file1',
'/root/Second_Folder/Second_Folder_file1',
'/root/Second_Folder/Second_Folder_file2'

But if u erase "else" keyword, you will get results like;

'/root/First_Folder',
'/root/First_Folder/First_Folder_file1',
'/root/Second_Folder',
'/root/Second_Folder/Second_Folder_file1',
'/root/Second_Folder/Second_Folder_file2' 

Upvotes: 1

Related Questions