Need help converting unix text to windows text

Discussion in 'Perl Misc' started by sking623@aol.com, Mar 22, 2005.

  1. Guest

    I have been working on this for weeks. Whatever my FTP client does to
    translate the unix text file with each line ending in a LF character
    back into a LF CR I need some code in Perl to this.

    The task is, save data from an html form into a flatfile database. I
    have a Linux server. The program I have allows the user to search for a
    text file (unix text) and download the file through their browser. It
    has to convert the little LF's to CRLF. I have tried using some
    people's suggestion on how to do this. But it doesn't work yet. Right
    now it serves up the file because I have everything commented out! A
    little messy, but the solution might be hiding in there! I just need to
    know the right way to tweak it...

    Any assistance is greatly appreciated.

    Here's my code:

    #!/usr/bin/perl
    # This file is the one working
    # This is called form the search page from option 1 in menu. You have
    to login and search for a record.
    # use a 1 to search for 1001.txt
    use CGI ':standard';
    use CGI::Carp qw(fatalsToBrowser);

    my $file_location;
    my $ID;
    my @fileholder;

    $subdirectory=param('subdirectory');
    $ID = param('id');
    $file_location = "../" . $subdirectory. "/" . $ID . "du.txt";
    if ($ID eq '') {
    print "Content-type: text/html\n\n";
    print "You must specify a file to download.";
    } else {
    #open(FILE, "$file_location") || Error('open', 'file');
    #binmode (FILE); # will read file as is (binary) it need to send
    images
    #@data=<FILE>;
    #close FILE;
    #print "Content-type: text/html\n\n";
    #print $file_location;
    #print @data;
    #exit;
    open(DLFILE, "<$file_location") || Error('open', 'file');
    binmode -f(DLFILE);
    @fileholder = <DLFILE>;
    close (DLFILE) || Error ('close', 'file');

    #foreach $fileholder (@fileholder) {
    #$fileholder =~ s|\012|\m\n|g;
    #$fileholder =~ s|\012|$/|g;
    #$fileholder = split(/\012/,$fileholder);
    #}
    #print "Content-type: text/html\n\n";
    #foreach $fileholder (@fileholder) {
    #print $fileholder;
    #}
    #exit;
    open (LOG, ">>/home/mortga20/public_html/$subdirectory/log.txt") ||
    Error('open', 'file');
    print LOG "$ID\n";
    close (LOG);

    #print "Content-type:application/x-download\n"; #paste your type
    #binmode STDOUT; # i not shure than need it
    #print join(//, @data); # send client

    print "Content-Type:application/x-download\n";
    print "Content-Disposition:attachment;filename=$ID\n\n";
    print @fileholder
    }

    sub Error {
    print "Content-type: text/html\n\n";
    print "The server can't $_[0] the $_[1]: $! \n";
    exit;
    }
    , Mar 22, 2005
    #1
    1. Advertising

  2. Hello,

    wrote:
    > I have been working on this for weeks. Whatever my FTP client does to
    > translate the unix text file with each line ending in a LF character
    > back into a LF CR I need some code in Perl to this.
    >

    A simple substitute of CR (Unix) to CRLF (DOS) shall do this.
    Look at: http://www.cpan.org/authors/id/G/GO/GONZO/linebreaktool-0.2pre1.pl

    (No, the script is not written by me)

    regards,
    Reinhard
    Reinhard Pagitsch, Mar 22, 2005
    #2
    1. Advertising

  3. <> wrote:

    > Whatever my FTP client does to
    > translate the unix text file with each line ending in a LF character
    > back into a LF CR



    No, you need it back into CR-LF, the order matters.


    > I need some code in Perl to this.



    s/\012/\015\012/;


    > I have tried using some
    > people's suggestion on how to do this. But it doesn't work yet.



    Then you should followup to the article containing the non-working
    suggestion so that we could help you fix it.


    > #!/usr/bin/perl



    You should ask for all the help you can get!

    Especially before asking hundreds of people around the world to
    look at it for you.

    use warnings;
    use strict;


    > use CGI ':standard';



    > $subdirectory=param('subdirectory');
    > $ID = param('id');



    Who will be filling out your web form?

    Any-old-body on the Internet?

    If so, you better have taint checking in your program before you
    put it on a publicly available web server:

    perldoc perlsec


    > $file_location = "../" . $subdirectory. "/" . $ID . "du.txt";



    You should declare your variables in the smallest possible scope.

    Variable interpolation _is_ concatenation, only easier to read:

    my $file_location = "../$subdirectory/${ID}du.txt";


    > print "Content-type: text/html\n\n";



    Here you tell the browser that you will be giving it HTML.


    > print "You must specify a file to download.";



    That doesn't look like HTML.

    You shouldn't lie to the browser like that.


    > open(DLFILE, "<$file_location") || Error('open', 'file');
    > binmode -f(DLFILE);



    It can't be *both* text and binary.

    You must treat it as one or the other.

    Which is it?



    That isn't how you use binmode() anyway, have you read the docs for it?

    perldoc -f binmode


    > @fileholder = <DLFILE>;


    > #foreach $fileholder (@fileholder) {



    Why do you need a "file holder" if you are going to process it
    a line-at-a-time anyway?


    > #$fileholder =~ s|\012|\m\n|g;



    What character does the \m escape give you?

    Why the "g" option when you only have a single line anyway?

    Where did you get this "suggestion" from?


    > #$fileholder =~ s|\012|$/|g;



    If you are on a unixlike system, then that is a no-op.

    What were you hoping that it would accomplish for you?


    > open (LOG, ">>/home/mortga20/public_html/$subdirectory/log.txt") ||
    > Error('open', 'file');
    > print LOG "$ID\n";
    > close (LOG);



    You had better implement file-locking in your CGI program if
    you don't want the log file to become corrupted.


    > #print join(//, @data); # send client



    That is a mighty strange looking 1st argument to join() ...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 22, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    5,733
    Steve W. Jackson
    Apr 12, 2005
  2. Karl Heinz Buchegger
    Replies:
    1
    Views:
    1,961
    vijay
    Aug 6, 2003
  3. Peter Koch Larsen
    Replies:
    2
    Views:
    532
    Karl Heinz Buchegger
    Aug 6, 2003
  4. Replies:
    12
    Views:
    1,611
    Dave Thompson
    Jan 10, 2005
  5. AIM
    Replies:
    0
    Views:
    323
Loading...

Share This Page