Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Pobieranie obrazków ze strony, Force download czy coś
Dzakub
post
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 8.05.2010

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


Witam.
Chciałbym, żeby na mojej stronie po kliknięciu na link pobierało obrazek (ale jeśli kliknąć na bezpośredni link otwierało w przeglądarce). Dość dużo szukałem, ale bez rezultatów. Proszę o pomoc.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
erix
post
Post #2





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




A zaglądałeś: header?


--------------------

ZCE :: Pisząc PW załączaj LINK DO TEMATU i TYLKO w sprawach moderacji :: jakiś błąd - a TREŚĆ BŁĘDU? :: nie ponaglaj z odpowiedzią via PW!
Go to the top of the page
+Quote Post
Dzakub
post
Post #3





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 8.05.2010

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


No, ale nie ma tam za wiele informacji :/

edit: Znalazłem, po 2 przeglądnięciu header. Jeśli ktoś chce to proszę:
  1. <?php
  2.  
  3. function dl_file($file){
  4.  
  5. //First, see if the file exists
  6. if (!is_file($file)) { die("<b>404 File not found!</b>"); }
  7.  
  8. //Gather relevent info about file
  9. $len = filesize($file);
  10. $filename = basename($file);
  11. $file_extension = strtolower(substr(strrchr($filename,"."),1));
  12.  
  13. //This will set the Content-Type to the appropriate setting for the file
  14. switch( $file_extension ) {
  15. case "pdf": $ctype="application/pdf"; break;
  16. case "exe": $ctype="application/octet-stream"; break;
  17. case "zip": $ctype="application/zip"; break;
  18. case "doc": $ctype="application/msword"; break;
  19. case "xls": $ctype="application/vnd.ms-excel"; break;
  20. case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  21. case "gif": $ctype="image/gif"; break;
  22. case "png": $ctype="image/png"; break;
  23. case "jpeg":
  24. case "jpg": $ctype="image/jpg"; break;
  25. case "mp3": $ctype="audio/mpeg"; break;
  26. case "wav": $ctype="audio/x-wav"; break;
  27. case "mpeg":
  28. case "mpg":
  29. case "mpe": $ctype="video/mpeg"; break;
  30. case "mov": $ctype="video/quicktime"; break;
  31. case "avi": $ctype="video/x-msvideo"; break;
  32.  
  33. //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
  34. case "php":
  35. case "htm":
  36. case "html":
  37. case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;
  38.  
  39. default: $ctype="application/force-download";
  40. }
  41.  
  42. //Begin writing headers
  43. header("Pragma: public");
  44. header("Expires: 0");
  45. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  46. header("Cache-Control: public");
  47. header("Content-Description: File Transfer");
  48.  
  49. //Use the switch-generated Content-Type
  50. header("Content-Type: $ctype");
  51.  
  52. //Force the download
  53. $header="Content-Disposition: attachment; filename=".$filename.";";
  54. header($header );
  55. header("Content-Transfer-Encoding: binary");
  56. header("Content-Length: ".$len);
  57. @readfile($file);
  58. }
  59.  
  60. ?>


Ten post edytował Dzakub 9.05.2010, 15:21:50
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 Aktualny czas: 20.08.2025 - 11:26