Forum Moderators: coopster
readfile("file");
will not work, as php can time out, or stop responding, or use too much ram, etc..
header("Location: file");
will not work either.. as this will invoke .htaccess
can anyone think of anyway to redirect a user to a file without invoking htaccess?
heres the problem.. im redirecting all files to a php script via htaccess, and im hoping to be able to have php allow the user to download the file if the user passes the test within the php script (ip, referrer, etc). but the problem is, when the php script tries to redirect the user with header("Location: file"), htaccess is invoked again, and htaccess points the user back to the php script.. creating an infinite loop
so what do you suggest, how can i overcome this problem?
Your readfile is your best bet. I am not sure of the internals of readfile but you say it takes to much ram maybe instead of readfile you will want to do something like:
// ensure output buffering is off!
ob_end_flush();
$chunk_size=2048;
$fp=fopen($filename,'r');
if(!$fp ) die("could not open file"); //clean this up!
while($buf=fread($fp,$chunk_size)) {
echo $buf;
}
You may also want to add a "set_time_limit(0)" at the top of the script to ensure that PHP does not timeout after 30seconds to account for a long download.
This would now not use more that $chunk_size bytes at any time for reading and sending the file. Remember the footprint of the script will be larger than that but that shouldn't be an issue.
daisho.