1

i have a folder with some folder names, for example 3 folders:

2012-2013, 2013-2014 ,2014-2015

is any way with some php code, to show the names folders in my php like this:

<option value="2012-2013">2012-2013</option>
<option value="2013-2014">2013-2014</option>
<option value="2014-2015">2014-2015</option>

untill I use to show the stuff with php-foreach using a html-template inside the folders. but i want to show direct the stuff without using the template in the data-folder, is any way? thx.

2 Answers 2

2

I suppose you want to recursive the folder in a directory. Below please find the code.

<?php
$folder_name = "c:\\your_folder\\";
$folders = scandir($folder_name);
echo '<select>';
foreach($folders as $folder){
    if (is_dir($folder_name . $folder)){
        if ($folder != '.' && $folder != '..')
            echo '<option value="' . $folder . '">' . $folder . '</option>';
    }
}
echo '</select>';
?>
Sign up to request clarification or add additional context in comments.

5 Comments

there is a problem in if (is_dir($folder_name . $folder))} if ($folder != '.' && $folder != '..')} f i remove this, its working.
eh, it's weird. Anyway, if you gotten it run on your site, then just remove it.
its working but with with ".." and "." can i remove only this? from the command "scandir" ? if i can do that, then its ok.
That's why if ($folder != '.' && $folder != '..') is to remove them. Perhaps, you can try ($folder == '.'){}else if ($folder == '..'){} else{ echo '<option value="' . $folder . '">' . $folder . '</option>'; }
I replace with "$folders = array_diff(scandir($folder_name), array('.', '..', '.DS_Store'));" and now its working.
0

You need to read the directory and print an option tag for each time you find a file that is a folder. first thing you need to open file handler to your directory by giving the main folder path, than itterate over the directory by using the read method on the folder handler. By using the "is_dir" function you can determine if the file is a folder, and if so print the file. I added my solution which is recursive.

function printFolders($path = "", $c = 0) {

    if ( empty($path) || !is_dir($path) )
    {
        return false;
    }

    //Folder handler
    $handler = dir($path);

    //Read each file name inside the directory
    while(($file = $handler->read()) !== false)
    {

        // "." is the current folder and ".." is the parent folder
        // We skip those folders
        if ( $file == "." || $file == ".." )
        {
            continue;
        }

        // The current file path
        $filePath = $path . "/" . $file;

        if ( is_dir($filePath) )
        {
            //Just to make things more pretty
            for($i=0; $i<=$c; $i++) {echo "-";}
            //Printing the folder name
            echo $file . "<br>";
            //Calling the function again with the folder we found
            printFolders($filePath, $c+1);
        }
    }
}

printFolders("path/to/folder");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.