I can not figure out in any way which headers to send, so that the archive comes not broken and files are normally extracted from it. At the moment I tried many options, one of them is this:

header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; 

Even if you dynamically substitute the mime type, the file is not extracted from the archive.

There is such an example:

  $file = '/path/do/filename.txt'; // или любое др расширение $filename = 'имя которое увидит юзер.txt'; // расширение тут обязательно header ('Content-Type: application/octet-stream'); header ('Accept-Ranges: bytes'); header ('Content-Length: '.filesize($file)); header ('Content-Disposition: attachment; filename='.$filename); readfile($file); exit(); 

Advise how to be?

    1 answer 1

     function download($filename, $mimetype='application/octet-stream') { if (!file_exists($filename)) return 'Файл не найден'; $from=$to=0; $cr=NULL; if (isset($_SERVER['HTTP_RANGE'])) { $range=substr($_SERVER['HTTP_RANGE'], strpos($_SERVER['HTTP_RANGE'], '=')+1); $from=strtok($range, '-'); $to=strtok('/'); if ($to>0) $to++; if ($to) $to-=$from; header('HTTP/1.1 206 Partial Content'); $cr='Content-Range: bytes ' . $from . '-' . (($to)?($to . '/' . $to+1):filesize($filename)); } else header('HTTP/1.1 200 Ok'); $etag=md5($filename); $etag=substr($etag, 0, 8) . '-' . substr($etag, 8, 7) . '-' . substr($etag, 15, 8); header('ETag: "' . $etag . '"'); header('Accept-Ranges: bytes'); header('Content-Length: ' . (filesize($filename)-$to+$from)); if ($cr) header($cr); header('Connection: close'); header('Content-Type: ' . $mimetype); header('Last-Modified: ' . gmdate('r', filemtime($filename))); $f=fopen($filename, 'r'); header('Content-Disposition: attachment; filename="' . basename($filename) . '";'); if ($from) fseek($f, $from, SEEK_SET); if (!isset($to) or empty($to)) { $size=filesize($filename)-$from; } else { $size=$to; } $downloaded=0; while(!feof($f) and !connection_status() and ($downloaded<$size)) { echo fread($f, 512000); $downloaded+=512000; flush(); } fclose($f); return 0; } 
    • I understand here with the resume file is implemented? As Pts it is difficult to work - SnowMan
    • ! C: \ Users \ User \ Desktop \ PayPal_Platform_PHP_SDK_N (1) .zip: Unexpected end of archive gives out what if I use your function. size archive is downloaded correctly - SnowMan
    • Or maybe it is worth trying to step aside for a minute from the image of "I am cool and I know PCP" and read the RFC? - user6550