View Full Version : Writing PHP to write Apache directory
gorilla1
09-16-2002, 02:47 PM
Beliw is an example of how apache writes out the contents of a directory, familiar to all of you. The formatting is done with the html <pre> tag and regular spacing. If you had to write the php statement that wrietes out each line of the directory contents so that is is spaced properly, how would you do it? (I am able to get the directory contents to write fine, but the spacing I cant quite get right, since if I set up the echo statement with a certain spacing, variation in the length of the field values such as filename throws the spacing off).
G
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>Index of /Wkp/2002-09-09</TITLE>
</HEAD>
<BODY>
<H1>Index of /Wkp/2002-09-09</H1>
<PRE><IMG SRC="/icons/blank.gif" ALT=" "> <A HREF="?N=D">Name</A> <A HREF="?M=A">Last modified</A> <A HREF="?S=A">Size</A> <A HREF="?D=A">Description</A>
<HR>
<IMG SRC="/icons/back.gif" ALT="[DIR]"> <A HREF="/Wkp/">Parent Directory</A> 09-Sep-2002 18:07 -
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="zzz.gif">zz zzzzzz-19.gif</A> 08-Sep-2002 11:56 19k
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="xxx.gif">xxx xxxxx-9.gif</A> 08-Sep-2002 11:50 17k
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="yyy.gif">yyy yyyyyy-1118.gif</A> 08-Sep-2002 11:49 24k
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="yyy.gif">yyy yffff</A> 08-Sep-2002 11:49 24k
</PRE><HR>
<ADDRESS>Apache/1.3.22 Server at xyz.com Port 80</ADDRESS>
</BODY></HTML>
mordred
09-16-2002, 03:05 PM
What is the code you used?
gorilla1
09-16-2002, 04:35 PM
Unfortunately, I redid it with a table format and did not save my original code. But below is basically what I did (this is in a loop after doing a readdir and putting the results in an array). The directory data all comes out fine, it is just that the columns don't all line up right. Note that when you do a view source on an apache directory, you get what I showed in the post at the top of this thread. The column structure is all done with the <pre> tag and defined spacing. The defined spacing both in the html I posted and in the php code I show below is getting collapsed when I post here. So it may be helpful to do a view source on an apache directory to see how they do it.
(By the way, when I did it in table format, I got the alignment all right, but the font and the font size were no longer right,e ven though I specified no styling at all in either case.)
G
echo"<br><IMG SRC=\"/icons/image2.gif\" ALT=\"[IMG]\<A HREF=\"$thisFile[0]\">$thisFile[0]</A>$thisTime filesize ( $thisFile[0])";
mordred
09-16-2002, 06:40 PM
Ok, I got what you want to achieve. Here's an example that writes a list of files with the appropriate spacing between the file names and the arbitrary string on the right side to show the effect:
$thisFile = array(
"help.html",
"table.html",
"unreadeable.html"
);
$lineWidth = 50;
for ($i = 0; $i < count($thisFile); $i++) {
$spaces = $lineWidth - strlen($thisFile[$i]);
$output = "";
$output .= "<IMG SRC=\"/icons/image2.gif\" ALT=\"[IMG]\ /> <A HREF=\"$thisFile[$i]\">$thisFile[$i]</A>";
$output .= str_repeat(" ", $spaces);
$output .= "size: x kb\n";
print($output);
}
Of course you need to put <pre> tags around this snippet. It worked well in Mozilla, if other browsers don't work as expected, than it's possibly more a HTML than a PHP problem.
hth
gorilla1
09-16-2002, 06:55 PM
Thanks, Mordred. It certainly is not a php problem. Basically, when you look at the html behind the apache directory page, most likely you are looking at html written by the server with php code. So clearly the formatting can be done - it is a matter of guessing what php they used to get it right. The idea is that the columns for filename, time size are supposed to line up, and do so despite the fact that the file name can vary in length (and no table tags or other positioning tags are used)... Anyway, I will try you code later and post back.
G
mordred
09-16-2002, 10:42 PM
Originally posted by gorilla1
Basically, when you look at the html behind the apache directory page, most likely you are looking at html written by the server with php code.
Are you perfectly sure that's the case? I mean, how does the directory index code be constructed if no php module/CGI is available to the apache - I remember it still gave the directory listing, and that a certain configuration directive exists that controls whether directories get listed this way or not.
So clearly the formatting can be done - it is a matter of guessing what php they used to get it right.
If you're really interested, I think you should look either at the apache source or simply ask the developers.
The idea is that the columns for filename, time size are supposed to line up, and do so despite the fact that the file name can vary in length (and no table tags or other positioning tags are used)... Anyway, I will try you code later and post back.
The trick is very simple indeed: Inside a <pre> tag, everything is/should be in a fixed-size font. So all I did was to put a variable amount of spaces after each filename string, depending on the length of the file name. I set the width from first character position of the filename to the first character to 50 spaces. And from this number I subtracted the length of the filename and put the resulting number in spaces behind... really no rocket science here, but I guess you figured this out while I'm still typing these lines. ;)
gorilla1
09-17-2002, 02:55 AM
mordred,
Yes, you are right, the developers presumably coded in a lower level language... Talk to the developers or read the source? Didn't know that was available... Read your code? No, I responded to you without reading the code - I mentioned that I would look at it later - I had to focus too much on other things at that point to look at the code.... I used the strategy in your code, and I am getting close to getting there (for some reason the linewidth is working at a lower value than it seems it ought to be, resulting in a negative value on longer file names). Below is the code I am using so far.
(to update this, I got it to work - I overlooked some spacing I left in the echo statement, causing the occasional negative value)
G
$size = filesize($thisFile[0]);
$size = round($size/$kb,0)."k";
$spaces = $lineWidth - strlen($thisFile[0]);
$spacer = str_repeat(" ", $spaces);
echo"<IMG SRC=\"/icons/image2.gif\" ALT=\"[IMG]\"> <A HREF=\"$thisFile[0]\">$thisFile[0]</A> $spacer$thisTime $thisTime $size<br>";
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.