Jump to content
xisto Community
Sign in to follow this  
sachavdk

Making A Webserver Directory Listing Helps you organise your webserver

Recommended Posts

I recentely installed IIS with PHP and MySQL on my pc (previous I used UniServer, but that doesn't matter here). But I had always to go to LOCALHOST/websiteiwanted/ or I had to create a shortcut on my desktop for every site so I decided to create an "overviewpage". It shows all the websites in your wwwroot with a link to them. If you have folders you don't want to be included, you extense the && check (but I'll explain this lateron)

Here's the total code:



<ul><table cellpadding="3" cellspacing="3" border="0" width="95%"><?$dir=opendir('.');$isdirtrue = false;while ($file = readdir($dir)){ if($file != '..' && $file !='.' && $file !=''){  if (is_dir($file)){   if ($file != 'IIS_Services' && $file != 'RECYCLER' && $file != 'Config' && $file != 'System Volume Information')   {    $isdirtrue  = true;    echo '<tr><td>';    echo '<a href="'.$file.'/" target="_blank"> [ '.$file.' ]</a>  ';    echo '</td></tr>';   }  } }}if ($isdirtrue == false) {echo "<div align='center'>- No projects -</div>";}closedir($dir);clearstatcache();?></table></ul>

Now I'll explain every step

$dir=opendir('.'); opens the current folder. You could use .. to specify the folder one level up or ./folder/folder to open other folders.

$isdirtrue = false; is going to check if the opened file is a directory lateron in the script, this just initializes it

while ($file = readdir($dir)){ is going to read every "file" in the opened directory. Also . .. are files in the opened folder. This can be convenient sometimes but now they don't matter thats why I'm gonna use following code. To see that the opened file not . or .. is:

if($file != '..' && $file !='.' && $file !=''){

if (is_dir($file)){ checks wheter the file is a directory. if it is one, we go to the next step

if ($file != 'IIS_Services' && $file != 'RECYCLER' && $file != 'Config' && $file != 'System Volume Information'){ here you can specify which folders don't participate in your projects list

$isdirtrue = true; this sets the isdir to true because we have at least one project in the list. more explanation withing some lines

echo '<tr><td>';
echo '<a href="'.$file.'/" target="_blank"> [ '.$file.' ]</a>  ';
echo '</td></tr>';
these are gonna write a link to the projectfolder

if ($isdirtrue == false) {
echo "<div align='center'>- No projects -</div>";
}
now remember that we just set $isdirtrue to true because we had at least one project? at the beginning of the script we initialized it false. if there wouldn't have been a projectdirectory it would still be false and then it would give a message that there are no projects on your server.

now very important are
closedir($dir); and
clearstatcache(); because if you don't use these, your cache memory will be left full of needless information.
closedir() closes de directory you opened in the beginning of the script and clearstatcache() deletes all "temporary files" from the memory.

Now that's it. I hope lots of people will follow my example and be more neater with their local webserver. B):P



Share this post


Link to post
Share on other sites

so how does this work?
I copied your code to notepad saved the file as test.html and nothing came up but:

[ '.$file.' ]  '; echo ''; } } } } if ($isdirtrue == false) { echo "- No projects -"; } closedir($dir); clearstatcache(); ?>

what do you do if you defined the inetpub folder to a different location other than C:\inetpub\wwwroot?

Share this post


Link to post
Share on other sites

You need to save it as test.php not as a .html file, Klass.

That should fix your problem. Also, I would like to know if it is possible to make something similar for Apache. Not just the normal directory listing, but one with links and some folders not displayed?

Share this post


Link to post
Share on other sites

You need to save it as test.php not as a .html file, Klass.

That should fix your problem. Also, I would like to know if it is possible to make something similar for Apache. Not just the normal directory listing, but one with links and some folders not displayed?

162310[/snapback]

Normally it could serve for Apache Servers. Run the script and see which directories are listed you don't want to

 

if ($file != 'IIS_Services' && $file != 'RECYCLER' && $file != 'Config' && $file != 'System Volume Information')

 

IIS_Services, Config are directories I created to install PHP and MySQL, RECYCLER and System Volume Information are part of IIS and can be deleted with apache. IE. you have three directories. Two are project and one is some configuration dir.

1. MyConfiguration

2. Project1

3. Project2

 

Now you don't want dir MyConfiguration listed, then you simply have to replace

if ($file != 'IIS_Services' && $file != 'RECYCLER' && $file != 'Config' && $file != 'System Volume Information') by

if ($file != 'MyConfiguration').

 

And for the links, normal it should display a link on every project

 

this code takes care of that:

echo '<tr><td>';   echo '<a href="'.$file.'/" target="_blank"> [ '.$file.' ]</a>  ';   echo '</td></tr>';
if it shouldn't work, try this:

echo '<tr><td>';   echo '<a href="./'.$file.'/" target="_blank"> [ '.$file.' ]</a>  ';   echo '</td></tr>';

Share this post


Link to post
Share on other sites

ok i sill don't understand your how to:1. You do not tell us to save as a PHP file.2. Where am I supposed to run this from?3. Do I need to configure this for a directory since I do not use Inetpub defaults?

Share this post


Link to post
Share on other sites

ok i sill don't understand your how to:

 

1. You do not tell us to save as a PHP file.

2. Where am I supposed to run this from?

3. Do I need to configure this for a directory since I do not use Inetpub defaults?

162414[/snapback]


1. I've forgotten to tell it, sorry.

 

2. Wherever you want in your webroot.

 

3. It doesn't matter if you use Inetpub or some other. Imagine your wwwroot is "C:\MyDocuments\WWW\WEBSERVER\" and you have 5 webprojects in your webroot ie pro1, pro2, pro3, pro4 and pro5, each is located in "C:\MyDocuments\WWW\WEBSERVER\" so you have

"C:\MyDocuments\WWW\WEBSERVER\pro1\"

"C:\MyDocuments\WWW\WEBSERVER\pro2\"

"C:\MyDocuments\WWW\WEBSERVER\pro3\"

"C:\MyDocuments\WWW\WEBSERVER\pro4\"

"C:\MyDocuments\WWW\WEBSERVER\pro5\"

and maybe some configdir or a dir you dont want to have listed

"C:\MyDocuments\WWW\WEBSERVER\whatever\"

 

if you place the script in "C:\MyDocuments\WWW\WEBSERVER\" and you replace line 8 (if ($file != 'IIS_Services' && $file != 'RECYCLER' && $file != 'Config' && $file != 'System Volume Information')) by if ($file != 'whatever') the script will give you a list from all projects (pro1 to pro5). If you click one, the script will open the project (directory) in a new window.

 

You can set this script whereever you like (ie you named this script list.php and set it in your rootdir) and go to http://forums.xisto.com/no_longer_exists/

 

TO EVERYONE WHO READS THIS:

if ($file != 'IIS_Services' && $file != 'RECYCLER' && $file != 'Config' && $file != 'System Volume Information')
is just a line of code so dirs I don't want to be listed, are not listed.

If there are no dirs you don't want to be listed change the line to

if ($file != '')
this way all dirs are listed.

 

I hope it's clear now to you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.