views:

61

answers:

4

Hi, i have a tar archive on the server that must be downloadable through php. This is the code that i've used:

$content=file_get_contents($tar);
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=$tar");
header("Content-Length: ".strlen($content));    
unlink($name);
die($content);

The file is downloaded but it's broken and it can't be open. I think that there's something wrong with headers because the file on the server can be open without problems. Do you know how can i solve this problem?

UPDATE I've tried to print an iframe like this:

<iframe src="<?php echo $tar?>"></iframe>

And the download works, so i'm sure that there's something missing in headers.

+1  A: 

Try something like the following:

$s_filePath = 'somefile.ext';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'. s_filePath.'"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Cache-control: private');
header('Pragma: private');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header("Content-Length: ".filesize($s_filePath));

$r_fh = fopen($s_filePath,'r');
while(feof($r_fh) === false) {
    $s_part = fread($r_fh,10240);
    echo $s_part;
}
fclose($r_fh);
exit();
Prot0
Same result. I think that there something wrong with the encoding header.
mck89
+1  A: 

I have used this code when I have had to do it:

function _Download($f_location, $f_name){
 header('Content-Description: File Transfer');
 header('Content-Type: application/octet-stream');
 header('Content-Length: ' . filesize($f_location));
 header('Content-Disposition: attachment; filename=' . basename($f_name));
 readfile($f_location);
 }

_Download("../directory/to/tar/raj.tar", "raj.tar");
//or
_Download("/var/www/vhost/domain.com/httpdocs/directory/to/tar/raj.tar", "raj.tar");

Try that.

PHPology
I've tried that but the result is the same..I think that there's something missing with the encoding header.
mck89
do other files download ok i.e. images for example.I am testing locally for you now
PHPology
Ok it seems to be working for me with what ever file I throw at it :-|How are you calling this file? Directly like http://localhost/download.php for example
PHPology
+1  A: 

Use Content-Type: application/octet-stream or Content-Type: application/x-gtar

stillstanding
A: 

Don't use file_get_contents() and then echo or print to output the file. That loads the full contents of the file into memory. A large file can/will exceed your script's memory_limit and kill the script.

For dumping a file's contents to the client, it's best to use readfile() - it will properly slurp up file chunks and spit them out at the client without exceeding available memory. Just remember to turn off output buffering before you do so, otherwise you're essentially just doing file_get_contents() again

So, you end up with this:

$tar = 'somefile.tar';
$tar_path = '/the/full/path/to/where/the/file/is' . $tar;
$size = filesize($tar_path);

header("Content-Type: application/x-tar");
header("Content-Disposition: attachment; filename=\"$tar\");
header("Content-Length: $size");    
header("Content-Transfer-Encoding: binary");

readfile($tar_path);

If your tar file is actually gzipped, then use "application/x-gtar" instead.

If the file still comes out corrupted after download, do some checking on the client side:

  1. Is the downloaded file 0 bytes, but the download process seemed to take much longer than it would take for 0 bytes to transfer, then it's something client-side preventing the download. Virus scanner? Trojan?
  2. Is the downloaded file partially present, but smaller than the original? Something killed the transfer prematurely. Overeager firewall? Download manager having a bad day? Output buffering active on the server and the last buffer bucket not being flushed properly?
  3. Is the downloaded file the same size as the original? Do an md5/sha1/crc checksum on both copies. If those are the same, then something's wrong with the app opening the file, not the file itself
  4. Is the downloaded file bigger than the original? Open the file in notepad (or something better like notepad++ which doesn't take years to open big fils) and see if any PHP warnings messages, or some invisible whitespace you can't see in your script got inserted into the download at the start or end of the file.
Marc B