Here's my db_update function then your code with some mods:
<?php
function update_db($location) {
require_once('/usr/home/doc/mysqlconnect.php');
$query = "SELECT dl_count FROM downloads WHERE filelocation = '$location'";
$result = mysql_query($query);
$dl_count = mysql_result($result, 0, "dl_count");
$dl_count++;
$update = "UPDATE downloads SET dl_count = '$dl_count' WHERE filelocation = '$location'";
$result = mysql_query($update);
if(!$result) { mysql_close(); die(); }
mysql_close();
}
class onetime { var $fp = FALSE; function stream_open($path, $mode, $options, $opened_path) { $location = substr($path, 6); $this->fp = fopen($location,'rb'); return TRUE; } function stream_read($count) { $ret = fread($this->fp, filesize($location)); return } function stream_eof() { return feof($this->fp); } // This runs no matter what.
function stream_close() { update_db($location); return fclose($this->fp); } }
////////////////// Then I implement it : ///////////////////
header("Expires: 0");
header("Content-Type: application/download");
$IE = eregi("MSIE", $_SERVER["HTTP_USER_AGENT"]);
$IE6x = eregi("MSIE 6.", $_SERVER["HTTP_USER_AGENT"]);
if($IE) {
header('Pragma: no-cache');
if(!$IE6x) {
header('Cache-Control: no-cache, must-revalidate');
}
header("Content-Type: application/force-download");
} else {
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
}
header("Content-Transfer-Encoding: binary");
header("Connection: close");
header("Content-Type: application/octet-stream");
header("Content-Disposition: inline; filename=\"".$filename."\"");
header("Content-Length: ".filesize($filelocation));
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
stream_wrapper_register("one","onetime") or die(); readfile("one://$filelocation");
Should I use $this->position to run through the file bit by bit? Or can a stream wrapper even perform this function?
John Holmes wrote:
Does the stream_open function in your example actually check if the file has already
been downloaded?
No... I don't know how you're doing that, so it's up to you to write.
I got an error. failed to open stream: infinite recursion prevented in..
But that's probably my fault since if I change $path to the literal location of the file it works.
You need to strip the custom protocol from $path. $path is "mystream://path/to/file" for example and you need to strip off the "mystream://", otherwise you're creating the infinite loop by trying to open the same custom stream again and again and again... I just went through this with some of my own code. :)
I made a function which updates the database and put it in the stream_close
function (and tried it in the stream_eof function). But it seems that if you leave the dialog box
("Save to disk" or "Open with..") open for a while and then hit cancel, it still logs it as a download.
Is there any way to tell the server to not read the file until the user starts downloading?
Or is there someting to do in the stream wrapper?
how are you actually using this wrapper is the question... show some code.
FYI, please include any replies to the list. Others will benefit from this conversation and so will the archives. :)
---John Holmes...
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php