WTF ... IS WTF!?
We are a collective of people who believe in freedom of speech, the rights of individuals, and free pancakes! We share our lives, struggles, frustrations, successes, joys, and prescribe to our own special brand of humor and insanity. If you are looking for a great place to hang out, make new friends, find new nemeses, and just be yourself, WTF.com is your new home.

Need a script

countrygrl

Highly Excitable
446
0
0
#1
I need a script that will display the contents of a directory on a my web site. My hosting service has it locked down so that if there isn't a default page in the directory then the file lsiting won't display. I don't want to have to change an index page every time that I upload a file for someone to download.

The thing is that I don't have a clue about what to look for in searching. I found some thing via Google that sounded as though they might be right but then I didn't have a clue about how to use them.

I develop windows applications and can make them do any damn thing that you want them to and I can do some database scripting for web pages and such (I could probably get this figured out if it was windows and I could use asp) but I don't have a clue about where to start to write something to do this and to run on Linux. Can anybody point me in the right direction?
 

Woodreaux

Original Dicksman
2,429
0
0
#2
Is your website static html or is generated by a cgi script? If cgi is it Perl, PHP, ASP?
Either way, you can have have the directory contents portion displayed as a frame, such that the frame is generated by the script. In Perl it's real easy. Somelike:

#!/usr/bin/perl
# file name my_dir_frame.cgi
use cgi;

my $directory_to_show = "<the Unix path to the directory you wanna show the contents of>";

my $cgi = new_cgi();
print "<HTML>\n\t<BODY>";
print "<P>The directory contents:</P>;
print 'ls $directory_to_show`;
print "\n\t</BODY>\n</html>";

Something like that will do it. Provided the webhost has facilities for Perl and the Perlmodule in the Apache Server. In your index page or whatever page is supposed to display the directory contents, just make a frame with the source code coming from an invokation of my_dir_frame.cgi :thumbsup:
 

Jung

???
Premium
13,993
1,401
487
#3
If you have access to a Cpanel, you can change how Apache handles directory listings. This can also be accomplished with .htaccess.

Here's how I do it for my site though, using PHP and CSS for styling.

Example: http://justinb.us/firefox/extensions

Here's the code:
PHP:
<?php
error_reporting(E_ALL);
# config ###########################################################
$root='./';  // where to browse, ./ means wherever this script is located in
$rurl='./';  // url to $root, url equivalent of the path. ./ will be the same, relative
$burl='index.php?'; // base url, the url to access the indexer in case you are including this script into another one
$surl='index.php'; // the absolute url to this script so that it is linked as an image. ex: http://mydomain.com/some/where/out/there/index.php
$incl=0; // are you including this page into another one? 1 or yes, 0 for no. Yes means the script will print out <html><head...etc. No means the script will only print out the table.
$name='Scripts'; // name?
# css styles ######################################################
$css=<<<EOT
body,td{font-family:tahoma,verdana,arial;font-size:12px;line-height:15px;background-color:#BABABA;color:#666666;margin-left:20px;}
strong{font-size:12px;}
a:link{color:#0066CC;}a:hover {
        background-color:#DCDCDC;
}
a:active{color:#9DCC00;}
table.itable{}
td.irows{height:20px;background:url("index.php?i=dots") repeat-x bottom}
EOT;
# end config #######################################################
function e($s){$p=strrpos($s,'.');return substr($s,$p+1,strlen($s));}
function s ($a,$c,$o='asc',$t=SORT_STRING){if(!isset($a[0][$c]))return $a;for ($i=0;$i<count($a);$i++)$temp[$i]=&$a[$i][$c];$o=($o=='asc')?SORT_ASC:SORT_DESC;array_multisort($temp,$o,$t,$a);return $a;}
if (isset($_GET['i']))
{
    $i=trim ( $_GET['i'] );
    $imgs['images']='R0lGODlhEAAQAPcAAP//////AP8A//8AAAD//wD/AAAA/wAAAPuBhP0RI7Crrr24vL25vn+CmJ6syZ6rxbfF4cbO3srO1oOk4WF4oo6fvpyuzqCvyqKxzKGwy6i30qa10KKxy6Oxy6Kwyb3M57G/2JyovqCswsTR6dDd9crW7NPe8tXg89Hb7uLs/uLr/Njh8eHp9wBe9YaZuYiauYeYtZWmw5mqxqCvyKi30KSyysXW8p+rvqu3ysPQ5tvo/eDr/dzm9uLs/OTt/Njg7eDn8+vy/gxn7Ku92LvO68TQ4uPu/t7p+c/Y5tXe7Nfg7cvb8ejw++vw9+fs87LD2OHu/lem/vH3/lSp/uTx/vP5/snj9+33/qGoqPz+/v3+/lXSYAC1AH3GdS6qHnDIW1OmL+Hp173JqoGaKby9srurRv3slf7dbf7cc/7Xb/7QZ/3SdP7FVd6wUP7LaP6/SP68SeS7cv6vMP62QNycN+CoTN2nTfvt1fueGf6qL/6vN/6xOf65V+C/jP2XEv6eGvmeH/6gI/6iJOCSKM+WR9OZSv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ZfHowqRKjiJEICAIxdQOHDJcrVJQwAbIiR4UDgZrGAcOjrdsXK3BoMBAoCxs0WnjIsMGX7xAbHQgE6pLGxZQkPmRYWfykBhEQAQbfONFiiZItcuQEETKDRgkAgdRE6dMERQoVIqCEsDBBggnQgr5ggUGBg4cNGDJckOBagEuBA4ILHx78t8BAyJMrRx4QADs=';
    $imgs['php']=&$imgs['htm'];$imgs['asp']=&$imgs['htm'];
    $imgs['zip']='R0lGODlhEAAQAOYAAMjY9gNKsCBarSZox7TP9iJesCdktitpuzp5xEiH0XCc05jC9ZCz3jl/0JC235S335a535i531qZ12+v7na09IKx3zeZ81Wm9Fur9mCz/1yl62e3/2Co62Ws7XG7/22x7ne+/3S38HW38HGx5nq88XSx5IjD84DA8oTD84vK+ZvP9YnJ963b+bzl+8fs/fT///79mf//r///uf//xPr2k//9pP/4hv/6kPr1kPr1kf/7mvfvgvPkbNm/Kdm/K/n25dm8Lvvwvfvzzt61APbNK9m3KPzUMP3VONm3MNu5Mtq6NN6+Ov3ZRv3aUPvdY/zkhfvnl/roodq0Kf7TN/zZV/zebPvec/zifPrjj/rprfrrs/juytKgBtSjB9WlCs2fC8+hDc6fDdWmD9KlEtWmFdSnGtKnHNGnHNKnHtKoHtiuJ9rDebN7DbJ8DbWBJKlrCqhrCrB5IKpxG/EYAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAHQALAAAAAAQABAAAAe7gHSCg3QoJyQhHx0cg2tgj2scLy4tLComIgmCX3OdYAOgoQ0YEgF0YVJFPT5ASElKS2x0FSWmaTAzMjE1Ojc2PG10DiimZjQ/W1lYVlRTQ8EQKaZnOUJaUFdOTEbPdNHT1dfZ290P0nRoOEFRT1VNR0TdESumYztqZWRiXl1c3QwjTLmJ8waOnFChFEzQYIoQHUkbMngAAUIDAoeDOBwwUKCAgIYYHXJgFDIjxZEjQToEAYDAAgoXLAQIBAA7';
    $imgs['rar']=&$imgs['zip'];$imgs['tar']=&$imgs['zip'];
    $imgs['dir']='R0lGODlhEAAOANUAAP//mf/3kf/0jv/rhf/mgP/gev/UbplmAJpnAZxpA55rBaBtB6NwCqVyDKh1D6t4Eq57FbB9F7OAGrSBG7WCHLeEHriFH7qHIbyJI72KJL+MJsCNJ8KPKcWSLMeULsmWMMuYMsyZM9OgOtypQ+azTe+8VvjFX//MZv///8nJycDAwLS0tH5+fnZ2dmJiYmFhYURERP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAADEALAAAAAAQAA4AAAaSwFgsBPp4hMikMIRqojjKJBNABaAylwpFEkGCAoJw2NmEsISfAaHDbm8wlolEEvMUOuQ8AcVgdQwhVYJUJgALLhwnIAABjY6NJQAKLxsnHQADmZqZJAAJLxknGgAFpaalIwAILxcnFgAGsbKxIgAHLilbERAPDg0MCwoJCAcwLTErLS/LzM0vLStCKSrU1dYqQkEAOw==';
    $imgs['<x>']='R0lGODlhDQAQANUAAL60urm203d3wXd3wMHB2sTG4ra5xL/K2MHO2b/N1bzM1MHP1r/O1b7N1LbKzrvN0L7Q0ZacnHh8fLq+vp6hoYqMjLu9vX1+fmRlZfz9/ez19MHIx/H49+n08uz29Ov18/f7+tS/SqGBObmXSZBqMIZoQZ2CXKmQbryqkZJ7YY14boRtbP///8fHx5eXl5OTk4mJiYODg3Z2dnBwcGlpaWBgYP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAADYALAAAAAANABAAAAZyQJtwSCyOQiRRKBVamVpCgkBAQqkCJ4DJJRwUSp2SpwQqsWbdR2ft4YAy5+5i3Wm/47YBo3PpW+5oeQl0dnCBAxB8fRcWLHgDDYRuhl0Hin6OhwiSgF0KfBaFjw5rHxobExQVMEMGERIYNTQyMS9Ft0RBADs=';
    $imgs['dots']='R0lGODlhAwABAJEAAAAAAP///2RkZP///yH5BAEAAAMALAAAAAADAAEAAAIC1FYAOw==';
    if(isset($imgs[$i]))$d=&$imgs[$i];else$d=&$imgs['<x>'];
    header('Content-type: image/gif');header('Content-Disposition: attachment; filename="'.$i.'"');print base64_decode($d);
}
else
{
    ob_start('ob_gzhandler');
    if(!$incl)print'<html><head><style type="text/css">'.$css.'</style></head><body>';
    $sb=isset($_GET['s'])?$_GET['s']:0;
    $so=isset($_GET['o'])?$_GET['o']:'asc';
    $st=$sb==='s'||$sb==='m'?SORT_NUMERIC:SORT_STRING;
    $d=isset($_GET['d'])?urldecode($_GET['d']):'';
    $d=$d!==''&&$d!=='/'?trim($d,'/').'/':'';
    $p=$root.$d;
    $h=@opendir($p);
    if(strstr($d,'../'))exit('Denied');
    if(!$h)exit('Unable to open "'.$d.'". <a href="'.$burl.'">Go home.</a>');
    $F=array();$D=array();
    while(FALSE!==($f=readdir($h)))if($f[0]!=='.')if(is_dir($p.$f))$D[]=array('n'=>$f,'m'=>filemtime($p.$f),'s'=>filesize($p.$f),'t'=>'Directory');else$F[]=array('n'=>$f,'m'=>filemtime($p.$f),'t'=>e($f),'s'=>filesize($p.$f));
    if($sb){$F=s($F,$sb,$so,$st);$D=s($D,$sb,$so,$st);}
    print '<table class="itable" cellspacing="2"><tr><td colspan="5">';
    print ($d!=='')?"<a href=\"$burl\"><strong>$name</strong></a> / ":"<strong>$name</strong> ";
    if($d!==''){$t=explode('/',trim($d,'/'));for($i=0,$r=array(),$z='';($r[]=@$t[$i]),$z=@$t[$i];$i++) { print(implode('/',$r)!==trim($d,'/'))? ('<a href="'.$burl.'d='.implode('%2F',$r).'"><strong>'.$z.'</strong></a> / ' ):"<strong>$z</strong>";}}
    $f=trim($d,'/');
    print '</td></tr><tr><td class="irows">&nbsp;</td>'
    .'<td class="irows"><a href="'.$burl.'s=n&amp;o='.($so=='asc'?'dsc':'asc').'&amp;d='.urlencode($f).'">Name</a></td>'
    .'<td class="irows"><a href="'.$burl.'s=s&amp;o='.($so=='asc'?'dsc':'asc').'&amp;d='.urlencode($f).'">Size</a></td>'
    .'<td class="irows"><a href="'.$burl.'s=t&amp;o='.($so=='asc'?'dsc':'asc').'&amp;d='.urlencode($f).'">Type</a></td>'
    .'<td class="irows" nowrap="nowrap"><a href="'.$burl.'s=m&amp;o='.($so=='asc'?'dsc':'asc').'&amp;d='.urlencode($f).'">Modified</a></td>';
    for($i=0,$c='';($c=@$D[$i++]);){print '<tr><td><img src="'.$surl.'?i=dir" alt="'.$c['n'].'"/></td><td><a href="'.$burl.'d='.urlencode($d.$c['n']).'">'.$c['n'].'</td><td>&nbsp;</td><td>&nbsp;</td><td>'.date('m/d/Y h:i A',$c['m']).'</td></tr>';}
    for($i=0,$c='';($c=@$F[$i++]);){print '<tr><td><img src="'.$surl.'?i='.$c['t'].'" alt="'.$c['n'].'"/></td><td><a href="'.$rurl.$d.$c['n'].'">'.$c['n'].'</td><td>'.number_format($c['s']/1024,1).'KB</td><td>'.$c['t'].'</td><td>'.date('m/d/Y h:i A',$c['m']).'</td></tr>';}
  }  
?>
If you read the comments, you'll see where to edit the name of the directory and the CSS. If you use an editor that displays line numbers (jEDIT, Dreamweaver et cetera) name is on line 9, and CSS starts on line 12.
 

countrygrl

Highly Excitable
446
0
0
#4
Yeah I can enable cgi on the host site and yes PERL will work. That looks like something that I may be able to play around with and get to work. At least a starting place. I need to generate the file list a line at a time so that I can link the actual file to the listing.


I kinda of screwed myself when I changed hosts. I get more bandwidth and storage for less but this host doesn't allow me to set up individual ftp accounts for users.


Thanks again Woodreaux!! :thumbsup:
 

countrygrl

Highly Excitable
446
0
0
#5
junglizm said:
If you have access to a Cpanel, you can change how Apache handles directory listings. This can also be accomplished with .htaccess.

Here's how I do it for my site though, using PHP and CSS for styling.

Example: http://justinb.us/firefox/extensions

That's EXACTLY what I'm looking for!!! Do you mind if I use the code?


Damn it really does pay to just ask sometimes!