If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > Simple download file script doing strange things.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-05-05, 00:29
sking sking is offline
Registered User
 
Join Date: Mar 2005
Posts: 5
Talking 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
Reply With Quote
  #2 (permalink)  
Old 03-05-05, 15:08
senza_nome senza_nome is offline
Registered User
 
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.
Reply With Quote
  #3 (permalink)  
Old 03-05-05, 22:50
sking sking is offline
Registered User
 
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.
Reply With Quote
  #4 (permalink)  
Old 03-06-05, 10:11
senza_nome senza_nome is offline
Registered User
 
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 10:14.
Reply With Quote
  #5 (permalink)  
Old 03-09-05, 02:25
sking sking is offline
Registered User
 
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?
Reply With Quote
  #6 (permalink)  
Old 03-09-05, 22:55
sking sking is offline
Registered User
 
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;

################################################## #############################
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On