1

phpdocx を使用してワード文書を作成しています。サーバー上で見ると有効なワード文書です。しかし、ダウンロードしようとすると、ファイルに問題があるため、ファイルを開くことができませんと表示されます。Word はドキュメントを回復できますが、クライアントは無効な Word ドキュメントについて文句を言います。ファイルをダウンロードするために使用するコードは次のとおりです。

function readfile_chunked_remote($filename, $seek = 0, $retbytes = true, $timeout = 3) {
    set_time_limit(0); 
    $defaultchunksize = 1024*1024; 
    $chunksize = $defaultchunksize; 
    $buffer = ''; 
    $cnt = 0; 
    $remotereadfile = false; 


if (preg_match('/[a-zA-Z]+:\/\//', $filename)) 
$remotereadfile = true; 

$handle = @fopen($filename, 'rb'); 

if ($handle === false) { 
return false; 
} 

stream_set_timeout($handle, $timeout); 

if ($seek != 0 && !$remotereadfile) 
fseek($handle, $seek); 

while (!feof($handle)) { 

if ($remotereadfile && $seek != 0 && $cnt+$chunksize > $seek) 
    $chunksize = $seek-$cnt; 
else 
    $chunksize = $defaultchunksize; 

$buffer = @fread($handle, $chunksize); 

if ($retbytes || ($remotereadfile && $seek != 0)) { 
    $cnt += strlen($buffer); 
} 

if (!$remotereadfile || ($remotereadfile && $cnt > $seek)) 
    echo $buffer; 

ob_flush(); 
flush(); 
} 

$info = stream_get_meta_data($handle); 

$status = fclose($handle); 

if ($info['timed_out']) 
return false; 

if ($retbytes && $status) { 
return $cnt; 
} 

return $status; 

}

ありがとう!

4

0 に答える 0