Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] pobieranie pliku
patryk20120
post 15.11.2008, 18:32:04
Post #1





Grupa: Zarejestrowani
Postów: 256
Pomógł: 1
Dołączył: 20.04.2008

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


Witam mam taki problem.
Mam ten kod na stronie:
  1. <?php
  2. $file = 'test.doc';
  3.  
  4.    if (! is_file($file))
  5.    {
  6.        header('HTTP/1.1 404 File Not Found');
  7.        die();
  8.    }
  9.  
  10.    $fname = basename($file);
  11.    $fsize = filesize($file);
  12.    $ftime = filemtime($file);
  13.    $range = $_SERVER['HTTP_RANGE'];
  14.  
  15.    $r_start = 0;
  16.    $c_length = $fsize;
  17.  
  18.    if ($range)
  19.    {
  20.        $reg = "/bytes=([0-9]+)-([0-9]*)/";
  21.        preg_match($reg, $range, $matches);
  22.  
  23.        $r_start = (int) $matches[1];
  24.        $r_stop = (int) $matches[2];
  25.        if ($r_stop < $r_start) $r_stop = $fsize - 1;
  26.        $c_length = $r_stop - $r_start + 1;
  27.  
  28.        header('HTTP/1.1 206 Partial Content');
  29.        header('Content-Range: bytes ' .
  30.            $r_start . '-' . $r_stop . '/' . $fsize);
  31.    }
  32.    else
  33.    {
  34.        header('HTTP/1.1 200 OK');
  35.    }
  36.  
  37.    header('Last-Modified: ' .
  38.        gmdate('D, d M Y H:i:s', $ftime) . ' GMT');
  39.    header('Content-Disposition: ' .
  40.        'attachment; filename="' . $fname . '"');
  41.    header('Accept-Ranges: bytes');
  42.    header('Content-Type: application/x-unknown');
  43.    header('Content-Transfer-Encoding: binary');
  44.    header('Content-Length: ' . $c_length);
  45.  
  46.    if ($fp = fopen($file, 'rb'))
  47.    {
  48.        flock($fp, 1);
  49.        fseek($fp, $r_start);
  50.        echo(fread($fp, $c_length));
  51.        flock($fp, 3);
  52.        fclose($fp);
  53.    }
  54. ?>

Lecz zamiast mi go pobierać to on się wyświetla (chyba bo jakies znaczki tongue.gif ) + błędy typu:
Warning: Cannot modify header information - headers already sent by...
Co zrobić, aby mi ten plik pobierało questionmark.gif
Go to the top of the page
+Quote Post
piotrooo89
post 15.11.2008, 18:36:06
Post #2


Newsman


Grupa: Moderatorzy
Postów: 4 005
Pomógł: 548
Dołączył: 7.04.2008
Skąd: Trzebinia/Kraków




było naprawdę setki razy na forum o tym poszukaj i poczytaj!


--------------------
Go to the top of the page
+Quote Post
patryk20120
post 15.11.2008, 22:49:09
Post #3





Grupa: Zarejestrowani
Postów: 256
Pomógł: 1
Dołączył: 20.04.2008

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


No dobrze, mam pobiera mi go, lecz wyświetla treść( w pliku pobranym) źródło strony, a nie zawartość pliku...

Dodałem to:
  1. <?php
  2. header("Content-Type: application/msword");
  3. ?>

lecz w pliku nadal jest kod strony...
Go to the top of the page
+Quote Post
marcio
post 15.11.2008, 22:52:21
Post #4





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


Poprostu daj kod ktory ma robic download plikow do oddzielnego pliku *.php gdzie nie ma kodu html i bedzie good tez mialem taki problem.

  1. <?php
  2. if ($fp = fopen($file, 'rb'))
  3.   {
  4.       flock($fp, 1);
  5.       fseek($fp, $r_start);
  6.       echo(fread($fp, $c_length));
  7.       flock($fp, 3);
  8.       fclose($fp);
  9.   }
  10. ?>

Oszczedz to sobie i zastap za pomoca readfile($file);

Ten post edytował marcio 15.11.2008, 22:53:18


--------------------
Zainteresowania: XML | PHP | MY(SQL)| C# for .NET | PYTHON
http://code.google.com/p/form-builider/
Moj blog
Go to the top of the page
+Quote Post
patryk20120
post 15.11.2008, 22:54:16
Post #5





Grupa: Zarejestrowani
Postów: 256
Pomógł: 1
Dołączył: 20.04.2008

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


marcio, tak, lecz po kodzie strony jest treść pliku, lecz są to same jakieś znaczki tongue.gif
Go to the top of the page
+Quote Post
marcio
post 15.11.2008, 23:14:39
Post #6





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


Blad napewno siedzi w naglowkach i tym niepotrzebnym kombinowaniem podczac wczytywaniu pliu daj tylko potrzebne naglowki i tyle:
  1. <?php
  2. if (! is_file($file))
  3.    {
  4.        header('HTTP/1.1 404 File Not Found');
  5.        die();
  6.    }
  7. else {
  8.  
  9.            header('Content-type: application/x-unknown');
  10.            header('Content-Disposition: attachment; filename='.$file);
  11.            header('Connection: close');
  12.            header('Expires: 0');
  13.            readfile($file);
  14. }
  15. ?>


--------------------
Zainteresowania: XML | PHP | MY(SQL)| C# for .NET | PYTHON
http://code.google.com/p/form-builider/
Moj blog
Go to the top of the page
+Quote Post
patryk20120
post 15.11.2008, 23:25:08
Post #7





Grupa: Zarejestrowani
Postów: 256
Pomógł: 1
Dołączył: 20.04.2008

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


Dalej to samo, plik się pobiera w dobrym formacie, lecz jest w nim kod strony tongue.gif
Go to the top of the page
+Quote Post
marcio
post 15.11.2008, 23:32:00
Post #8





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


Jaki kod wklej go tu.


--------------------
Zainteresowania: XML | PHP | MY(SQL)| C# for .NET | PYTHON
http://code.google.com/p/form-builider/
Moj blog
Go to the top of the page
+Quote Post
patryk20120
post 15.11.2008, 23:34:27
Post #9





Grupa: Zarejestrowani
Postów: 256
Pomógł: 1
Dołączył: 20.04.2008

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


Wklejam tak jak mi napisałeś tongue.gif
  1. <?php
  2. $file = 'test.doc';
  3.  
  4.   if (! is_file($file))
  5.   {
  6.       header('HTTP/1.1 404 File Not Found');
  7.       die();
  8.   }
  9. else {
  10.  
  11.           header('Content-type: application/x-unknown');
  12.           header('Content-Disposition: attachment; filename='.$file);
  13.           header('Connection: close');
  14.           header('Expires: 0');
  15.           readfile($file);
  16. }
  17. ?>
Go to the top of the page
+Quote Post
marcio
post 15.11.2008, 23:38:09
Post #10





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


Pokaz zawartosc pliku jakiego ci pobiera.


--------------------
Zainteresowania: XML | PHP | MY(SQL)| C# for .NET | PYTHON
http://code.google.com/p/form-builider/
Moj blog
Go to the top of the page
+Quote Post
patryk20120
post 16.11.2008, 11:41:52
Post #11





Grupa: Zarejestrowani
Postów: 256
Pomógł: 1
Dołączył: 20.04.2008

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


Jest to tabelka z tekstem w formacie *.doc...
Go to the top of the page
+Quote Post
phpion
post 16.11.2008, 11:44:59
Post #12





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Zamiast:
  1. <?php
  2. header('Content-type: application/x-unknown');
  3. ?>

daj:
  1. <?php
  2. header('Content-type: application/msword');
  3. ?>
Go to the top of the page
+Quote Post
patryk20120
post 16.11.2008, 12:17:54
Post #13





Grupa: Zarejestrowani
Postów: 256
Pomógł: 1
Dołączył: 20.04.2008

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


Dawałem już wcześniej i dalej nie pomaga...
Go to the top of the page
+Quote Post
phpion
post 16.11.2008, 12:22:31
Post #14





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




No to jeszcze to:
  1. <?php
  2. header('Content-Disposition: attachment; filename='.$file);
  3. ?>

na:
  1. <?php
  2. header('Content-Disposition: attachment; filename="'.$file.'"');
  3. ?>

i po readfile($file); daj dla pewności:
  1. <?php
  2. ?>
Go to the top of the page
+Quote Post
-gox-
post 16.11.2008, 14:35:23
Post #15





Goście







Eh ludzki, jakie readfile()...
Na prawde nie widzicie, że skrypt służy do pobierania przerywanego, pozwala na wznawianie....
bez fseek() raczej ciężko..

Kodzik przekopiowany na żywca z tej strony: http://4programmers.net/PHP/Pobieranie_pli...nego_połączenia
Go to the top of the page
+Quote Post
patryk20120
post 16.11.2008, 14:40:31
Post #16





Grupa: Zarejestrowani
Postów: 256
Pomógł: 1
Dołączył: 20.04.2008

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


Niestety nie działa, mój teraźniejszy kod wygląda tak:
  1. <?php
  2. $file = 'test.doc';
  3.  
  4.   if (! is_file($file))
  5.   {
  6.       header('HTTP/1.1 404 File Not Found');
  7.       die();
  8.   }
  9. else {
  10.  
  11.           header('Content-type: application/msword');
  12.           header('Content-Disposition: attachment; filename="'.$file.'"');
  13.           header('Connection: close');
  14.           header('Expires: 0');
  15.           readfile($file);
  16.           exit;
  17. }
  18. ?>
Go to the top of the page
+Quote Post
l0ud
post 16.11.2008, 14:44:51
Post #17





Grupa: Zarejestrowani
Postów: 1 387
Pomógł: 273
Dołączył: 18.02.2008

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


Upewnij się, czy przed <?php nie masz żadnego znaku (chociażby nowej linii, spacji), oraz czy ten plik (skrypt) nie jest zapisany w kodowaniu UTF-8 z BOM.


--------------------
XMPP: l0ud@chrome.pl
Go to the top of the page
+Quote Post
patryk20120
post 16.11.2008, 14:53:11
Post #18





Grupa: Zarejestrowani
Postów: 256
Pomógł: 1
Dołączył: 20.04.2008

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


przed <?php nie ma nic, lecz gdy zmieniam na UTF-8 z BOM to zamiast plik pobierać to wyświetla jakieś znaczki na stronie...
Go to the top of the page
+Quote Post
piotrooo89
post 16.11.2008, 14:54:43
Post #19


Newsman


Grupa: Moderatorzy
Postów: 4 005
Pomógł: 548
Dołączył: 7.04.2008
Skąd: Trzebinia/Kraków




poczytaj co to jest BOM... jak dasz sobie z BOM to będą się wyświetlać.


--------------------
Go to the top of the page
+Quote Post
patryk20120
post 16.11.2008, 14:58:41
Post #20





Grupa: Zarejestrowani
Postów: 256
Pomógł: 1
Dołączył: 20.04.2008

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


No właśnie tongue.gif nie chcę żeby plik wyświetlało na stroni i to jeszcze zniekształcony tylko żeby go pobrało...
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 22.06.2025 - 23:25