Simulating static file via headers & filestream
Posted: Sun Sep 17, 2006 9:52 pm
Gday Guys
Hoping I can explain exactly what i'm trying to do, without overshadowing the issue at hand. My aim is to provide, using apache mod_rewrite and an activex (i know i know but it's the only viewer available for this filetype) module, a preview of a file (type NWD) which is dynamically retrieved. The script that i'm creating needs to dump the output of the file in such a way as to trick the <object> into believing it is retrieving a static file. (ie. http://some.url.com/blah.nwd). Using mod_rewrite, i can obviously get the browser to believe a static url is a file. My problem lies in the way that I post my header information and the file contents. I can confirm that my object code (html) works perfectly with a static file, and a packet sniff indicates that i'm having problems with the way in which i'm delivering the streamed file.
I've attempted several methods of streaming the file in order to trick the retriever into believing it is a static file, and i'm confident that the header area isn't causing any problems. This is the current code that i'm attempting to use:
Note the several commented out areas to give you an idea of what i've tried. A packet sniff reveals that the calling object is retrieving a portion of the file, usually around 1.5mb. Previous methods have resulted in TCP Window full packets being sent. The current method using the readfile_chunked method (yoinked & modified from a tutorial site) have not resulted in TCP Window Full packets, however the "simulated download" is still stopping around this 1.5mb mark.
I can also confirm that no extraneous data or whitespace is causing problems, and a simple modification of the above to force a header download results in a 100% working file.
As far as I can tell, i am 100% simulating a static file on a server, but having some sort of a problem with the way that the file is being delivered to the sender
*phew* ok, any ideas?
** edit: fixed typo in original code **
Hoping I can explain exactly what i'm trying to do, without overshadowing the issue at hand. My aim is to provide, using apache mod_rewrite and an activex (i know i know but it's the only viewer available for this filetype) module, a preview of a file (type NWD) which is dynamically retrieved. The script that i'm creating needs to dump the output of the file in such a way as to trick the <object> into believing it is retrieving a static file. (ie. http://some.url.com/blah.nwd). Using mod_rewrite, i can obviously get the browser to believe a static url is a file. My problem lies in the way that I post my header information and the file contents. I can confirm that my object code (html) works perfectly with a static file, and a packet sniff indicates that i'm having problems with the way in which i'm delivering the streamed file.
I've attempted several methods of streaming the file in order to trick the retriever into believing it is a static file, and i'm confident that the header area isn't causing any problems. This is the current code that i'm attempting to use:
Code: Select all
else if ($to_be_zipped == 'stream')
{
if ((date('H')-8) < 10) $tmp = 0 . (date('H')-8);
else $tmp = (date('H')-8);
/*header("Content-Disposition:filename=\"navi.nwd\"");*/
header("Last-Modified: " . date("D, d M Y ") . $tmp . date(":i:s \G\M\T"));
header("ETag: \"test\"");
header("Accept-Ranges: bytes");
header("Content-Length: ".filesize($path_to_file));
/*header('Content-Transfer-Encoding: binary');*/
header("Connection: Keep-Alive");
header("Content-Type: text/plain");
ob_flush();
flush();
readfile_chunked($path_to_file);
/*$file = file_get_contents($path_to_file);
echo $file;
ob_flush();
flush();*/
/*$dataFile = fopen( $path_to_file, "r");
if ($dataFile)
{
while (!feof($dataFile))
{
$buffer = fgets($dataFile, 4096);
echo $buffer;
ob_flush();
flush();
}
fclose($dataFile);
}
else
{
die ("fopen couldn't open file");
} */
}
function readfile_chunked ($filename) {
$chunksize = 1*(1504); // how many bytes per chunk
$buffer = '';
$handle = fopen($filename, 'rb');
if ($handle === false)
{
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
print $buffer;
flush();
ob_flush();
usleep(1000);
}
return fclose($handle);
}I can also confirm that no extraneous data or whitespace is causing problems, and a simple modification of the above to force a header download results in a 100% working file.
As far as I can tell, i am 100% simulating a static file on a server, but having some sort of a problem with the way that the file is being delivered to the sender
*phew* ok, any ideas?
** edit: fixed typo in original code **