Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3490074
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:28:19+00:00 2026-05-18T11:28:19+00:00

I have created this php script which displays the contents of a designated directory

  • 0

I have created this php script which displays the contents of a designated directory and allows users to download each file. Here is the code:

<?php  
if ($handle = opendir('test')) {  
     while (false !== ($file = readdir($handle))) {  
        if ($file != "." && $file != "..") {  
            echo "<a href='test/$file'>$file\n</a><br/>";  
        }  
    }  
    closedir($handle);  
}
?>

This script also displays folders, but when I click a folder, it does display the contents of the folder, but in the default Apache autoindex view.

What I would like the script to do when a folder is clicked, is display the contents, but in the same fashion as the original script does (as this is more editable with css and the like).
Would you know how to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:28:20+00:00Added an answer on May 18, 2026 at 11:28 am

    Don’t create a link to the directory itself, but to a php page which displays the contents.

    Change your php code to somthing like:

    if(isset($_REQUEST['dir'])) {
        $current_dir = $_REQUEST['dir'];
    } else {
        $current_dir = 'test';
    }
    
    if ($handle = opendir($current_dir)) {
         while (false !== ($file_or_dir = readdir($handle))) {
            if(in_array($file_or_dir, array('.', '..'))) continue;
            $path = $current_dir.'/'.$file_or_dir;
            if(is_file($path)) {
                echo '<a href="/Implementation/'.$path.'">'.$file_or_dir."\n</a><br/>";
            } else {
                echo '<a href="script.php?dir='.$path.'">'.$file_or_dir."\n</a><br/>";
            }
        }
        closedir($handle);
    }
    

    PS write you html code with double quotes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom PHP script which uploads information submitted by a form
I have created this report using BIRT and phpjavabridge <?php header(Content-type: application/pdf); header(Content-Disposition: inline;
I have created an array in php that prints this Array ( [mark] =>
i have created a module with this among others this function in it: <?php
I have this problem getting my newly created php project on Netbeans work on
the procedure: i have a script which uploads images (via hmtl-form and php) for
I have created a php script to import rss feed into the database. The
I currently have an ASP.NET script for a TV guide which displays the data
I have a php file which connects to a MySql db and read the
I have a table created with PHP from an Sql Database that successfully displays

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.