沒有辦法想象你的就是你發佈的代碼纔去上什麼,但是,它會建議,如果你正在做一個下載,你要小心,不要輸出任何文本(甚至白色空間),直到部分代碼完全運行。在<?php
之前甚至有一個空格會引發錯誤。這裏是一個函數,你可能想嘗試一下(如果你願意):
<?php
/**
* FULL-NAME OF THE FILE TO DOWNLOAD
* @param $downloadFileName
* NEW-NAME FOR THE FILE... DO NOT USE EXTENSIONS LIKE .pdf OR .doc OR .txt
* @param null $newFileName
* @return bool
*
*/
function processDownload($downloadFileName, $newFileName=null) {
$ext = pathinfo($downloadFileName, PATHINFO_EXTENSION);
if(!$newFileName){
$newFileName = basename($downloadFileName);
}else{
$newFileName .= "." . $ext;
}
if(file_exists($downloadFileName)){
$size = @filesize($downloadFileName);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $newFileName);
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);
readfile($downloadFileName);
return TRUE;
}
return FALSE;
}
如果您選擇嘗試這個功能,建議,使之成爲第一個函數調用在腳本這樣的頂部:
<?php
// NOTICE THAT THERE IS NO WHITE-SPACE BEFORE PHP.
// AND NO OUTPUT WAS SENT (IE: NOTHING ECHOED OUT...)
processDownload(__DIR__ . "/downloads/unique_ips.txt", "Unique-IPs");
// FROM HERE ON, YOU MAY ECHO WHATEVER YOU WISH...
// echo $whatever;
// echo $youWish;
// echo $may;
// echo $nowBeEchoed;
// echo $afterCalling;
// echo $theFunctionAbove;
echo 'IP addresses only found in file A:' . "\n";
echo $array_a_result . "\n\n";
echo 'IP addresses only found in file B:' . "\n";
echo $array_b_result;
function processDownload($downloadFileName, $newFileName=null) {
$ext = pathinfo($downloadFileName, PATHINFO_EXTENSION);
if(!$newFileName){
$newFileName = basename($downloadFileName);
}else{
$newFileName .= "." . $ext;
}
if(file_exists($downloadFileName)){
$size = @filesize($downloadFileName);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $newFileName);
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);
readfile($downloadFileName);
return TRUE;
}
return FALSE;
}
這裏運行的是哪裏?如果它沒有在其他輸出之前發送,那麼它將不起作用 - 報頭輸出必須是發送的第一個輸出。 –
你有沒有嘗試過爲你的Content-type使用「application/octet-stream」? – Terry