PHP使用标题输出文件附件 [英] PHP Outputting File Attachments with Headers

查看:116
本文介绍了PHP使用标题输出文件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了几篇文章之后,我制定了这个函数,它是一堆其他函数的混合体:

 函数outputFile($ filePath,$ fileName,$ mimeType =''){
//安装程序
$ mimeTypes = array($ b $'pdf'=>'application / pdf',
'txt'=>'text / plain',
'html'=>'text / html',
'exe'=>'application / octet-stream',
'zip'=>'application / zip',
'doc'=>'application / msword',
'xls'=>'application / vnd.ms-excel',
'ppt'=>'application / vnd.ms-powerpoint',
'gif'=>'image / gif',
'png'=>'image / png' ,
'jpeg'=>'image / jpg',
'jpg'=>'image / jpg',
'php'=>'text / plain'
);

//发送头文件
// - 根据建议固定下一行 -
头文件('Content-Type:'。$ mimeTypes [$ mimeType]);
header('Content-Disposition:attachment; filename =''。$ fileName。'');
header('Content-Transfer-Encoding:binary');
头('Accept-Ranges:bytes');
header('Cache-Control:private');
header('Pragma:private');
header('Expires:Mon,1997年7月26日05:00:00 GMT');
readfile($ filePath);


$ / code $ / pre

我有一个php页面(file.php)这样的东西(大量的其他代码剥离出来):

  //我运行这个通过一个安全的函数,这里没有显示
$ safe_filename = $ _GET ['filename'];
outputFile(/ the / file / path / {$ safe_filename},
$ safe_filename,
substr($ safe_filename,-3));

似乎应该可以工作,它几乎可以,但我遇到以下问题: p>


  1. 当它是一个文本文件时,我在文本文档中得到一个奇怪的符号作为第一个字母


  2. 当它是一个单词doc时,它已经被破坏了(大概相同的第一位或者字节将会抛出异常)


  3. 我认为所有其他文件类型将被破坏 - 甚至没有尝试过它们。


关于我在做什么的任何想法错误?



感谢 -


$ b

更新:仍然是同样的问题。

解决方案

好吧,我想通了。



上面的脚本确实有效。



发生了什么是我有一系列include_once文件,其中一个文件有空行导致问题。


After reading a few posts here I formulated this function which is sort of a mishmash of a bunch of others:

function outputFile( $filePath, $fileName, $mimeType = '' ) {
    // Setup
    $mimeTypes = array(
        'pdf' => 'application/pdf',
        'txt' => 'text/plain',
        'html' => 'text/html',
        'exe' => 'application/octet-stream',
        'zip' => 'application/zip',
        'doc' => 'application/msword',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',
        'gif' => 'image/gif',
        'png' => 'image/png',
        'jpeg' => 'image/jpg',
        'jpg' => 'image/jpg',
        'php' => 'text/plain'
    );

    // Send Headers
    //-- next line fixed as per suggestion --
    header('Content-Type: ' . $mimeTypes[$mimeType]); 
    header('Content-Disposition: attachment; filename="' . $fileName . '"');
    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');
    readfile($filePath);

}

I have a php page (file.php) which does something like this (lots of other code stripped out):

   // I run this thru a safe function not shown here
$safe_filename = $_GET['filename']; 
outputFile ( "/the/file/path/{$safe_filename}", 
             $safe_filename, 
             substr($safe_filename, -3) );

Seems like it should work, and it almost does, but I am having the following issues:

  1. When its a text file, I am getting a strange symbol as the first letter in the text document

  2. When its a word doc, it is corrupt (presumably that same first bit or byte throwing things off).

  3. I presume all other file types will be corrupt - have not even tried them

Any ideas on what I am doing wrong?

Thanks -

UPDATE: changed line of code as suggested - still same issue.

解决方案

Ok, I figured it out.

The script above DOES work.

What was happening is I had a series of include_once files, and one of them had a blank line causing the issue.

这篇关于PHP使用标题输出文件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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