Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Parse directory to webpage script?

Parse directory to webpage script?
Thread Tools
Mac Enthusiast
Join Date: Oct 2000
Location: Toronto
Status: Offline
Reply With Quote
Jul 1, 2001, 01:12 AM
 
Hi everyone,

I was wondering if there was a perl script which I could run from the command line which would read through a directory, and output a webpage with links to all the files in that directory, and subdirectories. I have a folder with a lot of pdf's which I would like to link to. There are far too many to hand-write the links, which is why I have been putting it off for so long. Can anyone help?

Thanks in advance
     
Mac Elite
Join Date: Mar 2001
Status: Offline
Reply With Quote
Jul 1, 2001, 10:26 PM
 
Sorry but this going to be slightly off topic since it doesn't deal with perl BUT if your webserver is running ASP then you can use the scripting.filesystemobject to output the contents of said directory and put

<pre>
<a href=' '> </a>
</pre>

around it. The good thing about doign it in an asp (or server side scripting language of your choice) is that if you add or remove files from the directory you have nothing to worry about because the list is generated when a user requests the page.

BUT if the directory listing is going to be somewhat static then a perl script (or any other language) will probably suit you fine.

Anyway try looking at hotscripts.com or something for a suitable solution. If your webserver is able to run PHP or ASP then I can write the simple script for you in PHP or ASP if you don't know how.
     
Mac Enthusiast
Join Date: Oct 2000
Location: Toronto
Status: Offline
Reply With Quote
Jul 2, 2001, 02:08 AM
 
Originally posted by Raman:
<STRONG>Sorry but this going to be slightly off topic since it doesn't deal with perl BUT if your webserver is running ASP then you can use the scripting.filesystemobject to output the contents of said directory and put

&lt;pre&gt;
&lt;a href=' '&gt; &lt;/a&gt;
&lt;/pre&gt;

around it. The good thing about doign it in an asp (or server side scripting language of your choice) is that if you add or remove files from the directory you have nothing to worry about because the list is generated when a user requests the page.

BUT if the directory listing is going to be somewhat static then a perl script (or any other language) will probably suit you fine.

Anyway try looking at hotscripts.com or something for a suitable solution. If your webserver is able to run PHP or ASP then I can write the simple script for you in PHP or ASP if you don't know how.</STRONG>
Thanks a lot Raman, I would really appreciate it if you could write something in PHP, I would do it myself, but I do not know how. My server is, however, configured for PHP. If you could also point me in a direction to learn more about php (for free ) I'd be really appreciative.

As I learn more, I'd like to be able to configure it so that it would only make links out of file with a certain extension (.txt, .pdf, etc).

On another topic....I want to do a script, which would parse my bookmarks.html file, and output to a template I have created. The template is largely javascript, and I it needs to be formatted in a particular manner. I was planning on learning how to do this in perl, but should I use php? Would you have any suggestions on this as well?

Thanks a lot, I will talk to you soon..........anothermacguy
     
Mac Enthusiast
Join Date: Oct 2000
Location: Toronto
Status: Offline
Reply With Quote
Jul 11, 2001, 08:25 AM
 
Raman? Did you get a chance to look at this?

Sorry to bug you...but I'm desparate.
     
Grizzled Veteran
Join Date: Sep 1999
Location: Boston, MA USA
Status: Offline
Reply With Quote
Jul 11, 2001, 02:35 PM
 
Originally posted by anothermacguy:
<STRONG>Raman? Did you get a chance to look at this?

Sorry to bug you...but I'm desparate.</STRONG>
Try this:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
&lt;?
function listDir($theDir, $<font color = purple>self</font>, $topLevel) {

<font color = brown>// edit these to show images</font>
$folderIcon = <font color = red>""</font>;
$fileIcon = <font color = red>""</font>;

print(<font color = red>"&lt;tr&gt;&lt;td colspan=\"</font><font color = blue>2</font>\<font color = red>" align=\"</font>center\<font color = red>"&gt;&lt;H2&gt;Directory Listing <font color = green>for</font> "</font>);
<font color = green>if</font> ($theDir == <font color = red>"/"</font>) {
$theDir = $topLevel;
print(<font color = red>"/"</font>);
} <font color = green>else</font> {
$theDir = $topLevel . <font color = red>"/"</font> . $theDir;
$subPath = eregi_replace(<font color = red>"$topLevel/"</font>, <font color = red>""</font>, $theDir);
print(<font color = red>"/$subPath"</font>);
}
print(<font color = red>"&lt;/H2&gt;&lt;hr&gt;&lt;/td&gt;&lt;/tr&gt;\n"</font>);
<font color = green>if</font> ($theDir != $topLevel) {
$currPath = explode(<font color = red>"/"</font>,$subPath);
$lastItem = ((count($currPath))<font color = blue>-1</font>);
$upLevel = <font color = red>""</font>;
<font color = green>for</font> ($i=<font color = blue>0</font>; $i&lt;$lastItem; $i++) {
<font color = green>if</font> ($i != <font color = blue>0</font>) {
$upLevel .= <font color = red>"/"</font>;
}
$upLevel .= $currPath[$i];
}
print(<font color = red>"&lt;tr&gt;&lt;td align=\"</font>left\<font color = red>"&gt;&lt;A HREF=$<font color = purple>self</font>?currDir=$upLevel&gt;&lt;-- Up one level&lt;/a&gt;&lt;hr&gt;&lt;/td&gt;&lt;/tr&gt;\n"</font>);
}
$level=<font color = blue>1</font>;
$dir = opendir($theDir);
<font color = green>while</font> (($theFile = readdir($dir)) != <font color = purple>FALSE</font>) {
$firstChar = substr($theFile,<font color = blue>0</font>,<font color = blue>1</font>);
<font color = green>if</font> ($firstChar != <font color = red>"."</font>) {
<font color = green>if</font> (is_dir($theDir.<font color = red>"/"</font>.$theFile)) {
<font color = green>if</font> ($subPath) {
$folderLine = <font color = red>"&lt;tr&gt;&lt;td&gt;&lt;A HREF=$<font color = purple>self</font>?currDir=$subPath/$theFile&gt;"</font>;
} <font color = green>else</font> {
$folderLine = <font color = red>"&lt;tr&gt;&lt;td&gt;&lt;A HREF=$<font color = purple>self</font>?currDir=$theFile&gt;"</font>;
}
<font color = green>if</font> ($folderIcon) {
$folderLine .= <font color = red>"&lt;img src=images/foldericon.gif ALT=\"</font>dir: $theFile\<font color = red>"&gt;"</font>;
} <font color = green>else</font> {
<font color = brown>//$folderLine .= <font color = red>"dir: "</font>;</font>
$folderLine .= <font color = red>""</font>;
}
$folderLine .= $theFile;
$folderLine .= <font color = red>"&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;\n"</font>;
print($folderLine);
} <font color = green>else</font> {
$theSize = round(filesize(<font color = red>"$theDir/$theFile"</font>)/<font color = blue>1000</font>,<font color = blue>2</font>);
$theDirClean = preg_replace(<font color = red>"/\s/"</font>,<font color = red>"%<font color = blue>20</font>"</font>,$subPath);
$theFileClean = preg_replace(<font color = red>"/\s/"</font>,<font color = red>"%<font color = blue>20</font>"</font>,$file);
<font color = green>if</font> ($theFileClean != '.htaccess') {
$theLink = <font color = red>"&lt;p class=\"</font>defaultbody\<font color = red>"&gt;"</font>;
$theLink .= <font color = red>"&lt;A HREF="</font> . $theDirClean . <font color = red>"/"</font> . $theFile . <font color = red>" TARGET=\"</font>stage\<font color = red>"&gt;"</font>;
$fileLine = <font color = red>"&lt;tr&gt;&lt;td&gt;"</font> . $theLink;
<font color = green>if</font> ($fileIcon) {
$fileLine .= <font color = red>"&lt;img src='images/fileicon.gif' ALT=\"</font>file: $theFile\<font color = red>"&gt;"</font>;
} <font color = green>else</font> {
<font color = brown>//$fileLine .= <font color = red>"file: "</font>;</font>
$fileLine .= <font color = red>""</font>;
}
$fileLine .= <font color = red>"$theFile &lt;/a&gt;&lt;/td&gt;&lt;td align=\"</font>right\<font color = red>" valign=\"</font>top\<font color = red>"&gt;$theSizek&lt;/td&gt;&lt;/tr&gt;\n"</font>;
print($fileLine);
}
}
}
}
}

?&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Directory Listing&lt;/title&gt;
&lt;/head&gt;

&lt;body bgcolor=<font color = red>"#FFFFFF"</font> text=<font color = red>"#<font color = blue>000000</font>"</font>&gt;
&lt;div align=<font color = red>"left"</font>&gt;
&lt;table border=<font color = red>"<font color = blue>0</font>"</font> cellpadding=<font color = red>"<font color = blue>5</font>"</font> cellspacing=<font color = red>"<font color = blue>0</font>"</font> width=<font color = red>"<font color = blue>95</font>%"</font>&gt;
&lt;?
$pathParts = pathInfo($HTTP_SERVER_VARS[<font color = red>"PATH_TRANSLATED"</font>]);
$<font color = purple>self</font> = $pathParts[<font color = red>"basename"</font>];
$topDir = $pathParts[<font color = red>"dirname"</font>];
<font color = green>if</font> (!($currDir)) {
$currDir = <font color = red>"/"</font>;
}
listDir($currDir,$<font color = purple>self</font>,$topDir);
?&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

</font>[/code]
Note the lines that define "foldericon" and "fileicon". If you want icons, put the (relative or absolute) image paths here and they'll show up before each item. You can also edit lines 45 and 62 to provide a text descriptor for each item (eg: "dir:" and "file:"). I've already put in ALT tags, so this should work with text-based browsers as well.

If MacNN's boards mangle the above code, you can also download it from http://www.isaka.net/misc/dir_list.php.sit.gz

Good luck, and sorry about the lack of documentation/commented code - it was a quick hack.

[ 07-11-2001: Message edited by: dogzilla ]
     
Mac Elite
Join Date: Mar 2001
Status: Offline
Reply With Quote
Jul 16, 2001, 05:50 PM
 
Wow. if you want a hack then just copy this into a text file and save it as anything.php and copy it to whatever directory you'd like. (I'd save it as index.php or something so whenever someone browses to that directory, it will default to this directory lister page, as long as you have index.php as your default page. anyway. lemmie know if you need more help. these forums are so slow i don't read them as much lately. my email is osx@raman-sinha.com if you need more hack code written.

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
&lt;?php
$directoryhandle=opendir('.');
<font color = green>while</font> ($file = readdir($directoryhandle)){
echo <font color = red>"&lt;A href='$file'&gt;$file&lt;/a&gt;&lt;br&gt;"</font>;
}
closedir ($directoryhandle);
print(<font color = red>"&lt;p&gt;Finished with directory listing...&lt;/p&gt;"</font>);
?&gt;
</font>[/code]
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Jul 17, 2001, 12:21 AM
 
And this is a bit late for now, but a good (and cheap!!) book I found was 'Teach yourself PHP4' from SAMS. It's easy, straightforward (for designers like me who are still a bit green when it comes to programming) and has great examples.

[ 07-17-2001: Message edited by: Simon Mundy ]
Computer thez nohhh...
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:41 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2