Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2005
    Posts
    5

    Talking Unanswered: Simple download file script doing strange things.

    I have a fairly simple script which utilizes a template to tell it where to look for a file. I pass a hidden variable and use param to get it. The program locates the file, and I can save it to my hard drive with the save as dialog box which opens up. The only problem is, for some strange reason, all the carriage returns are replaces by little squares. It makes the file totally unusable. I got the same problem when I used another program to send me an email with the file as a file attachment. The same thing happened. Little squares instead of proper carriage returns at the end of the line.

    The file must be delivered exactly as it is on my server. Some of the ends of lines are nothing but spaces. A line can be a few characters followed by 120 spaces followed by a new line character. I have to have every single space just exactly as it is on the server. It is created by a form on my site. I can drag the file in my FTP program from my server to my local browser window, and open it in notepad just fine. So it is correct on the server. Why doesn't the save as dialog save the file properly?

    here is the download a file code..


    #!/usr/bin/perl

    use CGI ':standard';
    use CGI::Carp qw(fatalsToBrowser);

    my $files_location;
    my $ID;
    my @fileholder;
    my $subdirectory=param('subdirectory');
    $ID = param('ID') . "du.txt";

    $files_location = "/home/mortga20/public_html/$subdirectory/";
    #print "Content-type: text/html\n\n";
    #print "files_location is " . $files_location;
    #exit;
    if ($ID eq '') {
    print "Content-type: text/html\n\n";
    print "You must specify a file to download.";
    exit;
    }

    open(DLFILE, "<$files_location/$ID") || die ("Can't open file!");
    @fileholder = <DLFILE>;
    close (DLFILE);

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

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



    Thanks for your help.

    Scot

  2. #2
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    You are aware of differences between a *nix file and a Windows file, I presume. Try using a decent editor like UltraEdit on the file. Oh wait, if you use UltraEdit you could use the open from FTP feature.

  3. #3
    Join Date
    Mar 2005
    Posts
    5

    This isn't working

    Doesn't anyone know how ot fix this? Someone suggested I try binmode -f. THis didn't do anything. I did it right after opening the file.

    I'm trying to get my text file from my server to my hard drive without changing the end of line characters to little squares.

  4. #4
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    No, nobody here knows how to fix this.

    Quote Originally Posted by sking
    I'm trying to get my text file from my server to my hard drive without changing the end of line characters to little squares.
    Your file is unchanged. Please note that you get them little squares because

    Unix uses a LF (line feed; ^J or 0x0A), Mac uses a CR (carriage return; ^M or 0x0D), and Dos uses CRLF (^M^J or 0x0D0A).

    So slurp the file, split with the appropriate terminator, join with the appropriate terminator and write it out. If the server is Unix, Linux then split with LF. Join with "$/".

    Or slurp the file and run a regex such as "s|\012|$/|g" then write it out.

    Or do it with a regex as a one liner.

    perl -pi -e 's!\012{1,2}!$/!g' <file>
    Last edited by senza_nome; 03-06-05 at 11:14.

  5. #5
    Join Date
    Mar 2005
    Posts
    5

    Still need help on making this work.

    !/usr/bin/perl

    $file="../form1/1001du.txt";
    open(IN, $file) || die ("can't open file");
    &slurpfile ($file);
    @fields=split(/\012/, $text);
    $newlines=join($/,@fields);
    print "Content-type: text/html\n\n";
    print $newlines;


    print header( -type => 'application/x-download',
    -attachment => $ID,
    );

    #print $_ while (read(DLFILE, $_, 1024));
    #close (DLFILE) || die ("Can't close file!");

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

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

    sub slurpfile {

    local( $/, *FH ) ;
    open( FH, $file ) or die "unable to read file\n";
    $text = <FH>;
    }

    I tried to get this to work, but to no avail. I'm not sure how to fake the headers to the browser so that it presents the file save as dialog. I'm trying to convert a text file on a unix server to remove the LF character and make it windows compatible. (remove the pesky squares seen in notepad) I open the file, slurp it in, do the split and join, and then I need to present that string in a file save as. Does this routine have to read from a file? Can it use the string I just made? I tried to split and join the file back using $/ as glue. Does anyone know how to make this work?

  6. #6
    Join Date
    Mar 2005
    Posts
    5

    I found this code.. but I can't get it to work

    I tried to run this from my browser with a pathtoprogram.pl?../text.txt but it didn't work.


    #!/usr/bin/perl

    ################################################## #############################
    ################################################## #############################
    #
    # To convert linbreaks in a text file to MS-DOS/Windows format
    #
    # Version 1
    #
    # by Andrew Hardwick, http://duramecho.com, 2002/4/11
    #
    # Released under GNU Public Licence.
    #
    ################################################## #############################
    ################################################## #############################
    #
    # How To Use:
    #
    # Run from a command line with the file name (path) as arguement.
    #
    ################################################## #############################
    ################################################## #############################

    # Include libraries
    use Cwd; # To find current directory
    use strict; # Disenable automatic variables

    my ($Text);
    my ($TextUnix,$TextMac,$TextDos);
    my ($CountUnix,$CountMac,$CountDos);
    #$Text="../form1/1003du.txt";
    Get data from file
    unless($ARGV[0])
    { die("Error: file name not specified to read.");}
    unless(open(FILE,'+<'.$ARGV[0]))
    { die("Error: Cannot open $ARGV[0] to read.");}
    binmode FILE;
    read FILE,$Text,-s "../form1/1003du.txt";
    #read FILE,$Text,-s $ARGV[0];
    # Convert linebreaks trying different possibilities & counting number found
    $CountUnix=(($TextUnix=$Text)=~s/\x0A/\x0D\x0A/g);
    $CountMac=(($TextMac=$Text)=~s/\x0D/\x0D\x0A/g);
    $CountDos=(($TextDos=$Text)=~s/\x0D\x0A/\x0D\x0A/g);
    # Check which file format it was in
    if(!$CountUnix&&!$CountMac&&!$CountDos)
    { die("Error: No linebreaks found.\n");}
    elsif($CountUnix&&!$CountMac&&!$CountDos)
    { print "Converted from Unix format.\n";
    $Text=$TextUnix;}
    elsif($CountMac&&!$CountUnix&&!$CountDos)
    { print "Converted from Mac format.\n";
    $Text=$TextMac;}
    elsif($CountDos&&$CountUnix==$CountDos&&$CountMac= =$CountDos)
    { die("It is already in DOS format.\n");}
    else
    { die("Error: It appears to be in mixed format. It might not be text.\n");}
    # Write data to file
    seek FILE,0,0;
    print FILE $Text;
    close FILE;

    ################################################## #############################

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •