Appending with Perl

I want to append a link and text in to a html document.  The link comes from a file a user uploads and the text is a discription of the file uploaded. Thanks
Wild_Bill_2000Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
maneshrConnect With a Mentor Commented:
ok . here is the html file and script that should help you with what you are doing.

notice now that you would have to start with a html file instead of the PERL script.

NOTE: you will have to change the path variables in the PERL script as perl your server directory structure.

===============exchange.html
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML><HEAD><TITLE>Caos Research Report Upload Area</TITLE>
</HEAD><BODY><BODY BGCOLOR="#ffffff">      
<CENTER><H1>Caos Research Report Upload Area</H1></CENTER>
<HR>Please select a report to download or upload your own.<br>
Note: uploading only works with Netscape 2.0 and above
<P>
Select the <VAR>browse</VAR> button to choose a file
to upload.
<FORM METHOD="POST" action="/cgi-bin/exchange.pl" ENCTYPE="multipart/form-data"
 >
<H3>Enter the file to upload</H3><BR><INPUT TYPE="file" NAME="filename" VALUE="
" SIZE=45 ><BR>
<INPUT TYPE="reset"><INPUT TYPE="submit" NAME="submit" VALUE="Upload"></FORM>
<A HREF = http://www.thesolutions.com/incoming/data.html>data.html</A><P>
<HR>

=============exchange.pl
#!/usr/local/bin/perl

$html_file="/www/docs/manesh/exchange.html";

use CGI;

$query=new CGI;

$dir_to_store="/tmp";
$base_url="http://www.thesolutions.com/incoming/";

if (($query->param('filename')=~ /^\s+$/ || $query->param('filename')=~ /^$/)){
  print "<B>Enter file name for upload</B><br>\n";
  exit;
}else{
  $DocLocation=$query->param('filename');
  $tmp_DocLocation=$query->param('filename');

  $tmp_DocLocation=~ s/\\/\//g;
  @tmp_DocLocation=split(/\//,$tmp_DocLocation);

  $WebFileName = $dir_to_store."/".$tmp_DocLocation[$#tmp_DocLocation];
   
  open(MYFILE,"> $WebFileName") || die $!;
  binmode MYFILE;
  while($bytesread=read($DocLocation,$data,1024)){
    $size+=$bytesread;
    print MYFILE $data;
  }
  close(MYFILE);
  close($DocLocation);

  open(HTML,">>$html_file") || die $!;
  print HTML "<a href=\"$base_url$tmp_DocLocation[$#tmp_DocLocation]\">$tmp_Doc
Location[$#tmp_DocLocation]</a><BR>\n";
  close(HTML);

  print "Content-type: text/html\n\n";
  print qq{<META HTTP-EQUIV="REFRESH" CONTENT="0; URL=/manesh/exchange.html">\n
};
}
0
 
maneshrCommented:
can you pl. provide more details?

Eg. a sample html file with the link and text,
the format of the file, if any, which the user uploads.

that way one can provide you with a more accurate and exact solution.
0
 
geotigerCommented:
Not quite clear about "the link comes from a file a user uploads." Do you mean the appended link will link to the file uploaded by the user?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Wild_Bill_2000Author Commented:
At the moment a visitor to my web site can upload a file to a pub dir, the perl that i have reads the directory and then creates links out of the file names.  here is that bit of code

opendir(DIR, "$filedir");       # $filedir from constant above      
local(@files) = readdir(DIR);   # load @files array from readdir(DIR)
    for (@files) {
      if (/\\/) {
        $oldfile = $_;          # store current element in $oldfile
        @path = split (/\\/,$oldfile); # store all path elements in @path array
        $newfile = pop(@path);  # store file name w/o path to $newfile
        # rename old filename with path info to just plain filename
        rename("$filedir"."$oldfile","$filedir"."$newfile");
      }
    next if (/^\./);            # bypass listing for current dir "."
    next if (/^index/);         # or file starting with "index"    
    print "<A HREF = "."$fileurl"."$_".">$_</A><P>\n"; # output html link to file
    }


if ($file = $query->param('filename')) {
    print "<HR>\n";
    $newfile = $file;
    if (/\\/, $file) {
          $oldfile = $file;
      @path = split(/\\/, $oldfile);
      $newfile = pop(@path);
    }
    print "<A HREF = "."$fileurl"."$newfile"."><H2>$newfile</H2></A>\n";
    open(OUTFILE, ">$filedir"."$newfile");
    while (<$file>) {
    print OUTFILE "$_";
    }
    close OUTFILE;
}


however, i  would like the perl script to just add to an html file, instead of creating one on the dynamically everytime the page loads.  Because my goal is to allow my visitors to add a description to the files they are uploading, b/c most of the files they are uploading need a couple of sentences to make a fair description.  thanks again
0
 
maneshrCommented:
i have some questions about the code that you have given.

from what i have understood from the above code, you are reading the entire directory (minus ., .. & files starting with index) and printing the remaining entires as HTML links.

Q) print "<A HREF = "."$fileurl"."$_".">$_</A><P>\n"; # output html link to file

where is the file that you are printing to?? or is the comment itself wrong??

in the next part you start with a if statement.

if ($file = $query->param('filename')) {

what is the purpose of this statement??
is it supposed to do a comparison between $file and the user entered value?? or do you want to do a simple assignment??

Also pl. explain what you are trying to do after that or at least what you want done/are expecting from that piece of code?
0
 
Wild_Bill_2000Author Commented:
if you go to http://www.thesolutions.com/cgi-bin/exchange.pl
you can see it in action.  I want to allow a user to also add a description to go along with the file name.  short of going through a DB i think the easiest way would be to use a 2nd html file and just append to the second html page.  I have seen this before, but i just cannot remember where.
thanks again
0
 
maneshrCommented:
i checked out and in fact even uploaded a file (ee.txt).

i have another Q though.

now lets say you were to convert this dynamic page (exhcange.pl) to a static page (which can be done), how would you distinguish between users and which users gets to see which file??

OR would you be serving the same static HTML file to EVERY user who comes to that page??

let me know
0
 
Wild_Bill_2000Author Commented:
I would be serving the same page to every user, when it is implemented it will be within a secure site, and only those with access will know its there.
0
 
Wild_Bill_2000Author Commented:
Comment accepted as answer
0
 
maneshrCommented:
was there anything additional that you were looking for in the solution?

how close was my comment to your expected answer??

let me know
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.