Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to list files in a directory with no subdirectories?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 82
    Comment on it

    In PHP, we can list files with no sub-directories. To understand this we will take a example. Suppose we have a directory demo whose files we want to list inside a drop down box. We will take a form then open the directory whose files we want to display by using the function opendir(). Then we display all the files inside drop down. To do this you need to use the following script:

    <? 
        echo ""; 
    //Looks into the directory and returns the files, no subdirectories 
        echo "<select name='yourfiles'>"; 
    //The path to the style directory 
        $dirpath = "demo"; 
        $dh = opendir($dirpath); 
           while (false !== ($file = readdir($dh))) { 
    //Don't list subdirectories 
              if (!is_dir("$dirpath/$file")) { 
    //Truncate the file extension and capitalize the first letter 
               echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>'; 
    
       } 
    } 
    closedir($dh); 
    //Close Select 
    echo "</select>"; 
    echo "</form>"; 
    ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: