Ik ben bezig mijn HTML website te optimaliseren en met PHP enkele functies toe te voegen. Zo wil ik nu files die ik normaal manueel erop deed zetten via een upload script in de database laten opslaan en deze automatische laten plaatsen op een download pagina. Alle info hiervoor heb ik netjes op het internet gevonden en alles werkt tot het laatste stapje. Als ik heb gedownload komt het vaker voor dat mijn gedownload bestand corrupt is. Ik heb hier via Google op gezocht maar kan niet echt de oplossing vinden en ik zit hier nu al een paar dagen mee te klooien. Hebben jullie nog tips waar het fout in gaat?
Onderstaand mijn code die ik gebruik om de files te tonen (list_files.php) en te downloaden(get_file.php).
list_files.php
<meta charset="utf-8">
<head>
<title>Naamloos document</title>
</head>
<body>
<?php
// Connect to the database
$dbLink = new mysqli('parochie-molenberg.nl', 'www', 'Welkom1', 'Parochie');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Query for a list of all existing files
$sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`';
$result = $dbLink->query($sql);
// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
// Print the top of a table
echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>Mime</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b> </b></td>
</tr>';
// Print each file
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['mime']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
<td><a href='get_file.php?id={$row['id']}'>Download</a></td>
</tr>";
}
// Close table
echo '</table>';
}
// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
// Close the mysql connection
$dbLink->close();
?>
</body>
</html>
Get_file.php
<meta charset="utf-8">
<head>
<title>Naamloos document</title>
</head>
<body>
<?php
// Make sure an ID was passed
if(isset($_GET['id'])) {
// Get the ID
$id = intval($_GET['id']);
// Make sure the ID is in fact a valid ID
if($id <= 0) {
die('The ID is invalid!');
}
else {
// Connect to the database
$dbLink = new mysqli('parochie-molenberg.nl', 'www', 'Welkom1', 'Parochie');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Fetch the file information
$query = "
SELECT `mime`, `name`, `size`, `data`
FROM `file`
WHERE `id` = {$id}";
$result = $dbLink->query($query);
if($result) {
// Make sure the result is valid
if($result->num_rows == 1) {
// Get the row
$row = mysqli_fetch_assoc($result);
// Print headers
header("Content-Type: ". $row['mime']);
header("Content-Length: ". $row['size']);
header("Content-Disposition: attachment; filename=". $row['name']);
// Print data
echo $row['data'];
}
else {
echo 'Error! No image exists with that ID.';
}
// Free the mysqli resources
@mysqli_free_result($result);
}
else {
echo "Error! Query failed: <pre>{$dbLink->error}</pre>";
}
@mysqli_close($dbLink);
}
}
else {
echo 'Error! No ID was passed.';
}
?>
</body>