I downloaded a zip file from a database but am unable to find what I should put in the $file_names = array()
? When I run the file the error displayed is No ID Selected
I think that this is because there is nothing inside the array()
. Can someone help me define the array()
?
<?php
$file_names = array();
// Make sure an id was passed
if (isset($_GET['id'])) {
// Get the id into string (in case of an array)
$id_pass = implode(",",$_GET['id']);
// Make sure the name is in fact a valid
if ($id_pass <= 0) {
die ('No ID Selected!');
} else {
// codes to connect to the database is here
// Fetch the file information
$query = "select * from docu where id = {$id_pass};";
var_dump($result);
$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);
//zip function
$zip = new ZipArchive();
$filename = "export_" . date('Y.m.d H.i.s') . ".zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true) {
echo "Cannot Open for writing";
}
$ext = $row['name'] . ".pdf"; // taking file name from DB and adding extension separately
$zip->addFromString($ext, $row['content']); //adding blob data from DB
$zip->close();
header("Content-disposition: inline; filename='.$filename'");
header('Content-type: application/zip');
readfile($filename);
unlink($filename);
}
}
// Free the mysqli resources
mysqli_free_result($result);
mysqli_close($dbLink);
}
}
result of var_dump($result)
:
object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(2) ["lengths"]=> NULL ["num_rows"]=> int(22) ["type"]=> int(0) }
Here is the HTML Form.
<html>
<form action="test.php" method="get"/>
<?php
//codes to connect to the database is here
mysqli_select_db($con, "images");
$query = "SELECT id, name FROM docu order by id";
$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) = mysqli_fetch_array($result, MYSQLI_BOTH))) {
echo "<input type='checkbox' name='id[]'>";
echo $name . " " . $id . " " . "<br>"; //id and name removed
}
}
?>
<input type="submit" name="submit" value="Submit" />
</html>
Do this. In your while loop:
echo "<input type='checkbox' name='$id' value='$id'>";
and it should work.
Explanation: When you post a form on your server, the checkbox's value is taken by the value
contained in it. Since you were not putting anything in your value, you were not able to get the id
on your server.