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):
我有一個php頁面(file.php),它做了這樣的事情(許多其他代碼被剝離):
// 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:
好像它應該工作,它幾乎可以,但我有以下問題:
When its a text file, I am getting a strange symbol as the first letter in the text document
當它是一個文本文件時,我得到一個奇怪的符號作為文本文檔中的第一個字母
When its a word doc, it is corrupt (presumably that same first bit or byte throwing things off).
當它是一個單詞doc時,它是腐敗的(可能是相同的第一位或字節丟棄了)。
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.
發生的事情是我有一系列include_once文件,其中一個有一個空行導致問題。
Not sure if this is the real answer, but I think you intended
不確定這是否真的答案,但我認為你的意圖
header('Content-Type: ' . $mimeType);
to be
header('Content-Type: ' . $mimeTypes[$mimeType]);
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/05/22/2fa979157dacd9f338c3cd9b3f339051.html。