在我的函數中,我正在下載一個文件,將下載文件保存到日誌文件中。每當我嘗試下載我上傳的Excel文件時,Excel都會聲明它已損壞。我的本地副本工作正常。這是我的download.php:PHP readfile()破壞文件
<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
ob_start();
?>
<?php if (login_check($mysqli) == true) :?>
<?php
$logFile = "download.log";
$directory = "./downloads/";
date_default_timezone_set('America/New_York');
$filename = $_GET['file'];
$path = "$directory$filename";
if(file_exists($path) AND substr_count($filename,"/") == "0") {
if (isset($logFile)) {
$downloadLogRecord = $filename." || ".$_SESSION['name']." || ".$_SESSION['username']." || ".$_SERVER['REMOTE_ADDR']." || ".date('Y-m-d H:i:s')."\r\n";
@file_put_contents($logFile,$downloadLogRecord,FILE_APPEND|LOCK_EX);
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Length: ".filesize($path));
readfile("$path");
}
?>
<?php else : ?>
<p>
<span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>.
</p>
<?php endif; ?>
我該如何解決這個問題?
你ob_start會緩衝輸出;但除非您在某個時間點刷新緩衝區,否則在腳本終止導致損壞時''>''和'<?php'之間的空白仍會被注入到文件中 –