2

I have script where user can upload any kind of file & I have stored their extension type also in database but how can I force the file download with respective extension

here is what I've tried

$filename = $file; // this can be in any format may be zip or maybe mp3

$path = "/home/tw/public_html/file/"; //path of this file
$fullPath = $path.$filename; //path to download file

$filetypes = $extension; // it is the extension of current file

if ($fd = fopen ($fullPath, "r")) {
if (!in_array(substr($filename, -3), $filetypes)) {
    echo "Invalid download State.";
    exit;
}
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);

header("Content-type: application/zip");
header("Content-Disposition: filename=".$path_parts["basename"]."");
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
 }
fclose ($fd);
exit;

extension can be anything

 'application/x-bittorrent',
 'application/x-bzip',
 'application/x-bzip2',
 'application/x-compressed',
 'application/x-excel',
 'application/x-gzip',
 'audio/*',
 'image/*',
 'multipart/x-gzip',
 'multipart/x-zip',
 'text/plain',
 'text/rtf',
 'text/richtext',
 'text/xml',
 'video/*',
 'text/csv',
 'jpg' => 'image/jpeg',
 'jpeg' => 'image/jpeg',
 'jpe' => 'image/jpeg',
 'gif' => 'image/gif',
 'png' => 'image/png',
 'bmp' => 'image/bmp',
 'flv' => 'video/x-flv',
 'js' => 'application/x-javascript'
roulibic
  • 325
  • 3
  • 9

3 Answers3

3

You can get the correct MIME Type using mime_content_type().

So get the content type in your code like this,

$contentType=mime_content_type($fullPath);

And append it to the PHP header.

header("Content-type: ".$contentType."");

$fullPath should contain the path of the file including file name.

mime_content_type() returns the MIME content type for a file as determined by using information from the magic.mime file.

Alok Patel
  • 7,842
  • 5
  • 31
  • 47
3

To download any file extension forcefully. You can first read the file and writes that file to the output buffer for download.

Here is the code, to download files. Hopes, it will also help to fix your problem.

This code is based on: http://php.net/manual/en/function.readfile.php

<?php
// You can used any file extension to output the file for download
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}
?>
Suresh Sapkota
  • 149
  • 3
  • 7
2

Forcing a download in PHP

You will need to set a certain range of headers to get your download going.

// You will need to set the content type
// Thanks to Alok's answer
$contentType=mime_content_type($fullPath);

header('Content-Type: '.$contentType);

// You can change the extension in the filename if you'd like
$filename = sprintf('"%s"', addcslashes(basename($filename), '"\\'));
$size   = filesize($filename);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
Peter
  • 8,776
  • 6
  • 62
  • 95