如果文件成功下载,如何从用户删除文件?我这里有一个下载功能,但我不知道如何进行一次下载。
使用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
}
?>
file.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,PICTURE
答案 0 :(得分:0)
只需在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'");
答案 1 :(得分:0)
新的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();
?>
File.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()连接部分并使用单独的文件。