Text Resizer Text Resizer
February 8th, 2012
You're browsing: Scriptmatico.Com » PHP » Forzar la descarga de un fichero (Archivo) con PHP otra forma

Forzar la descarga de un fichero (Archivo) con PHP otra forma

Posted on abr 22 in PHPby PrintText Resizer Text Resizer

Hace unas semanas puse una forma de forzar la descarga de un archivo con PHP pero ahora les pongo otra forma:

PHP:
  1. function descargaFichero ($fichero)
  2. {
  3. $filename = basename($fichero);
  4. $filesize = filesize($fichero);
  5.  
  6. // 'application/octet-stream' es un tipo MIME registrado pero
  7. // MSIE y Opera prefieren manejarlo asi 'application/octetstream'
  8. $USR_BROWSER_AGENT="";
  9. if (preg_match('@Opera(/| )([0-9].[0-9]{1,2})@', $_SERVER['HTTP_USER_AGENT'])) $USR_BROWSER_AGENT='OPERA';
  10. if (preg_match('@MSIE ([0-9].[0-9]{1,2})@', $_SERVER['HTTP_USER_AGENT'])) $USR_BROWSER_AGENT='IE';
  11. $mime_type = ($USR_BROWSER_AGENT == 'IE' || $USR_BROWSER_AGENT == 'OPERA')
  12. ? 'application/octetstream'
  13. : 'application/octet-stream';
  14.  
  15. // Esta funcion esta operativa desde php 4.3.0 y puede ser una buena opcion para obtener el tipo MIME de un
  16. // fichero y su extension
  17.  
  18. //$mime_type=mime_content_type ($fichero);
  19.  
  20. header('Content-Type: ' . $mime_type);
  21. // Se informa al navegador del tamaño del fichero y puede mostrar la barra de
  22. // progreso de descarga
  23. header('Content-Length: ' . filesize($fichero));
  24. header('Content-Transfer-Encoding: binary');
  25. header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  26. // IE necesita que le especifiquen las cabeceras
  27.  
  28. if ($USR_BROWSER_AGENT == 'IE')
  29. {
  30. //header('Content-Disposition: inline; filename="' . $filename . '"');
  31. header('Content-Disposition: attachment; filename="' . $filename . '"');
  32. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  33. header('Pragma: public');
  34. }
  35. else
  36. {
  37. header('Content-Disposition: attachment; filename="' . $filename . '"');
  38. header('Pragma: no-cache');
  39. }
  40. @readfile ($fichero);
  41. exit();
  42. }

Para usar el script es solo mandar a llamar la funcion de esta forma:

PHP:
  1. descargaFichero ("miarchivo.ppt");

Related posts:

  1. Forzar la descarga de un fichero con PHP
  2. Crear archivos ZIP con PHP (Otra forma)
  3. Insertar o Migrar datos desde un archivo de texto en mysql

Leave a Reply

You must be logged in to post a comment.

Back to Top