【问题标题】:How to create a file download that will be remove from the database once downloaded如何创建下载后将从数据库中删除的文件下载
【发布时间】:2014-09-24 01:38:58
【问题描述】:

如果文件已成功下载,我如何从用户那里删除该文件?我这里有下载功能,但我不知道如何进行一次性下载。

带有 PHP 的 HTML 代码:

<?php 
                    $result=mysql_query("SELECT * FROM school_management");
                    while($row=mysql_fetch_array($result))
                    {
                ?>
                    <div class="col-xs-6 col-md-3" style="padding-bottom: 10px;">
                        <div class="thumbnail">
                          <img src="images/ebooks/<?php echo $row['PICTURE'];?>" alt="...">
                        </div>
                        <p><center><a href="files/file.php?file=<?php echo $row['FILE_NAME'];?>" class="btn btn-xs btn-inverse" role="button" style="background-color: #2980b9;">Download</a></center></p>
                    </div>
                <?php
                    }
                ?>

文件.php

<?php

include('config.php');

$file=$_GET['file'];
echo $file;
header("Content-disposition: attachment; filename=$file");
header("Content-type: application/pdf");
header('Content-Description: File Transfer');
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile("$file");

?>

我的file.php只能下载标题中带有字母的文件,但如果文件标题中有符号或数字,我就无法下载该文件。因此,我更改了所有 pdf 标题以便能够下载它。任何使我的 file.php 正确下载的建议,如果用户成功下载文件,该文件将从用户的数据库中删除。

数据库名称:caledte1_ebook

表名称:school_management

表格列:ID_NUMBER、EBOOK_FILENAME、图片

【问题讨论】:

  • 为你做点什么。但是出于原则(以及您的安全!!!)将其完全更改为 mysqli() 我拒绝使用已弃用的代码。
  • @icecub 好的,我将其更改为 mysqli()。如何知道文件是否下载成功?当我获得该功能时,我可以处理该删除查询。
  • 你不能。有一些技巧可以尝试检测失败的下载。见:stackoverflow.com/questions/8771226/… 但最后还是不靠谱。

标签: php mysql pdf download


【解决方案1】:

只需在 file.php 中调用数据库即可。您可能还想在交付之前验证文件是否存在。

$filename = mysql_real_escape_string($_GET['file']);
$result = mysql_query("SELECT id_number FROM school_management WHERE ebook_filename = '$filename'");
if(mysql_num_rows($result) == 0) {
    // Perform error handling
}
mysql_query("DELETE FROM school_management WHERE ebook_filename = '$filename'");

【讨论】:

  • 是的,该文件存在于数据库中。下载后如何删除数据库中的文件名?我将把删除查询放在哪里?我想知道下载过程是否停止或有时PC突然与互联网断开连接,文件必须删除还是必须保留并等待下载完成后删除?
  • 我会检索文件,将其从数据库中删除,然后将其传送到浏览器。这确实有缺点,如果下载中断,文件会丢失,但我不知道有什么方法可以在运行另一个查询之前自动检测成功下载。
【解决方案2】:

新的 PHP/HTML 文件:

<?php

$dbhost = ""; //Enter db host here
$dbuser = ""; //Enter db user here
$dbpass = ""; //Enter db pass here
$dbname = ""; //Enter db name here

$db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

/* check connection */
if ($db->connect_errno) {
    printf("Connect failed: %s\n", $db->connect_error);
    exit();
}

$sql = "SELECT * FROM school_management";

if ($result = $db->query($sql)){
    while ($row = $result->fetch_assoc()){ ?>
        <div class="col-xs-6 col-md-3" style="padding-bottom: 10px;">
            <div class="thumbnail">
                <img src="images/ebooks/<?php echo $row['PICTURE'];?>" alt="...">
            </div>
            <p>
                <center>
                    <a href="files/file.php?file=<?php echo $row['FILE_NAME']; ?>" class="btn btn-xs btn-inverse" role="button" style="background-color: #2980b9;">Download</a>
                </center>
            </p>
        </div>
    <? }

    $result->free();

} else {
    printf("Error: %s\n", $db->error);
}

$db->close();

?>

文件.php:

<?php

$dbhost = ""; //Enter db host here
$dbuser = ""; //Enter db user here
$dbpass = ""; //Enter db pass here
$dbname = ""; //Enter db name here

$db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

/* check connection */
if ($db->connect_errno) {
    printf("Connect failed: %s\n", $db->connect_error);
    exit();
}

$file = $_GET['file'];

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);

    $file = $db->real_escape_string($file);

    $sql = "DELETE FROM school_management WHERE ebook_filename = '". $file ."'";

    if ($succes = $db->query($sql)){
        //Succesfull deleted code handle here
    } else {
        printf("Error: %s\n", $db->error);
    }

    $db->close();
}

?>

当然,您可以从中取出 mysqli() 连接部分,并为此使用单独的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    相关资源
    最近更新 更多