gpt4 book ai didi

PHP readfile() 损坏文件

转载 作者:可可西里 更新时间:2023-11-01 13:33:53 26 4
gpt4 key购买 nike

在我的函数中,我正在下载一个将下载保存到日志文件的文件。每当我尝试下载我上传的 Excel 文件时,Excel 都会指出它已损坏。我的本地副本工作正常。这是我的 download.php:

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

我该如何解决这个问题?

最佳答案

想通了。我只是在 readfile(); 之前添加了 ob_get_clean(); 并在其之后添加了 ob_end_flush();

关于PHP readfile() 损坏文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22211862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com