尝试使用php强制下载文件时出错

尝试使用php强制下载文件时出错,php,http-headers,Php,Http Headers,我尝试使用简单的php脚本强制从服务器下载文件: $file = '13.zip'; header("Content-disposition: attachment; filename=".$file); header("Content-type: application/zip"); readfile($file); 该文件将下载,但如果我尝试在本地计算机上打开它,我会收到错误消息“压缩(压缩)文件夹C:/…/13.zip无效”,并且zip只有1KB。出什么问题了?请执行以下操作: <

我尝试使用简单的php脚本强制从服务器下载文件:

$file = '13.zip';
header("Content-disposition: attachment; filename=".$file);
header("Content-type: application/zip");
readfile($file);

该文件将下载,但如果我尝试在本地计算机上打开它,我会收到错误消息“压缩(压缩)文件夹C:/…/13.zip无效”,并且zip只有1KB。出什么问题了?

请执行以下操作:

<?php

    $file = "13.zip";

    $file_name = basename($file);

    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=$file_name");
    header("Content-Length: " . filesize($file)); //added this line

    readfile($file);
?>


为什么我们要发布评论而不是答案?请删除您的
错误日志
并重试,它们可能是旧错误@stckvrw