Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Szukam rozwiązania - pobieranie konkretnych plików z serwera
tomec
post 13.01.2016, 00:46:37
Post #1





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 16.12.2011

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


Witam,

Znalazłem w archiwach forum skrypt jak poniżej i robi to czego potrzebuję z jednym wyjątkiem: zamiast pobierać pliki na komputer to uparcie wyświetla je w przeglądarce (w formie krzaczków). Chodzi mi dokładnie o to żeby user pobierał plik PDF z serwera o znanej mu nazwie (wpisuje nazwę w formularzu i pobiera mu się plik PDF o takiej nazwie). Może ktoś zna rozwiązanie tego problemu?

<?php

echo'<form action=test.php method=post>
<input type=text name=cos id=cos>
<br><input type=submit name=pobierz value=pobierz>
</form>';

if(isset($_POST['pobierz']))
{
$file = $_POST['cos'];

//First, see if the file exists
if (!is_file($file)) { die("<b>404 File not found!</b>"); }

//Gather relevent info about file
$len = filesize($file);
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));

//This will set the Content-Type to the appropriate setting for the file
switch( $file_extension ) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
case "mp3": $ctype="audio/mpeg"; break;
case "wav": $ctype="audio/x-wav"; break;
case "mpeg":
case "mpg":
case "mpe": $ctype="video/mpeg"; break;
case "mov": $ctype="video/quicktime"; break;
case "avi": $ctype="video/x-msvideo"; break;

//The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
case "php":
case "htm":
case "html":
case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;

default: $ctype="application/force-download";
}

//Begin writing headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");

//Use the switch-generated Content-Type
header("Content-Type: $ctype");

//Force the download
@$header="Content-Disposition: attachment; filename=".$filename.";";
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len);
@readfile($file);
exit;
}

?>
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: 11.05.2024 - 10:52