Jump to content

[SOLVED] Trying to download a rar file via PHP


bsprogs

Recommended Posts

As the title says, I'm trying to download a rar file when a button is clicked. So far Images, Videos, and Zip files download successfully.

 

Here are the mime types I've tried using

 

application/force-download

application/x-download

application/octet-stream

application/x-rar-compressed

application/x-rar

application/x-compressed

application/rar

 

None have worked. The mime type that I got from PHP for the file when I uploaded it was "application/x-rar-compressed" but that didn't do a thing :-/

I'll click the download button, it'll ask me to save the file and the download is done instantly. 0kb in size.

 

Please help

<?php

$filename = $_GET['file'];

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');

// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));

if( $filename == "" ) 
{
  echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";
  exit;
} elseif ( ! file_exists( $filename ) ) 
{
  echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>";
  exit;
};
switch( $file_extension )
{
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();

?>
    

Ok, I solved my own issue.

I had to change

ob_start("ob_gzhandler");

to

ob_start();

in order to get it to work.

 

But it works. Here is a reference to anyone else that ever has this problem!

 

application/x-rar-compressed worked

 

(Source: http://www.webmasterworld.com/php/3021609.htm)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.