File download scripts 
Author Message
 File download scripts

Hi,

I'm using the following php code to tar images in a directory and sending
headers so that the result is downloaded:

<?
    ....
    header("Content-Type: application/x-tar");
    header("Content-Length: $file_size_est");
    header("Content-Disposition: attachment; filename=$filename\n");
    passthru('tar -c *.jpg');
    exit();
    ....
?>

This works fine, except when I use the content-length header I can't quite
get it right (and not setting means your browser doesn't tell you how long
it will take to d/l etc). If I sum the file sizes using filesize($file) to
get $file_size_est the tar looks OK but the last file in the tar will be
corrupted (due to missing bytes off the end I think). If I guess and add
bytes then I'll never get it right because this depends on the number of
files in the directory (and setting $file_size_est too big breaks some
browsers).

Any ideas? (short of writing it out to disk, and deleting it after an hour)

--
Kyle Maddison


http://www.*-*-*.com/



Mon, 20 Jun 2005 23:06:36 GMT  
 File download scripts

Quote:

> This works fine, except when I use the content-length header I can't quite
> get it right (and not setting means your browser doesn't tell you how long
> it will take to d/l etc). If I sum the file sizes using filesize($file) to
> get $file_size_est the tar looks OK but the last file in the tar will be
> corrupted (due to missing bytes off the end I think). If I guess and add
> bytes then I'll never get it right because this depends on the number of
> files in the directory (and setting $file_size_est too big breaks some
> browsers).

Errm. In a tar file, each file is preceded by a 512-byte
header followed by sufficient 512-byte blocks to make up
the files. So you should be able accurately to predict the
size of the generated tar file. (Modulo things like the
size of one of the files changing between when you find
out its size and when tar reads it.)

Your system may have a tar(5) or archives(4) man page which
describes the format in more detail.

--
Chris Lightfoot, chris at ex dash parrot dot com; http://ex-parrot.com/~chris/
``That Ariel Sharon. Boy, he sure is controversial.
   Most Western countries would have to have a military coup
   before they enjoyed a leader of that caliber.'' (Sydney Webb)



Mon, 20 Jun 2005 23:42:31 GMT  
 File download scripts

Quote:

> Hi,

> I'm using the following php code to tar images in a directory and sending
> headers so that the result is downloaded:

> <?
>     ....
>     header("Content-Type: application/x-tar");
>     header("Content-Length: $file_size_est");
>     header("Content-Disposition: attachment; filename=$filename\n");
>     passthru('tar -c *.jpg');
>     exit();
>     ....
> ?>

> This works fine, except when I use the content-length header I can't quite
> get it right (and not setting means your browser doesn't tell you how long
> it will take to d/l etc). If I sum the file sizes using filesize($file) to
> get $file_size_est the tar looks OK but the last file in the tar will be
> corrupted (due to missing bytes off the end I think). If I guess and add
> bytes then I'll never get it right because this depends on the number of
> files in the directory (and setting $file_size_est too big breaks some
> browsers).

> Any ideas? (short of writing it out to disk, and deleting it after an hour)

> --
> Kyle Maddison


> http://kyle.is-a-geek.com/

You could also make a temporary file then delete it after it gets sent.

$fileName='image_'.rand(1000, 9999).'.tar';
system('tar -cf '.$fileName.' *.jpg');
header("Content-Type: application/x-tar");
header("Content-Length: ".file_size($fileName));
header("Content-Disposition: attachment; filename=$fileName\n");
readFile($fileName);
unlink($fileName);
exit();



Mon, 20 Jun 2005 23:02:08 GMT  
 File download scripts

Quote:

> You could also make a temporary file then delete it after it gets sent.

> $fileName='image_'.rand(1000, 9999).'.tar';
> system('tar -cf '.$fileName.' *.jpg');

NB: this isn't reliable or secure, but is probably OK if
the script's current working directory is not accessible
to other users and the site isn't under high load. Better
to use mkstemp or its equivalent.

--
Chris Lightfoot, chris at ex dash parrot dot com; http://ex-parrot.com/~chris/
       Brian: You have to be different!
       Crowd: Yes, we are all different!
Quiet voice: I'm not!             (from `The Life of Brian')



Tue, 21 Jun 2005 06:21:49 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Script for downloading a file from a http server

2. File download tcl script - help needed !

3. Script for download selected datafiles...

4. Download script problem

5. CGI script to count downloads

6. Req: Download throttle script.

7. Mac automation scripting tools that can be downloaded?

8. Creating an awk script to extract other scripts from a file

9. Downloading a file from Webserver

10. Download a file, displaying progress bar

11. 3270 Emulation and File Up/Download

12. downloading files

 

 
Powered by phpBB® Forum Software