Envia un archivo para ser descargado desde el explorador.

PHP:
  1. /**
  2. * ENVIA UN ARCHIVO PARA DESCARGAR
  3. * $path direccion completa del archivoa descargar
  4. */
  5. function send_file($path) {
  6.    ob_end_clean();
  7.    if (!is_file($path) || connection_status()!=0)//existe el archivo
  8.        return(FALSE);
  9.  
  10.     //para que no se detenga la descarga
  11.  
  12.    $name=basename($path);
  13.  
  14.    //Para evitar errores al enviar puntos en el nombre
  15.  
  16.    if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
  17.        $name = preg_replace('/\./', '%2e', $name, substr_count($name, '.') - 1);
  18.  
  19.         //$name sera el nombre que recibira la descarga al llegar al cliente
  20.  
  21.     //Headers requeridos para forzar la descarga
  22.  
  23.    header("Cache-Control: ");
  24.    header("Pragma: ");
  25.    header("Content-Type: application/octet-stream");
  26.    header("Content-Length: " .(string)(filesize($path)) );
  27.    header('Content-Disposition: attachment; filename="'.$name.'"');
  28.    header("Content-Transfer-Encoding: binary\n");
  29.  
  30.     //Enviamos el archivo
  31.    if($file = fopen($path, 'rb')){
  32.        while( (!feof($file)) && (connection_status()==0) ){
  33.            print(fread($file, 1024*8));
  34.            flush();
  35.        }
  36.        fclose($file);
  37.    }
  38.    return((connection_status()==0) and !connection_aborted());
  39.    
  40.   // exit();//Para terminar de procesar mas codigo
  41.    
  42. }