Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm a bit of a novice here, so please be patient if some of my questions have "obvious" answers. To this point I have been able to answer many of my questions through existing posts, but not with this one. Anyway...

Is there an existing script/library function that will convert an ftp directory listing to an html page? For various reasons I am unable to directly open the ftp directory in a web browser (I would be forced to use ftp://user:pass@host, but can not have the password shown in the address in the browser; IE is too stupid to ask for the password is I simply use ftp://user@host; and various other problems I won't go into...). I can use Net:FTP to get a directory listing from the FTP site, but now need to use that to generate an html page much like the one you would see when you open an ftp directory into a web browser normally (with links for each file and directory, etc.)

It seems like there should be something in place to do this already, since web browsers seem to do it just fine -- any suggestions?

Thanks.

  • Comment on How to generate an HTML page from an FTP directory listing?

Replies are listed 'Best First'.
Re: How to generate an HTML page from an FTP directory listing?
by THRAK (Monk) on Mar 28, 2001 at 21:40 UTC
    If you are using Net::FTP to get a listing then you've alreadly done half of the work. Net::FTP's list function should give you all of the files in an array. After you have that just print the start of the page either using CGI.pm or you can hand code it (best done in a here doc). Then just use a foreach loop to print each entry:

    foreach my $file (@file_list) { print "<p>$file</p>\n"; #This is basic, format as required. }


    After that just send the page ending stuff (</body>, </html>) the same way you generated the beginning of the page.

    -THRAK
    www.polarlava.com
      (from original author)

      It's the formatting part that I have a question on. Is there an existing script/tool that will take the directory listing and format it for me (creating links for each file/directory, etc.), or do I need to do that manually? (I know, I know, doing it manually isn't that big a deal for you experienced guys out there, but when you're just starting out, just about anything can be intimidating...) :)

      Thanks again.

        Wouldn't something as simple as:

        print <<"END" <a href="ftp://server/path/$file">$file</a><br> END
        work?

                - tye (but my friends call me "Tye")
        Well, there's probably nothing "out of the box" that does exactly what you want (yet, because I've noted this problem for a future column idea), but take a look at my past columns for ideas on how to generate links based on names, and how to respond to those links when selected.

        -- Randal L. Schwartz, Perl hacker

Re (tilly) 1: How to generate an HTML page from an FTP directory listing?
by tilly (Archbishop) on Mar 29, 2001 at 01:21 UTC
    If you are using Apache, you can just allow the server to display that page and turn on FancyIndexing to let people see it.

    If you aren't, you can install Apache somewhere and play around with its standard icon's and links to design a CGI script to do what you want. I did this, once, for my wife for a script to make it easy for her to navigate a CD of family photos that her father sent. As I recall it was pretty routine.

      Xitami does as well, but I don't know of any other server that does allow to display the "readme" file inside a directory as the leading text before the directory listing.
      But in fact, even if no other documents then really public ones should be in that directory it seems to be better to not to use this feature of webservers but instead building the structure as required or requested only to the frontend, so you have for example a DB containing your files and can (just as large image archives do) let them browse the (virtual) directories like ftp but structured by keywords. In this way it's also easy to prevent others from even knowing of ftp-sections which aren't (or shouldn't be)of their interest and at least as an admin you'd get some fewer warnings about denied file access attempts.
      Ok, I know thats beyond the scope of the question. just an idea. :-)

      Have a nice day
      All decision is left to your taste
Re: How to generate an HTML page from an FTP directory listing?
by Desdinova (Friar) on Mar 28, 2001 at 22:42 UTC
    A while back I had to provide web access to an FTP server and found something called Web-Ftp It is written in Perl. Idont know if this would be a drop in solution for you but it could give something to work off. It is a GPL/Artistic Licenence.

    Disclaimer: It has been a while since I have used this. Also i used it for only in-house people and didnt go over the security of the script so use with caution