You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Leif Snorre Schøyen Boasson <bo...@stud.ntnu.no> on 2002/10/22 18:08:07 UTC

Problems writing binary uploaded data...

I'm trying to save an uploaded binary file (a jpg) through a perlscript.
The code doing this looks like:

   if (open OUTFILE, ">/var/www/tmp/test.jpg"){

       binmode $ULFILE, ":raw";
       binmode OUTFILE, ":raw";

       while ($sizeread=read($ULFILE, $buffer, 1024)) {
           print OUTFILE $buffer;
           $size += $sizeread;
       }

       print "<br>Read ",$size," bytes.";
   }

Where ULFILE is the filehandle for the uploaded file, gotten from an
Apache::Request object. Now, the sum of the sizes reported by the
read-command is correct for the original jpg file I use to test, but the
written file is somewhat smaller (7192 bytes smaller), so I figure
something gets lost in the writing. But I cant figure out how or why...

...anybody see some reason why I shouldnt get the exact binary data out?


Re: Problems writing binary uploaded data...

Posted by Issac Goldstand <ig...@followmecorp.com>.
Well, there are 2 issues here:
1) Getting $upload->link to work (yes, you can do it if you really want
to:-))
2) Fixing your original problem

So:

1) Use TEMP_DIR to set a different spool directory for the uploaded file:

>From the same manpage: (under methods to pass $apr->new())

TEMP_DIR
Sets the directory where upload files are spooled. On a *nix-like that
supports link(2), the TEMP_DIR should be located on the same file system as
the final destination file:
 my $apr = Apache::Request->new($r, TEMP_DIR => "/home/httpd/tmp");
 my $upload = $apr->upload('file');
 $upload->link("/home/user/myfile") || warn "link failed: $!";

2) I'm not really sure - I only gave your problem a rather casual look-over,
but my first instinct is to add close (OUTFILE); to the end of your code -
perhaps it's not  flushing the output buffer to the file since you never
explicitly close the filehandle...

  Issac

----- Original Message -----
From: "Leif Snorre Schøyen Boasson" <bo...@stud.ntnu.no>
To: "Issac Goldstand" <ig...@followmecorp.com>
Cc: <mo...@perl.apache.org>
Sent: Tuesday, October 22, 2002 8:51 PM
Subject: Re: Problems writing binary uploaded data...


> Well, the original reason I didn't use that was I didn't know what
> "linking" it was used for... (so much for RTFM... :) ).
>
> However, my /tmp (which Apache is using as it's tmp-directory) is on its
> own partition, so I cant really link it anywhere else but /tmp, which is
> not exactly where I'd like it to go... Is there any other way to get it
> saved directly like that, and not having to cp it somewhere else?
>
> (and I'd still like to know why my original code doesn't work... :) )
>
> Thanks alot for the advice, though!
>
> cheers
> Snorre
>
> On Tue, 22 Oct 2002, Issac Goldstand wrote:
>
> > Um... Why don't you simply use $upload->link on the handle?
> >
> > <From Apache::Request manpage>
> >   my $upload = $apr->upload('file');
> >   $upload->link("/path/to/newfile") or
> >       die sprintf "link from '%s' failed: $!", $upload->tempname;
> > </From Apache::Request manpage>
> >
> >   Issac
> >
> > ----- Original Message -----
> > From: "Leif Snorre Schøyen Boasson" <bo...@stud.ntnu.no>
> > To: "mod_perl" <mo...@perl.apache.org>
> > Sent: Tuesday, October 22, 2002 6:08 PM
> > Subject: Problems writing binary uploaded data...
> >
> >
> > > I'm trying to save an uploaded binary file (a jpg) through a
perlscript.
> > > The code doing this looks like:
> > >
> > >    if (open OUTFILE, ">/var/www/tmp/test.jpg"){
> > >
> > >        binmode $ULFILE, ":raw";
> > >        binmode OUTFILE, ":raw";
> > >
> > >        while ($sizeread=read($ULFILE, $buffer, 1024)) {
> > >            print OUTFILE $buffer;
> > >            $size += $sizeread;
> > >        }
> > >
> > >        print "<br>Read ",$size," bytes.";
> > >    }
> > >
> > > Where ULFILE is the filehandle for the uploaded file, gotten from an
> > > Apache::Request object. Now, the sum of the sizes reported by the
> > > read-command is correct for the original jpg file I use to test, but
the
> > > written file is somewhat smaller (7192 bytes smaller), so I figure
> > > something gets lost in the writing. But I cant figure out how or
why...
> > >
> > > ...anybody see some reason why I shouldnt get the exact binary data
out?
> > >
> >
>


Re: Problems writing binary uploaded data...

Posted by Leif Snorre Schøyen Boasson <bo...@stud.ntnu.no>.
Well, the original reason I didn't use that was I didn't know what
"linking" it was used for... (so much for RTFM... :) ).

However, my /tmp (which Apache is using as it's tmp-directory) is on its
own partition, so I cant really link it anywhere else but /tmp, which is
not exactly where I'd like it to go... Is there any other way to get it
saved directly like that, and not having to cp it somewhere else?

(and I'd still like to know why my original code doesn't work... :) )

Thanks alot for the advice, though!

cheers
Snorre

On Tue, 22 Oct 2002, Issac Goldstand wrote:

> Um... Why don't you simply use $upload->link on the handle?
>
> <From Apache::Request manpage>
>   my $upload = $apr->upload('file');
>   $upload->link("/path/to/newfile") or
>       die sprintf "link from '%s' failed: $!", $upload->tempname;
> </From Apache::Request manpage>
>
>   Issac
>
> ----- Original Message -----
> From: "Leif Snorre Schøyen Boasson" <bo...@stud.ntnu.no>
> To: "mod_perl" <mo...@perl.apache.org>
> Sent: Tuesday, October 22, 2002 6:08 PM
> Subject: Problems writing binary uploaded data...
>
>
> > I'm trying to save an uploaded binary file (a jpg) through a perlscript.
> > The code doing this looks like:
> >
> >    if (open OUTFILE, ">/var/www/tmp/test.jpg"){
> >
> >        binmode $ULFILE, ":raw";
> >        binmode OUTFILE, ":raw";
> >
> >        while ($sizeread=read($ULFILE, $buffer, 1024)) {
> >            print OUTFILE $buffer;
> >            $size += $sizeread;
> >        }
> >
> >        print "<br>Read ",$size," bytes.";
> >    }
> >
> > Where ULFILE is the filehandle for the uploaded file, gotten from an
> > Apache::Request object. Now, the sum of the sizes reported by the
> > read-command is correct for the original jpg file I use to test, but the
> > written file is somewhat smaller (7192 bytes smaller), so I figure
> > something gets lost in the writing. But I cant figure out how or why...
> >
> > ...anybody see some reason why I shouldnt get the exact binary data out?
> >
>


Re: Problems writing binary uploaded data...

Posted by Issac Goldstand <ig...@followmecorp.com>.
Um... Why don't you simply use $upload->link on the handle?

<From Apache::Request manpage>
  my $upload = $apr->upload('file');
  $upload->link("/path/to/newfile") or
      die sprintf "link from '%s' failed: $!", $upload->tempname;
</From Apache::Request manpage>

  Issac

----- Original Message -----
From: "Leif Snorre Schøyen Boasson" <bo...@stud.ntnu.no>
To: "mod_perl" <mo...@perl.apache.org>
Sent: Tuesday, October 22, 2002 6:08 PM
Subject: Problems writing binary uploaded data...


> I'm trying to save an uploaded binary file (a jpg) through a perlscript.
> The code doing this looks like:
>
>    if (open OUTFILE, ">/var/www/tmp/test.jpg"){
>
>        binmode $ULFILE, ":raw";
>        binmode OUTFILE, ":raw";
>
>        while ($sizeread=read($ULFILE, $buffer, 1024)) {
>            print OUTFILE $buffer;
>            $size += $sizeread;
>        }
>
>        print "<br>Read ",$size," bytes.";
>    }
>
> Where ULFILE is the filehandle for the uploaded file, gotten from an
> Apache::Request object. Now, the sum of the sizes reported by the
> read-command is correct for the original jpg file I use to test, but the
> written file is somewhat smaller (7192 bytes smaller), so I figure
> something gets lost in the writing. But I cant figure out how or why...
>
> ...anybody see some reason why I shouldnt get the exact binary data out?
>