Forum Moderators: coopster

Message Too Old, No Replies

Can't get file to download

         

toucan

3:01 pm on Nov 17, 2005 (gmt 0)

10+ Year Member



Hi,

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.

Receptional Andy

3:47 pm on Nov 17, 2005 (gmt 0)



I believe you can just send a redirect header to get the visitor to start downloading the file:


header("Location: $file_to_download");
exit;

toucan

3:55 pm on Nov 17, 2005 (gmt 0)

10+ Year Member



I tried what you said, removing any output before the header call. I think it worked but I get an error saying:

"Redirection limit for this URL exceeded. Unable to load requested page. This may be caused by cookies that are blocked."

Receptional Andy

3:57 pm on Nov 17, 2005 (gmt 0)



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

toucan

4:11 pm on Nov 17, 2005 (gmt 0)

10+ Year Member



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. So maybe the

header("Location: $file_to_download");

is redirecting the browser to a .rar file again, causing the loop?

Receptional Andy

4:23 pm on Nov 17, 2005 (gmt 0)



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.

toucan

4:27 pm on Nov 17, 2005 (gmt 0)

10+ Year Member



Ya, I've been over to the .htaccess forum already before coming over here.

Thanks