Forum Moderators: coopster
I'm not all that new to PHP but I'm working on something I've never tried before and having trouble. I have a site that contains hundreds of zip files to download all in a /members directory. Users of the site pay for a certain number of bytes that they can then use to download these files. I'm trying to set it up so that when someone clicks a link to download one of the files, .htaccess rewrites(redirects) to a php script that checks its files size and then allows or disallows the download. My .htaccess redirects to the php script ok. But now how do I begin the download when I need to? This is probably a simple question but for some reason I'm confused by this. Below is a simplified version of my script.
<?php
$header = "header.html";
$footer = "footer.html";
readfile($header);
echo "File to Download: $file_to_download<br>";
$file_size = filesize($file_to_download);
echo "Size: $file_size<br>";
if enough available credit {
//begin download
}
else {
//error message
}
readfile($footer);
?>
Any help would be greatly appreciated.
header("Location: $file_to_download");
exit;
"Redirection limit for this URL exceeded. Unable to load requested page. This may be caused by cookies that are blocked."
Odd - this error indicates a redirection loop (a file that redirects to a file that redirects back usually). It suggests that $file_to_download is the same as the page containing this code. Do you still get this error if you hardcode a file to download? e.g.
header("Location: http://example.com/somefile.zip");
exit;
The directory containing the .rar files to be downloaded also has a .htaccess in it that uses RewriteRule to redirect all requests to .rar files to my php script.
Aha! That would certainly account for the problem, and makes things a bit more complicated - should have paid more attention to the original post!
This means we need to be able to alter your mod_rewrite rule so that validated visitors redirected by the script are not redirected. I believe this might be achieved by setting an environment variable that can then be read by the htaccess file (RewriteCond %{ENV:ALLOW_DOWNLOAD} or something similar.
mod_rewrite isn't my specialty unfortunately, so hopefully someone else could advise you on this.