News:

Go to HostNed.com
Welcome.  This is a place to get user-to-user support, learn more, and share ideas.  If you can't find your answers here, feel free to ask by creating a new topic or visit the support ticket system at https://my.hostned.com :)  Have fun here!

Main Menu

[How To] Allow / Disallow Directory Browsing

Started by Dynaweb, February 20, 2007, 04:09:20 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Dynaweb

Most servers are configured so that directory browsing is not allowed.  This means that if a certain web directory does not contain an index (or default) file, then an error message will be displayed in the web browser.

If Directory Browsing is enabled, then the contents of the directory will be shown in the browser and you can click freely on files and folders therein.

Disllow Directory Browsing
If you want to Disallow Directory Browsing on a server that allows it, create an .htaccess file in the web root directory and add the following line:

IndexIgnore */*

Allow Directory Browsing
If you want to Allow Directory Browsing on a server that dies nto allow it by default, create an .htaccess file in the web root directory and add the following line:

Options +Indexes

Remember that .htaccess files do not work on Windows Servers, so you will have to have your web host set the configuration for you.  Have fun with your server and htaccess!

Dynaweb

Here is a quick and easy PHP script that will virtually list contents of any directory that it is placed in.  Very easy and very handy and tested to work on HostNed FH1 server system (where .htaccess will not work).


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>FileBrowser</title>

<style>

body {
font-family:Arial, Helvetica, sans-serif;
font-size:14px
}

.current_dir {
font-size:18px;
font-weight:bold
}

</style>

</head>

<body>
<center>
<div style="text-align:left; width:740px">
<div class="current_dir">
<?php
error_reporting
(E_ALL);
$dir_name explode("/"dirname(__FILE__));
if(isset(
$_GET['dir']) and file_exists($_GET['dir'])){
echo 
"./".$dir_name[count($dir_name)-1]."/".trim($_GET['dir'], "./")."/";
} else {
echo 
"./".$dir_name[count($dir_name)-1]."/";
}
?>

</div><br/>
<table border="1" cellspacing="0" cellpadding="2">
<tr><td width="40"><b>Type</b></td><td width="200"><b>Filename</b></td><td width="100"><b>Size</b></td><td width="300"><b>Date Modified</b></td><td width="100"><b>Permissions</b></td>
</tr>
<?php

// Settings //////////////

/* Only allow users to view the same directory this file is placed in. false: let users browse all directories */
$current_dir_only false;

/* Hide certain file types. Example: $hide_files = array("php", "txt"); */
$hide_files = array();

//////////////////////////

if(isset($_GET['dir'])){
$dir $_GET['dir'];
} else {
$dir ".";
}

if(
$current_dir_only == true){
$dir ".";
}

if (
is_dir($dir)) {
    if (
$dh opendir($dir)) {
        while ((
$file readdir($dh)) !== false) {

if(
$file == ".." and isset($_GET['dir'])){
$back explode("/"$_GET['dir']);
unset($back[count($back)-1]);
$link implode("/"$back);
} else {
$link $file;
$back = array();
}

// if dir exists, make the link to it
if(is_dir("./$dir/$link/")){
$link "$dir/$link";
}

// .. link problem
if(count($back)-== and is_dir("./$dir/$link/")){
$link "";
} else {
$link "dir=$link";
}

// as not to make ../dir/../beginning
if($file == ".." and count($back) == 1){
$link "";
}

// link to file or dir
if(is_file(trim("$dir/$file""dir="))){
$link trim("$dir/$file""dir=");
} else {
$link "?{$link}";
}

if(
is_file(trim("$dir/$file""dir="))){
$filesize round(filesize($link)/10002);
$filesize .= "KB";

$modified date("j/m/y h:i"filemtime($link));

$type explode("."$file);
$type $type[count($file)];

$perm substr(sprintf('%o'fileperms($link)), -4);

} else {

$dirname trim("$dir/$file""dir=");

$filesize "&nbsp;";
$modified "&nbsp;";
$type "Dir";

if(
file_exists($dirname)){
$perm substr(sprintf('%o'fileperms("$dirname")), -4);
}
}

if(
$link == "?dir=./"){
$link "?dir=../";
}

if(
$current_dir_only == true and $type == "Dir"){
$show_dir 0;
} else {
$show_dir 1;
}

if(!isset(
$_GET['dir']) and $file == ".."){
$no_go_back true;
} else {
$no_go_back false;
}

if(
$file != "."){
if(
$no_go_back == false){
if(
$show_dir != 0){
if(!
in_array($type$hide_files)){

echo "<tr><td>$type</td><td><a href=\"$link\">$file</a></td><td>$filesize</td><td>$modified</td><td>$perm</td></tr>\n";
}
}
}
}
}

        
closedir($dh);
    }
}


?>

</table>
</div>
<br />
<!-- Do not remove this notice -->
Powered by <a href="http://www.publicwarehouse.co.uk">FileBrowser</a> &copy; Copyright 2007 Peter Bailey
<!-- Do not remove this notice -->
</center>
</body></html>