gpt4 book ai didi

php - 单击下载链接时文件损坏

转载 作者:太空宇宙 更新时间:2023-11-04 14:07:12 25 4
gpt4 key购买 nike

我尝试使用 php 强制下载图像 jpg 文件,我已经实现了以下代码:

html

<a href = "filedownload.php?src=uploads/myimage.jpg&download=true>download this file</a>

下载.php

 <?php
ob_start();
include_once 'functions.php';

if (isset($_GET['download']) && $_GET['download'] == 'true')
{

$src = sanitizeString($_GET['src']);
header('Content-Description: File Transfer');
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename='.basename($src));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');

}

?>

假设图像的完整路径是“www.example.com/smith/topic/uploads/myimage.jpg”,我收到了正确的图像名称并且也出现了下载窗口,但是图像已损坏大小为 1KB,任何人都可以告诉我原因,非常感谢。

最佳答案

这里是如何使用readfile函数的例子

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>

关于php - 单击下载链接时文件损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7621565/

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