如何强制下载不同类型的扩展文件php

时间:2016-07-04 09:03:01

标签: php download

我有脚本,用户可以上传任何类型的文件&我已将其扩展类型也存储在数据库中,但如何强制下载相应扩展名的文件

这是我尝试过的事情

$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;

扩展名可以是任何

 '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'

3 个答案:

答案 0 :(得分:2)

您可以使用mime_content_type()获取正确的MIME类型。

因此,请在代码中获取内容类型,

$contentType=mime_content_type($fullPath);

并将其附加到 PHP 标题。

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

$ fullPath 应包含文件的路径,包括   文件名。

   mime_content_type()会返回使用magic.mime文件中的信息确定的文件的MIME内容类型。

答案 1 :(得分:2)

强制下载PHP

您需要设置一定范围的headers才能下载。

// 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);

答案 2 :(得分:2)

强行下载任何文件扩展名。您可以先读取文件并将该文件写入输出缓冲区以供下载。

以下是下载文件的代码。希望,它也有助于解决您的问题。

此代码基于: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;
}
?>