[PHP+SQL]Pobieranie plików - problem

0

Mam mały problem bo robię skrypt downloadu i nie wiem jak za pomocą php pobrać plik.
np klikamy w link który wygląda tak:
http://localhost/index.php?id=pobierz&fileid=1
i zmienna fileid jest wpisana jako weryfikacja wiersza w sql.
później z tego wiersza jest wypisana jego ścieżka (download/test1.rar), następnie ten kod znalazłem na:
http://us3.php.net/manual/en/function.readfile.php
i powinno pobierać plik.

<?
$read_ = mysql_query("SELECT * FROM download_file WHERE id='".$fileid."'") or die("Błąd! Nie można wykonać."); 
while($r = mysql_fetch_array($read_)) {
$id=$r['id'];
$file=$r['file_dir'];

if (file_exists($file)) {
    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, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
}
?>

A zamiast tego wyskakuje masakryczna ilość błędów:

Warning: Cannot modify header information - headers already sent by (output started at D:\WebServ\httpd\theme\index.php:18) in D:\WebServ\httpd\theme\index.php(40) : eval()'d code on line 8

Warning: Cannot modify header information - headers already sent by (output started at D:\WebServ\httpd\theme\index.php:18) in D:\WebServ\httpd\theme\index.php(40) : eval()'d code on line 9

Warning: Cannot modify header information - headers already sent by (output started at D:\WebServ\httpd\theme\index.php:18) in D:\WebServ\httpd\theme\index.php(40) : eval()'d code on line 10

Warning: Cannot modify header information - headers already sent by (output started at D:\WebServ\httpd\theme\index.php:18) in D:\WebServ\httpd\theme\index.php(40) : eval()'d code on line 11

Warning: Cannot modify header information - headers already sent by (output started at D:\WebServ\httpd\theme\index.php:18) in D:\WebServ\httpd\theme\index.php(40) : eval()'d code on line 12

Warning: Cannot modify header information - headers already sent by (output started at D:\WebServ\httpd\theme\index.php:18) in D:\WebServ\httpd\theme\index.php(40) : eval()'d code on line 13

Warning: Cannot modify header information - headers already sent by (output started at D:\WebServ\httpd\theme\index.php:18) in D:\WebServ\httpd\theme\index.php(40) : eval()'d code on line 14

Warning: Cannot modify header information - headers already sent by (output started at D:\WebServ\httpd\theme\index.php:18) in D:\WebServ\httpd\theme\index.php(40) : eval()'d code on line 15
Rar!��s�� �������
0

W 18 linijce index.php masz wypisanie czegoś na wyjście - przez to headery się nie wysyłają. a ob_clean nie zadziała, jeśli przed wysłaniem danych nie było ob_start.

0

pozwolę sobie zauważyć iż cały ten kod jest ładowany z sql (i wyświetlany jako strona) jak jest to zapisane np pobierz.php i link wygląda tak: pobierz.php?fileid=0 to wszystko działa poprawnie i nic się nie jebie.

Zrobiłem tak i przed
header('Content-Description: File Transfer');
dodałem
ob_start();
błędy poznikały ale plik dalej się nie pobiera tylko wypisuje na stronce:
Rar!��s�� �������

I jak zdążyłem zauważyć prszy plikach większych niż 1MB Firefox zawiesza się (ŻAŁOSNE TO JEST).

I nie otwierając nowego tematu to jak zrobić polskie znaki były ładowane z SQL?

0

Hmm... Nie moge teraz sprawdzić, ale czy przypadkiem ob_clean nie czyści calego bufora? Jeśli do bufora wysyłasz nagłówki, a potem ten bufor czyścisz to dokument jest traktowany jako zwykły plain text i niczego żałosnego w tym nie widzę.

0

Ale on wyświetla plik (jego zawartość wypisuje jako normalny text)jak strona jest czytana z sql, a jak z pliku togo pobiera(jak normalny plik np test.zip) to jest posrane...

0

Boże... Cały skrypt do SQLa pchasz? Ale pomińmy to, nic tu nie ma posranego, twój skrypt działa tak:

Jakieś tam operacje:

$read_ = mysql_query("SELECT * FROM download_file WHERE id='".$fileid."'") or die("Błąd! Nie można wykonać.");
while($r = mysql_fetch_array($read_)) {
$id=$r['id'];
$file=$r['file_dir'];

Potem wysyłasz nagłówki do bufora:

if (file_exists($file)) {
    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, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));

Czyścisz bufor -_-

    ob_clean();

I wypluwasz wszystko (czyli nic, bo bufor jest pusty) do użytkownika

    flush();

A potem do użytkownika jako plain text czytasz plik

    readfile($file);

Jeśli wyślesz nagłówki do bufora i tenże bufor wyczyścisz, to tak jakbyś tych nagłówków w ogóle nie wysłał! Czy to tak trudno zrozumieć? Zrób takie zmiany:

<?
ob_start(); //Zmienione
$read_ = mysql_query("SELECT * FROM download_file WHERE id='".$fileid."'") or die("Błąd! Nie można wykonać.");
while($r = mysql_fetch_array($read_)) {
$id=$r['id'];
$file=$r['file_dir'];

if (file_exists($file)) {
    ob_clean(); //Zmienione
    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, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_end_flush(); //Zmienione
    readfile($file);
    exit;
}
}
?>
0

to dalej się ładnie mówiąc [CIACH!] (zamiast rozpocząć pobieranie to wypisuje plikna stronie)
może jest inny sposób na pobranie pliku tylko nie że jest <a href=" i tak dalej

0

Twoim problemem jest pętla while, gdyż sprawia ona że program nie jest kończony i dlatego zamiast się pobierać wyświetla text.

1 użytkowników online, w tym zalogowanych: 0, gości: 1