<?php
if(!isset($_GET['id'])){
?>
<html>
<head>
<title>Download File From mysqli Database</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '','uploads') or die(mysqli_error());
$query = "SELECT id, name, size FROM upload";
$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,$size) = mysqli_fetch_array($result)) {
?>
<?php echo urlencode($name); ?>
<a href="download.php?id=<?php echo urlencode($id); ?>"
>Download</a> <?php echo urlencode($size); ?> <br>
<?php
}
}
mysqli_close($con);
}else{
?>
</body>
</html>
<?php
if (isset($_GET['id'])) {
$con = mysqli_connect('localhost', 'root', '','uploads') or die(mysqli_error());
$id = $_GET['id'];
$query = "SELECT name, type, size, content " .
"FROM upload WHERE id = '$id'";
$result = mysqli_query($con,$query) or die('Error, query failed');
list($name, $type, $size, $content) = mysqli_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
ob_clean();
flush();
echo $content;
mysqli_close($con);
exit;
}
}
?>