Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Pobieranie obrazków ze strony
Forum PHP.pl > Forum > Przedszkole
Dzakub
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.
erix
A zaglądałeś: header?
Dzakub
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. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.