Solved

Appending with Perl

Posted on 2000-05-16
10
253 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
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 100 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl rename 2 144
delete query using perl dbi 3 100
Filtering a file to table 9 101
Bash one liner, start and end time calculation 13 110
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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

770 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