Forum Moderators: coopster

Message Too Old, No Replies

find and output directory index

         

Skier88

7:29 pm on Oct 27, 2010 (gmt 0)

10+ Year Member



I am writing a script which processes a url; if the url points to a valid file the script outputs that file. The problem is that sometimes the URL is a directory, so the file I would need to output is something like $url.'/index.html', $url.'/index.php', etc (or, if no index file exists, a directory listing). Once the script determines the url is a directory, how do I output the contents of its index file? I would prefer not to individually test for the existence of index files, since this adds code, limits the allowed file types, and does not tolerate changing the default index name. Thanks for reading.

Matthew1980

7:44 pm on Oct 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Skier88,

Though I *sort of* understand your question, my first thought is: "What if the domain has directory browsing set to ignore/off". ie:-

# Prevent directory browsing
IndexIgnore */*

This could potentially render your script useless? Then again, If I have misunderstood your question, apologies.

Show us what you have up to yet and then we can offer guidance/suggestions from there...

Cheers,
MRb

Skier88

7:52 pm on Oct 27, 2010 (gmt 0)

10+ Year Member



Thanks for the reply Matthew. You're right, that is one more consideration the script should take into account (I assume that is a http setting you are talking about).

The problem is that I don't know where to start. I'll give you what I have, which just echoes 'get index' if it is fed a directory.


<?php
include 'header.php';

$url=strip_tags($_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'].'?');
if(strpos($url,'?'))
$url=substr($url,0,strpos($url,'?'));

if(is_dir($url))
echo 'get index';
else if(is_file($url))
readfile($url);
else
{
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
readfile('404.html');
}

include 'footer.php';
?>


Now of course I could replace
echo 'get index';

with
readfile($url.'/index.html');

... but that has all of the issues mentioned above.

astupidname

7:54 am on Oct 28, 2010 (gmt 0)

10+ Year Member



The problem is that sometimes the URL is a directory, so the file I would need to output is something like $url.'/index.html', $url.'/index.php', etc (or, if no index file exists, a directory listing).


You don't need a php script for this, as Matthew1980 began to hint at, if you are running apache server you can just use .htaccess file settings. By default viewing a folder url where the folder contains an index.html or index.php file the files contents should be parsed and output to the browser, unless you use the DirectoryIndex [httpd.apache.org] apache directive (within .htaccess file) to change the name of the default file to view from folder urls.
Now, if no default view file exists within the folder, and you have Options +Indexes [httpd.apache.org] in the .htaccess file in the particular folder, or in root of site, that will turn on indexing of any folders contents which does not contain a default view file.
You can even control styling of indexed directories [webmasterworld.com]
I'm wondering if I'm misunderstanding your intent regarding index.html or index.php files, do you want them output as normal, or displayed as plain text showing the actual files contents as opposed to the parsed and displayed content? You could display the files as plain text using the AddType [httpd.apache.org] directive such as:
AddType text/plain .html .php
within an .htaccess file will cause .html or .php files to be simply displayed as plain text contents.

Skier88

4:53 pm on Oct 28, 2010 (gmt 0)

10+ Year Member



Thanks for the reply. I oversimplified the script - what it actually outputs is a header, then user messages if applicable, then the file, then a footer. My goal is for the script to emulate the apache behavior you described.

Here's the actual complete code. I reordered it since my last post since it previously send 404 headers late. Of course, the issue is that it assumes every directory contains "index.php".

<?php
$url=strip_tags($_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'].'?');
if(strpos($url,'?'))
$url=substr($url,0,strpos($url,'?'));
$headers=array();

if(is_dir($url))
$url.='/index.php';
else if(preg_match("@^/database/[^\?]@i",$_SERVER['REQUEST_URI']))
$url='../database/item.php';
else if(!is_file($url))
{
$url='404.html';
$headers[]=$_SERVER['SERVER_PROTOCOL'].' 404 Not Found';
}

$alert=$_REQUEST['alert'];
if($alert==$_GET['alert'])
$alert=urldecode($alert);
if($alert)
$alert="<a href='?'
onclick='this.style.display=\"none\";'
style='position:absolute;
top:200px;
width:500px;
margin-left:150px;
padding:20px;
font-weight:bold;
color:#000;
border:1px solid #333;
text-decoration:none;
z-index:999;
background:#cdf url(\"/_structure/x.png\") no-repeat right top;
'>$alert</a>";

foreach($headers as $h)
header($h);
include 'header.php';
echo $alert;
include $url;
include 'footer.php';
?>


Alternatively, if there is some way to automatically include a php header and footer without modifying each file (even just to add "include 'header.php';") I could use that instead. But that's getting off topic.