Uploading Binary Data using Perl

I'm using the code below to upload images to a server.

open (OUTFILE,">>$outfile")|| warn "Error $!: cannot open $outfile";;
        while ($bytesread=read($infile,$buffer,1024)) {
        print OUTFILE $buffer;
        $tmp = $bytesread;
        $size = $size + $tmp;
       print "$size <BR>\n";

I wrote this well over 10 years ago, but it's still works fine. That is, until I try to upload certain music (MP3) files. The filesize is zero when it gets created on the server.

Here's what works
1. Images I create and save.
2. Audio Files I create and save.
3. Video files I create and save.

Here's what doesn't work
1. Audio files that I pulled of of a CD or downloaded online.

Filesize doesn't seem to be an issues. I uploaded a 15mb image file but can't upload an 8mb mp3 file.

I'm not trying to do anything illegal here and if it doesn't work, it doesn't work .
But it seems strange I can't upload commercial audio files this way. Could it be some copy protection in the MP3 files that stops it from being copied? But I am able to make multiple copies of the file using just my desktop. Odd.

Any ideas?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

what are (stat($infile))[7] and (stat($infile))[2]?
Does it matter which server you use?
nachtmskAuthor Commented:
I'm not sure how to pull places 2 and 7 out. Below is what I get when I just run stat against one of the files I'm having trouble with. I've only tried it so far on one server, Ubuntu.  

File: `test.mp3'
  Size: 5521085         Blocks: 10808      IO Block: 4096   regular file
Device: ca01h/51713d      Inode: 4169778     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/ myusername)   Gid: ( 1000/ myusername)
Access: 2015-03-27 16:39:59.000000000 +0000
Modify: 2015-03-27 16:40:23.000000000 +0000
Change: 2015-03-27 16:40:23.000000000 +0000
 Birth: -
Kim RyanIT ConsultantCommented:
There are some useful notes here about handling file uploads: http://search.cpan.org/~leejo/CGI-4.14/lib/CGI.pod#Processing_a_file_upload_field

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Teraplane: while this question has gotten a little stale, I don't see how the link you provided sheds any light on the problem. The code Nachtmsk is using is not quite the same as that in the link, but as far as I can see it's equivalent in function. Not only that, the code works fine for his other uploads.

I'd want to know more about how the value of $infile gets set, since that happens outside the code snippet we were given.

 I'd also want to know if the upload also fails when the browser doing the upload is running on another OS.

Nacht, can you tell us some more about what's going on here?
Kim RyanIT ConsultantCommented:
Hi jmcg, that is fair enough. I cited that code because I remember having problems with web uploads at one stage, and this was the only material I could find that gave some insight to the issue. It helped me a lot, so thought it may be useful here too.

<off_topic> I haven't used EE for years, but the volume of questions has really dropped a lot. Would be very hard to get enough points to even get free premium status now. I'm using Stack Overflow but problems there from heavy handed moderators.</off_topic>
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.