Ver Mensaje Individual
  #4 (permalink)  
Antiguo 03/07/2011, 07:36
Avatar de abimaelrc
abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Forzar descarga de un archivo

El enlace que te brindó Panino5001 es bueno, aunque se puede mejorar usando la librería fileinfo y solo indicar aquello que no se pueda descargar.

Código PHP:
Ver original
  1. <?php
  2. function dl_file($file){
  3.  
  4.     //First, see if the file exists
  5.     if (!is_file($file)) { die("<b>404 File not found!</b>"); }
  6.  
  7.     //Gather relevent info about file
  8.     $len = filesize($file);
  9.     $filename = basename($file);
  10.     $finfo = finfo_open(FILEINFO_MIME_TYPE);
  11.     $realExtention = finfo_file($finfo, realpath($file));
  12.     finfo_close($finfo);
  13.  
  14.     //This will set the Content-Type to the appropriate setting for the file
  15.     switch( $realExtention ){
  16.         case 'text/x-php':
  17.         case 'text/html':
  18.         case 'application/x-empty':
  19.         case 'text/plain': die('error');
  20.     }
  21.  
  22.     //Begin writing headers
  23.     header("Pragma: public");
  24.     header("Expires: 0");
  25.     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  26.     header("Cache-Control: public");
  27.     header("Content-Description: File Transfer");
  28.     header("Content-Type: $realExtention");
  29.  
  30.     //Force the download
  31.     header("Content-Disposition: attachment; filename=$filename;");
  32.     header("Content-Transfer-Encoding: binary");
  33.     header("Content-Length: ".$len);
  34.     @readfile($file);
  35.     exit;
  36. }
  37. dl_file('foo.png');
Algo así es que lo haría.
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos

Última edición por abimaelrc; 03/07/2011 a las 08:05