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

Hello dear monks, I have a script that displays the content of a folder, the thing is that the script works half way, you see after displaying the list of files listed there if you try to save the file like "save link as" or even click on the file it gives you an error message this is the error message :
Internal Server Error The server encountered an internal error or misconfiguration and was u +nable to complete your request. Please contact the server administrator, you@your.address and inform t +hem of the time the error occurred, and anything you might have done +that may have caused the error. More information about this error may be available in the server error + log. Oracle HTTP Server Powered by Apache/1.3.22 Server at localhost Port 8 +0
This is the whole code that I have right now
#!c:/perl/bin/perl.exe use strict; use CGI; my $list = new CGI; my $fileDir = "c:/files/err"; my @files; opendir DIR, "$fileDir" or die "Can't open $fileDir $!"; @files = grep { /\.(?:txt|err|csv)$/i } readdir DIR; closedir DIR; print $list->header("text/html"), $list->start_html("Archivos in $fileDir"), $list->p("Estos son los archivos de $fileDir"); foreach my $file (@files) { print $list->p( $list->a({-href=>$file}, $file) ); } print $list->end_html;
Can someone help me ?

Replies are listed 'Best First'.
Re: Getting the link to work
by glide (Pilgrim) on Dec 26, 2007 at 16:07 UTC
    Hi,

    It's your CGI executable by the Apache server?

    Check the log file of the Apache, you'll find the error there.
      Hi yes is apache 1.3 these is the error message that is saying :
      [Wed Dec 26 10:28:05 2007] [error] [client 127.0.0.1] ReadFile(c:/orac +le/ora92/apache/apache/cgi-bin/35854785.err) failed [Wed Dec 26 10:28:05 2007] [error] [client 127.0.0.1] c:/oracle/ora92/ +apache/apache/cgi-bin/35854785.err is not executable; ensure interpre +ted scripts have "#!" first line [Wed Dec 26 10:28:05 2007] [error] [client 127.0.0.1] couldn't spawn c +hild process: c:/oracle/ora92/apache/apache/cgi-bin/35854785.err

        The answer is right there, it says your script isn't executable. It's not being recognized as as a program. Possibly because it's named .err and not .pl or .cgi.

        Frank Wiles <frank@revsys.com>
        www.revsys.com

        You have problems with permissions, as you can see in the 2nd line of you log. Check the permissions of the c:/perl/bin/perl.exe, the apache user need read and execution permissions to it.
Re: Getting the link to work
by rgiskard (Hermit) on Dec 26, 2007 at 16:23 UTC
    In the case you cannot find your errorlog file, you can add use CGI::Carp qw(fatalsToBrowser); to your cgi script and get the errors sent to your browser.
      Hi the error log is saying this
      [Wed Dec 26 10:28:05 2007] [error] [client 127.0.0.1] ReadFile(c:/orac +le/ora92/apache/apache/cgi-bin/35854785.err) failed [Wed Dec 26 10:28:05 2007] [error] [client 127.0.0.1] c:/oracle/ora92/ +apache/apache/cgi-bin/35854785.err is not executable; ensure interpre +ted scripts have "#!" first line [Wed Dec 26 10:28:05 2007] [error] [client 127.0.0.1] couldn't spawn c +hild process: c:/oracle/ora92/apache/apache/cgi-bin/35854785.err
Re: Getting the link to work
by NetWallah (Canon) on Dec 26, 2007 at 18:21 UTC
    The first purpose of your script - to display a list of files - seems to work fine.

    If I understand your requirement correctly, the second purpose is
    to be able to CLICK on one of the file names, and have it display the contents of that file.

    In order to achieve the second purpose, your LINKS need to be coded correctly, depending on the type of file being displayed.

    If the files listed were HTML files, everything would work as you expected.(.txt files should work as well). However, for file types not recognized by apache, you need to provide non-default handlers for each file-type you expect.

    Instead of your "-href=>$file" link, you need to link it to code (CGI script) that will display the contents of the file.

    You will end up with something like "-href=>'/displayfile.cgi?filename=$file&DIR=$fileDir'".

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

      But how can I do that??, I just need to download the file, or be able to save the file on another computer.
        You will need to understand and send the appropriate "content-type" as a part of the HTML header information that you return to the requesting web browser.

        Typically, browsers already understand what to do with "html" and "text" type documents, and are assisted by "content-type" information which typically contains "text/html". You will need to send something like "application/zip" as the content-type.

        You will also need to brush up on Multipart MIME type messages. I suggest you get a HTML programming book.

        Happy learning!

             "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Re: Getting the link to work
by narainhere (Monk) on Dec 26, 2007 at 17:34 UTC
    Hey Armando I copied and executed you program..It's fine upto listing the contents of directory.But when you start downloading file instead of the actual-path
    i.e"c:/files/err"
    it's taking
    http://server/servlet-name/cgi-bin/<file-to-download>
    This can be resolved by coding the actual path in the script as
    foreach my $file (@files) { print $list->p( $list->a({-href=>$fileDir."/".$file}, $file) ); }

    The world is so big for any individual to conquer

      Hi it work thank you but now a new thing, when I click the filename that has the link and it gives me this error message Firefox doesnt know how to open this address; because the protocol (c) isnt associated with any problem. can you help me ??

        The 1st step is configuring your apache to permit the download of the files you need, for example with the URL http:://localhost/files/err/35854785.err.

        Now that you can download, redirecting the file request with

        $list->a({-href=>"/files/err/".$file}, $file) );

        This is the simples way to resolve the problem, AFAICS.
Re: Getting the link to work
by pileofrogs (Priest) on Dec 26, 2007 at 17:04 UTC

    It looks like you've got basic web server cgi setup problems.

    I recommend making a super-simple "hello world" CGI and making that work before moving on.

      Actuallly "Hello world" works fine, listen I need this, the program below puts in a web page the contents of the folder, only files, what I need to do is be able to download those file from that folder. but if I try to use "Save Link As.." it give me an error message or if I click on the file it give an error message.