I am getting reports by some users that they can’t download files.
This is the script inside the download counter file.
<?php
$id = $_GET['id'];
require('conn.php');
$stmt = write_conn()->prepare("UPDATE links SET dl_count = dl_count + 1 WHERE ID=" . $id);
$stmt->execute();
header('Location: ' . $row['mp3_url'] );
?>
I heard using Header() function for redirecting user to a download link has some limits and it doesn’t work well on IE. is that right ?
What’s the right way to do this ?
After researching and trying a lot of test & trials, I found some facts about the
header()function andreadfile()or similar ones.First of all you should know that
header()has compatibility issues with different browsers. So don’t try to look for a function that is flawless. The issue isn’t the php function, it’s the browsers that has different way of analyzing a download header so they all behave differently. And you may know already, the biggest troublemaker is IEIf you want to fix the problem and make it work using
header(), You’ll have to add/force some header details (Content-Type,etc) , But this is not possible in your php code. No matter how many times you try, it just doesn’t work that work. I don’t have much info on this, But I just know even if you add theContent-Typeheaders before the actualheader()it won’t work. seems they are completely for different purposes.You can use add the necessary headers and use the
readfile()function instead and it will work fine. But you should know that this function will load/read the file/url you’re calling into the memory. This could be a big problem and will probably slow down your webserver, The worst situation is that it could crash as well. This can happen if you have too downloaders or many files.Unfortunately, as I mentioned before, you can’t fix this in your php code. So what I did in order to fix this problem, was adding the headers in my webserver vhost.
NginX:
In my case, the files users had to download was in mp3 format. But you can change it or set a global directive so it can work with any format.