Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Force download i pliki zip
Black-Berry
post
Post #1





Grupa: Zarejestrowani
Postów: 663
Pomógł: 6
Dołączył: 3.06.2007
Skąd: Kraków

Ostrzeżenie: (0%)
-----


Mam taki kod do force-downlaod :

  1. <?php
  2. /* output the file itself */
  3.            $chunksize = 1 * (1024 * 1024); //you may want to change this
  4.            $bytes_send = 0;
  5.            if ($file = fopen($file, 'r')) {
  6.                if (isset($_SERVER['HTTP_RANGE'])) fseek($file, $range);
  7.                while(!feof($file) &&  (!connection_aborted()) && ($bytes_send<$new_length)) {
  8.                    $buffer = fread($file, $chunksize);
  9.                    echo ($buffer);
  10.                    flush();
  11.                    $bytes_send += strlen($buffer);
  12.                }
  13.                fclose($file);
  14.            } else {
  15.                die('Error - can not open file.');
  16.            }
  17.            //end PHP script...
  18.            exit;
  19. ?>


Wszystko działa ok ale przy plikach zip pojawia się poroblem. Nie można rozpakoać archiwum po sciągnięciu. Zauważyłem że w plikcach brakuje 3 ostatnich bajtów. Wszystkie o kodzie #00; Czyli brakuje 3 bajtowego ciągu #00#00#00.
Czy ktoś mógłby mi powiedzieć dlaczego i jak mógłbym ten problem naprawić ?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Black-Berry
post
Post #2





Grupa: Zarejestrowani
Postów: 663
Pomógł: 6
Dołączył: 3.06.2007
Skąd: Kraków

Ostrzeżenie: (0%)
-----


nie pomaga:/

Oto cały kod:

  1. <?php
  2.    class Download
  3.    {
  4.        /***
  5.          * This function takes a path to a file to output ($file), the filename that
  6.          * the browser will see ($name) and the MIME type of the file ($mimeType, optional).
  7.          *
  8.          * If you want to do something on download abort/finish,
  9.          * register_shutdown_function('function_name');
  10.          */
  11.        public static function OutputFile($file, $name='', $mimeType='')
  12.        {
  13.            if (!is_readable($file)) die('File not found or inaccessible!');
  14.            
  15.            $size = filesize($file);
  16.            if (!$name) $name = basename($file);
  17.            $name = rawurldecode($name);
  18.  
  19.            // Figure out the MIME type (if not specified)...
  20.            $known_mime_types = array(
  21.                "pdf"  => "application/pdf",
  22.                "txt"  => "text/plain",
  23.                "html" => "text/html",
  24.                "htm"  => "text/html",
  25.                "exe"  => "application/octet-stream",
  26.                "zip"  => "application/zip",
  27.                "rar"  => "application/rar",
  28.                "doc"  => "application/msword",
  29.                "xls"  => "application/vnd.ms-excel",
  30.                "ppt"  => "application/vnd.ms-powerpoint",
  31.                "gif"  => "image/gif",
  32.                "png"  => "image/png",
  33.                "jpeg" => "image/jpg",
  34.                "jpg"  => "image/jpg",
  35.                "php"  => "text/plain"
  36.            );
  37.  
  38.            if ($mimeType == '') {
  39.                $file_extension = strtolower(substr(strrchr($file, "."), 1));
  40.                if (array_key_exists($file_extension, $known_mime_types)) {
  41.                    $mimeType = $known_mime_types[$file_extension];
  42.                } else {
  43.                    $mimeType = "application/force-download";
  44.                };
  45.            };
  46.            
  47.            @ob_end_clean(); //turn off output buffering to decrease cpu usage...
  48.  
  49.            // required for IE, otherwise Content-Disposition may be ignored...
  50.            if (ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');
  51.            header('Content-type: text/html; charset=utf-8');
  52.            header('Content-Type: ' . $mimeType);
  53.            header('Content-Disposition: attachment; filename="'.$name.'"');
  54.            header("Content-Transfer-Encoding: binary");
  55.            header('Accept-Ranges: bytes');
  56.  
  57.            // The three lines below basically make the download non-cacheable...
  58.            header("Cache-control: private");
  59.            header('Pragma: private');
  60.            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  61.  
  62.            // multipart-download and download resuming support
  63.            if (isset($_SERVER['HTTP_RANGE'])) {
  64.                list($a, $range) = explode("=", $_SERVER['HTTP_RANGE'], 2);
  65.                list($range) = explode(",", $range, 2);
  66.                list($range, $range_end) = explode("-", $range);
  67.                $range = intval($range);
  68.                if (!$range_end) {
  69.                    $range_end=$size-1;
  70.                } else {
  71.                    $range_end=intval($range_end);
  72.                }
  73.                $new_length = $range_end-$range+1;
  74.                header("HTTP/1.1 206 Partial Content");
  75.                header("Content-Length: $new_length");
  76.                header("Content-Range: bytes $range-$range_end/$size");
  77.            } else {
  78.                $new_length = $size;
  79.                header("Content-Length: " . $size);
  80.            }
  81.            
  82.            /* output the file itself */
  83.            $chunksize = 1 * (1024 * 1024); //you may want to change this
  84.            $bytes_send = 0;
  85.            if ($file = fopen($file, 'r')) {
  86.                if (isset($_SERVER['HTTP_RANGE'])) fseek($file, $range);
  87.                while(!feof($file) &&  (!connection_aborted()) && ($bytes_send<$new_length)) {
  88.                    $buffer = fread($file, $chunksize);
  89.                    echo ($buffer);
  90.                    flush();
  91.                    $bytes_send += strlen($buffer);
  92.                }
  93.                fclose($file);
  94.            } else {
  95.                die('Error - can not open file.');
  96.            }
  97.            //end PHP script...
  98.            exit;
  99.        }    
  100.    }
  101. ?>

Jak na sztywno dopisać 3 brakujace bajty ?
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.10.2025 - 18:46