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

in cgi program if i want to display pdf files from some other directory ex /view/dvsview/vobs/ps/projects/xx.pdf which is not ubder htdocs directory how to display that file in web it always looks for htdocs directory when i am running that program can u please help me my program looks like this can u please send a mail to jyothil@yahoo.com
#!/usr/local/bin/perl print "Content-type:text/html\n\n"; print "<html>"; print "<head>"; print "<title>xx </title>"; print "<body bgcolor=lightblue>"; print qq[ <SCRIPT LANGUAGE= javascript> function back() { history.go(-1); } </SCRIPT>]; $ct=$ENV{"CONTENT_TYPE"}; $cl=$ENV{"CONTENT_LENGTH"}; $xy="/view/dvsview/vobs/ps/projects/linkair/docs/internal/servdefns/ip +relaysd"; #$xy1="/www/apache-1.3.19/cgi-bin/ps"; $op=opendir(DIR,$xy); #print "reurn opendir = $op"; @files=readdir(DIR); print qq[<p><a href="../../t1/lr_sd.pdf">files</a></p> ]; foreach $files(@files) { if($files =~/pdf$/) {$pq=$xy.'/'.$files; print qq [ <center> <table border="1" width=10%> <tr><td> <p><a href="$pq">$files</a></p> </td></tr> </table> </center> ]; } } closedir(DIR); &successsubmit("Doc successfully displayed!!","navy"); sub successsubmit { my($printstr,$color) = @_; print "<form><center>"; print "<br><br><br><br>"; print "<font color=$color font size=6>"; print "<b>$printstr</b>"; print "</font>"; print "<br><br><br>"; print "<font size=3>"; print "<input type=\"button\" value=\"BACK\" onClick=\"back()\">"; print "</font></center>"; print "</form>"; }

Edit kudra, 2001-10-05 Added code tags

Replies are listed 'Best First'.
Re: pdf file display
by jj808 (Hermit) on Oct 05, 2001 at 11:30 UTC
    If you have access to the Apache httpd.conf file, then define an alias to your PDF directory, e.g.
    Alias /pdf/ /view/dvsview/vops/ps/projects/
    You will then be able to link to http://yoursite/pdf/xx.pdf

    If this is not possible, then try writing a 'wrapper' along the lines of

    #! /usr/bin/perl -w use strict; use CGI qw(:standard); my $basedir = "/view/dvsview/vobs/ps/projects/"; my $file = param('file'); open PDF,"<$basedir$file" or die("Cannot open $file: $!"); print "Content-type: application/pdf\n\n"; print while (<PDF>);
    Save this as 'view.pl', then link to http://yoursite/cgi-bin/view.pl?file=xx.pdf

    If you use this method then I would strongly suggest putting some better error checking in the code (you will get a server error if the open fails).

    Hope this puts you on the right track,

    JJ

Re: pdf file display
by pixel (Scribe) on Oct 05, 2001 at 12:33 UTC

    If you want to display a file that lives outside your web document tree, then you'll have to write a program that reads the file from the filesystem and writes it to STDOUT. Something like this:

    #!/usr/bin/perl -wT use strict; use CGI qw(header param); my $file = param($file); my $dir = '/path/to/pdf/files'; # Ensure that filename is _just_ a pdf filename if ($file =~ /^(\w+\.pdf)$) { $file = $1; } else { die "bad file name: $file\n"; } print header(-type=>'application/pdf'); open(PDF, "$dir/$file") or die "Can't open $dir/$file: $!\n"; binmode PDF; my $buff; while (read(PDF, $buff, 1024)) { print $buff; }

    Blessed Be
    The Pixel

Re: pdf file display
by C-Keen (Monk) on Oct 05, 2001 at 11:26 UTC
    First of all you should make sure that we are able to read your question, so that answers are even possible. Please use break tags and such as well, though you know the bold tag already. Wrap your code in code tags. Thank you.

    Do you want to display a list of pdf files in a certain directory or the pdf file itself? I am not sure.
    For the access problem: This is a configuration problem of you r web server. Maybe you want to add a symbolic link and set the rights in an .htaccess file. (I am persuming that you use apache if not, have a look at the documentation there). You may want to look at apache's website to look up the right settings.

    Perl related stuff: Whenever you want to output some html stuff use cgi. Try to run a search on cgi on this site. It will give you tons of nodes to look at. Also try perldoc CGI at your command line.

    Hope this helps,
    C-Keen

    P.S.:This answer may seem a bit harsh but this place is not for doing somebodies work, just to learn how stuff is to be done. To do so you have to fiddle with it yourself

    Update: Thanks kudra for reformatting