file_download_php
https://www.w3docs.com/snippets/php/automatic-download-file.html
https://www.youtube.com/watch?v=fLyIQB-RY10
<?php
include "sql_connect.php";
$sql_query = "select * from image_upload ";
//run...
$result = mysqli_query($con, $sql_query) or die("not executing");
$r_count = mysqli_num_rows($result);
//echo $r_count;
if ($r_count > 0) {
$show_data = '<table border="1px" width="50%">
<tr><th>sno</th><th>Name</th><th>action</th></tr>';
while ($data = mysqli_fetch_assoc($result)) {
//echo var_dump($data);
$show_data .= "<tr>
<td> {$data['sno']}</td>
<td>{$data['image_path']}</td>
<td>
<a href='download.php?q=$data[image_path]'>download</a>
</td>
<tr>";
}
// data-* attribute give us the ability to embeded
// custom data attributes on all HTML elements
$show_data .= "</table>";
echo $show_data;
} //else {
// echo "data not found";
// }
mysqli_close($con); ?>
now download code:---
<?php
if(isset($_REQUEST["q"])){
// Get parameters
$filepath = urldecode($_REQUEST["q"]); // Decode URL-encoded string
//echo $filepath;
/* Check if the file name includes illegal characters
like "../" using the regular expression */
// if(preg_match('/^[^.][-a-z0-9_.]+[a-z]$/i', $file)){
// $filepath = "images/" . $file;
// Process download
if(file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
flush(); // Flush system output buffer
readfile($filepath);
die();
} else {
http_response_code(404);
die();
}
} else {
die("Invalid file name!");
}
//}
?>
Comments
Post a Comment