强制下载Zip文件结果已损坏

时间:2018-04-12 21:36:18

标签: php

我试图在一些检查(哈希,到期日期,下载号码)之后强制下载zip文件。 页面的链接包含用于验证下载的id(哈希)。 例如: www.example.com/download.php?id=505fbeabfd97c0ab15a016d68512d8df5acdedd6e6cc6

一切正常,但文件已损坏。 如果我尝试消除所有检查和与数据库的连接,则下载的zip是可以的。 这是代码:

<?php
$maxdownloads = "80";
// Set the key's viable duration in seconds (86400 seconds = 24 hours)
$maxtime = "2592000";

require ('connect.php');

if(get_magic_quotes_gpc()) {
    $id = stripslashes($_GET['id']);
}else{
    $id = $_GET['id'];
}
echo $id;
// Get the key, timestamp, and number of downloads from the database
$query = sprintf("SELECT * FROM digital_orders WHERE hash= '%s'",
mysqli_real_escape_string($conn,$id));
$result = $conn->query($query);

$row = $result->fetch_assoc();
if (!$row) { 
    echo "The download key you are using is invalid.";
}else{
    $timecheck = date('U') - $row['date'];
    echo $timecheck;

    if ($timecheck >= $maxtime) {
        echo "This key has expired (exceeded time allotted).<br />";
    }else{
        $downloads = $row['download'];
        echo $downloads;
        $downloads += 1;
        if ($downloads > $maxdownloads) {
            echo "This key has expired (exceeded allowed downloads<br/>";
        }else{
        $sql = sprintf("UPDATE digital_orders SET download .$downloads."' WHERE hash= '%s'",
 mysqli_real_escape_string($conn,$id));

            $incrementdownloads = $conn->query($sql);
            $result->close();

            // chiusura della connessione
            $conn->close();
// Debug        echo "Key validated.";

// Force the browser to start the download automatically

/*
Variables: 
    $file = real name of actual download file on the server
    $filename = new name of local download file - this is what the   visitor's file will actually be called when he/she saves it
*/

  // Get parameters

  $file = "dante.zip"; // Decode URL-encoded string
  $filename = "foto.zip";   
  $filepath = "digital_orders/Dante/" . $file;


  // Process download

 if(file_exists($filepath)) {
      ob_start();

  header('Content-Description: File Transfer'); 
  header('Content-Type: application/octet-stream'); 
  header('Content-Disposition: attachment; filename="'.$filename.'"');  
  header('Expires: 0'); 
  header('Cache-Control: must-revalidate'); 
  header('Pragma: public'); 
  header('Content-Length: ' . filesize($filepath)); 
  flush(); // Flush system output buffer
  ob_end_clean();
  readfile($filepath);

  exit; 
  }
        }
    }
}
?>

如果我尝试打开与记事本一起使用的zip文件,则有:

PK
     ûe‹L                  dante.txtPK 
     ûe‹L                $               dante.txt
         CdÔ‹‚ÑÓCdÔ‹‚ÑÓÆ‹‚ÑÓPK      [  

 '     

如果我尝试打开损坏的zip文件,则有:

 505fbeabfd97c0ab15a016d68512d8df5acdedd6e6cc61226822PK
     ûe‹L                  dante.txtPK 
     ûe‹L                $               dante.txt
         CdÔ‹‚ÑÓCdÔ‹‚ÑÓÆ‹‚ÑÓPK      [ 

有什么建议吗? &#39;

0 个答案:

没有答案