我正在尝试使用PHP通过单击按钮强制下载。但是,每次从页面重新加载中单击下载按钮时都不会发生任何事情。
if(isset($_GET['download'])){
try {
$sql = 'SELECT file_name, file_type FROM files WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_GET['file_id']);
$s->execute();
} catch (PDOException $e) {
$error = 'download error: ' . $e->getMessage();
include 'error.php';
exit();
}
$file = $s->fetch();
if(!$file){
$error = 'File not found!: ' . $e->getMessage();
include 'error.php';
exit();
}
$filename = $file['file_name'];
$mimetype = $file['file_type'];
$filedata = "users/$firstname/".$_POST['file_name'];
$disposition = 'inline';
header('Content-length: ' . strlen($filedata));
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: $disposition; filename=$filedata");
echo readfile($filedata);
exit();
}
答案 0 :(得分:0)
我认为强制下载的最佳方式是:
header('Location: *Your path to download file*');
但这就是我的想法,这个答案对你来说可能是无益的......