如何强制下载不同类型的扩展文件php [英] How to force download different type of extension file php

查看:68
本文介绍了如何强制下载不同类型的扩展文件php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这里是我尝试过的

  $ filename = $ file; //这可以是任何格式可能是zip或者可能mp3 

$ path =/ home / tw / public_html / file /; //这个文件的路径
$ fullPath = $ path。$ filename; //路径下载文件

$ filetypes = $ extension; //它是当前文件的扩展名

if($ fd = fopen($ fullPath,r)){
if(!in_array(substr($ filename,-3) ,$ filetypes)){
echo无效的下载状态。
退出;
}
$ 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); //使用这个直接打开文件
while(!feof($ fd)){
$ buffer = fread($ fd,2048);
echo $ buffer;
}
}
fclose($ fd);
退出;

扩展可以是任何东西

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


解决方案

要下载任何文件有力的延伸您可以先读取该文件并将该文件写入输出缓冲区以供下载。



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



此代码基于: http://php.net/manual/en/function.readfile.php

 <?php 
//您可以使用任何文件扩展名输出要下载的文件
$ 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);
退出;
}
?>


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'

解决方案

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;
}
?>

这篇关于如何强制下载不同类型的扩展文件php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆