如何在PHP下载文件后重定向到同一页面?

时间:2017-10-26 02:10:13

标签: php mysql

我提交了一个表单,我将数据库中的图像称为id

然后我按照php.net指南下载图片。文件下载成功。

现在下载图片或文件后,我想刷新/重定向到同一页面以获得下载次数。

if (isset($_POST['download']) && $_POST['download'] =='click')
{
$sqlone = "Select image from products where products.id='$product_id'";
$run_sqlone = mysqli_query($con,$sqlone);
$fech_file = mysqli_fetch_array($run_sqlone);
$filenmae = $fech_file['image'];

  $path = 'images/products/'.$fech_file['image'];

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($path));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($path);

 $insertquery = "INSERT INTO 
                      downloads
                           (product_id,
                            download_by) 
                        values 
                            (".$product_id.",
                            '".$user."')";

  $run_insert = mysqli_query($con, $insertquery);

  header('Location:product-detail.php?pcode='.$decrypt_id);
  exit;
}

我尝试在页面开头使用ob_start(),因为我无法下载文件。

请相应地指导我。

1 个答案:

答案 0 :(得分:1)

你的意思是这样吗?

header('Location:' . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']);
exit();

请勿忘记exit()后的header()