0

I've a form on submit I call images from DB against id

Then I follow php.net guide to download image. file downloaded successfully.

Now after downloading image or file I want to refresh/redirect to the same page to get downloads counts.

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;
}

I tried using ob_start() in the beginning of page by doing so I did not able o download file.

Please Guide me accordingly.

Zargham irtza
  • 75
  • 1
  • 14
  • You can't have both, you either need them to download or you need them to redirect, but the browser can't do both at the same time. Downloads of octet-streams are normally handled without loading a new page in most modern browsers anyways, so why is it necessary? – Devon Bessemer Oct 26 '17 at 02:24
  • you can see, i'm inserting record into Database after downloading a file, the purpose of redirect is to get latest download count. http://ucs-crm.com/theme-wide/product-detail.php?pcode=Nw== this my site – Zargham irtza Oct 26 '17 at 02:34
  • What kind of error are you getting? Where does it redirect you? Try to specify absolute path to your file instead of relative path you have `header('Location: /path/to/product-detail.php?pcode=' . $decrypt_id);` `/path/to/` you absolute path to `product-detail.php`, it might be just `/` if the file is in the root folder. – zstate Oct 26 '17 at 02:43
  • I'm not getting any error, after clcik on button given in produt-detail page http://ucs-crm.com/theme-wide/product-detail.php?pcode=Nw== file get downloaded values inserted to database successfully but not redirecting to same URL.& where should I've to give absolute path ? absolute path for image or header('Location:abosolutepath'); ? – Zargham irtza Oct 26 '17 at 02:50
  • It won't work because you can't set a header after you've sent any of the body. This is what I was getting at with it not being possible from the server side to send a file and commit a redirect. – Devon Bessemer Oct 26 '17 at 11:29

1 Answers1

1

You mean like this?

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

Don't forget to exit() after your header().

  • Hmm. Let me try to reproduce your problem. – Pia Wurtzbach Oct 26 '17 at 02:31
  • Did you try this one: `header("Refresh:0");`? I found it on one of the topics here: https://stackoverflow.com/questions/12383371/refresh-a-page-using-php – Pia Wurtzbach Oct 26 '17 at 02:33
  • yes i tried this too, unfortunately its also not working. redirect only work when i use ob_start() in start of page but by using ob_start() i can download file. http://ucs-crm.com/theme-wide/product-detail.php?pcode=Nw== this – Zargham irtza Oct 26 '17 at 02:36