Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. <?
  2. $arquivo = "imagens/image.jpg";
  3. if(isset($arquivo) && file_exists($arquivo)){
  4. // faz o teste se a variavel não esta vazia e se o arquivo realmente existe
  5. switch(strtolower(substr(strrchr(basename($arquivo),"."),1))){
  6. // verifica a extensão do arquivo para pegar o tipo
  7. case "pdf": $tipo="application/pdf"; break;
  8. case "exe": $tipo="application/octet-stream"; break;
  9. case "zip": $tipo="application/zip"; break;
  10. case "doc": $tipo="application/msword"; break;
  11. case "xls": $tipo="application/vnd.ms-excel"; break;
  12. case "ppt": $tipo="application/vnd.ms-powerpoint"; break;
  13. case "gif": $tipo="image/gif"; break;
  14. case "png": $tipo="image/png"; break;
  15. case "jpg": $tipo="image/jpg"; break;
  16. case "mp3": $tipo="audio/mpeg"; break;
  17. case "php": // deixar vazio por segurança
  18. case "htm": // deixar vazio por segurança
  19. case "html": // deixar vazio por segurança
  20. }
  21. header("Content-Type: ".$tipo);
  22. // informa o tipo do arquivo ao navegador
  23. header("Content-Length: ".filesize($arquivo));
  24. // informa o tamanho do arquivo ao navegador
  25. header("Content-Disposition: attachment; filename=".basename($arquivo));
  26. // informa ao navegador que é tipo anexo e faz abrir a janela de download,
  27. //tambem informa o nome do arquivo
  28. readfile($arquivo); // lê o arquivo
  29. exit; // aborta pós-ações
  30. }
  31. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement