Eric Pratt
Eric Pratt

Reputation: 11

PHP issue on IIS, maybe

V1rtualset

I'm not really sure what field this problem falls under, but my closest bet would be php configuration. Here's the problem, I have an ecommerce platform from SumEffect called Digishop that is php based. The downloading feature allowing customers to download products is causing an error which means my customers can't get their products. SumEffect support has washed their hands of the problem saying that the problem is mine. Without remonstrating me regarding whether I'm suited to run a server could someone take a look at this code and error message and make an enlighted guess as to what things I might try to fix it?

Server 2008 R2 IIS 7.5 PhP 5.3.6

Here's the Code

$path = 'C:/Documents/Virtualsetworks/files/VSPHD1080S164.zip';
$downloadFile = basename($path);
$filesize = filesize($path);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $downloadFile . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

if($filesize){
    header('Content-Length: ' . $filesize);
}

header('Content-Encoding: chunked'); //This is passed so in case the default encoding of the server uses compression a progress bar will display.


$handle = fopen($path, 'rb');
$chunksize = 1024;
$length = 0;
ob_start();

while($line = fread($handle, $chunksize)){
    echo $line;
    ob_end_flush();
    ob_flush();
    flush();
    ob_start();
}

fclose($handle);
ob_end_clean();

?>

Here's the error on the client side: "F:\E\temp\VSPHD1080S164(13).zip.part could not be saved, because the source file could not be read. Try again later, or contact the server administrator."

My clients get this too, it's not just me.

It usually reads between 9 and 10 megs of the file and then stops, waits a bit, and then throws that message. Sometimes it reads 0 bytes, or just a few bytes, but mostly it goes to 9.3MB. PHP.ini memory_limit is 1024Mb, max-execution time is 1200. The folder has all the correct security permissions. The files in question are between 100-1000Mb. zlib.output_compression = Off max_execution_time = 1200 max_input_time = 60 memory_limit = 1024M IIS 7.5 Windows 2008 R2

Any help would be appreciated

Upvotes: 1

Views: 510

Answers (1)

Marc B
Marc B

Reputation: 360562

Why are you repeatedly starting/stopping output buffering in the "dump the file contents" section? that's a hideous waste of cpu cycles. Just do:

// open the file first thing, so the die() can be seen, if something fails
$handle = fopen($file, 'rb') or die("Unable to open $file");

// THEN output the download headers    
header(...);
header(...);

// simple while loop, no need for any buffering/flushing.    
while(!feof($handle)) {
    echo fread($handle, $chunksize);
}

As well, with your 'Content-encoding' header is incorrect. For chunked downloads, you use Transfer-encoding. Since you're sending the entire file, there's no need for chunked encoding - you're not doing proper chunking anyways. With chunked transfers, you have to output a content-length for each chunk as it's sent.

Upvotes: 1

Related Questions