易PHP损坏的文件



这是我点击下载按钮的页面。

这是我在网上找到的东西,但它不起作用

为什么我下载的文件已损坏或损坏?

强制下载PDF文件,文件损坏

$query = "SELECT id, name FROM docu";
$result = mysqli_query($con,$query) or die('Error, query failed');
if(mysqli_num_rows($result)==0){
    echo "Database is empty <br>";
}
else{
    while ( (list($id, $name) = mysqli_fetch_array($result, MYSQLI_BOTH))){
?>
<p><a href="download.php?id=<?= $id ?>">Name : <?= $name ?></a></p>

这是下载代码。当我下载文件时,它说它已损坏。

if(isset($_GET['id'])){
 $id    = $_GET['id'];
 $con = mysqli_connect("localhost:3306", "waduser", "waduser", "fyp");
if(!$con) {
  die("cannot connect: " . mysqli_error());
 }
 mysqli_select_db($con,"fyp");
 $query = "SELECT * FROM docu where id ='" .$id ."'";
 $result = mysqli_query($con,$query) or die('Error, query failed');
 if($row =  $result ->  fetch_array(MYSQLI_ASSOC))
 {
    $name = $row['name'];
    $type = $row['type'];
    $content = $row['content']; //content of file
    $size = $row['size']; //file size
    header('Content-Type:"' . $type . '"');
    header('Content-length:' . $size  .'');
    header('Content-Disposition: attachment; filename="' .$name. '"');
  } 
 }
?>

我认为 Jim 想说的是你已经发送了所有的标头,但你实际上并没有发送文件本身。

所以试试

$name = $row['name'];
$type = $row['type'];
$content = $row['content']; //content of file
$size = $row['size']; //file size
header('Content-Type:"' . $type . '"');
header('Content-length:' . $size  .'');
header('Content-Disposition: attachment; filename="' .$name. '"');
echo $content;

此外,我认为至少在 href 属性中删除short_tag使用周围的空格更安全。

<p><a href="download.php?id=<?=$id?>">Name : <?= $name ?></a></p>

最新更新