bradcathey has asked for the wisdom of the Perl Monks concerning the following question:

Good monks,
I'm uploading a tab-delimited .txt file exported from Excel for a LOAD DATA LOCAL INFILE import into a MySQL table. The problem comes in uploading the file from client to server—apparently I'm losing newlines so the entire file ends up in the first record. If I simply FTP the file, everything is fine. But this is a client application and needs to be controlled from a web-based form.

So, is there anything I can do to upload the file and keep the newlines? TIA.

Here's my upload code FWIW:
sub upload_file { $| = 1; #flush the output buffer my $sourcefile = shift; my ($buffer, $bytes); $sourcefile =~ /([\w .-]+)$/i; #strip off path stuff my $newfile = $1; $newfile =~ s/ //; open (OUTFILE, ">../$newfile") or die "Cannot open $newfile: $!"; # binmode(OUTFILE); while ($bytes = read($sourcefile, $buffer, 1024)) { print OUTFILE $buffer; } close($sourcefile) or die "Close:$!";; close(OUTFILE) or die "Close:$!";; chmod (0666, ">../$newfile"); return $newfile; }
Update: I posted too soon. I thought I had tried it all but it had nothing to do with my uploader, but in my MySQL. I added LINES TERMINATED BY '\r' to my statement. So, this worked:
$stmt ="LOAD DATA LOCAL INFILE '../catalog.txt' INTO TABLE catalog LINES TERMINATED BY '\r'IGNORE 1 LINES";
Yikes, next time I'll RTFM more throughly before posting.;^&

—Brad
"A little yeast leavens the whole dough."

Replies are listed 'Best First'.
Re: File upload losing newlines
by duff (Parson) on Apr 24, 2004 at 22:01 UTC
    If this is a text file, you shouldn't be using binmode, it's for binary files only. What I guess is happening is that the "client" and "server" are different architectures with differeing ideas of line endings. With binmode no line-ending translation is done. Without binmode, line-endings are translated to the native line-ending of the receiving end.
      Thanks duff, I'll pull that line out. I wondered about that one...

      —Brad
      "A little yeast leavens the whole dough."