Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Appending with Perl

Posted on 2000-05-16
10
Medium Priority
?
259 Views
Last Modified: 2010-03-05
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
0
Comment
Question by:Wild_Bill_2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2814196
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
 
LVL 12

Expert Comment

by:geotiger
ID: 2814323
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
 

Author Comment

by:Wild_Bill_2000
ID: 2814409
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 16

Expert Comment

by:maneshr
ID: 2814625
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
 

Author Comment

by:Wild_Bill_2000
ID: 2814758
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2814939
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
 

Author Comment

by:Wild_Bill_2000
ID: 2817226
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
 
LVL 16

Accepted Solution

by:
maneshr earned 300 total points
ID: 2818080
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
 

Author Comment

by:Wild_Bill_2000
ID: 2849800
Comment accepted as answer
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2850469
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question