Ver Mensaje Individual
  #13 (permalink)  
Antiguo 25/04/2013, 09:47
pr0
 
Fecha de Ingreso: marzo-2004
Mensajes: 687
Antigüedad: 20 años, 1 mes
Puntos: 28
Respuesta: problema con forzar descarga de archivos

Te paso el método que hice para la descarga de ficheros. Me pasaba lo mismo que a tí y lo conseguí arreglar.

Al final el secreto estaba en usar ob_end_clean() en lugar de ob_clean()

Código PHP:
public function download(){

            
$file'./files/archivo.txt';
            
$filenamebasename($file);
            
$type '';
             
            if (
is_file($file)) {
                
$size filesize($file);
                if (
function_exists('mime_content_type')) {
                    
$type mime_content_type($file);
                } else if (
function_exists('finfo_file')) {
                    
$info finfo_open(FILEINFO_MIME);
                    
$type finfo_file($info$file);
                    
finfo_close($info);
                }
                if (
$type == '') {
                    
$type "application/force-download";
                }
                
// Set Headers
                
header("Content-Type: $type");
                
header("Content-Disposition: attachment; filename=$filename");
                
header("Content-Transfer-Encoding: binary");
                
header("Content-Length: " $size);
                
// Download File
                
ob_end_clean();
                
flush();
                
readfile($file);
            } else {
                echo 
$file.' no es un archivo.';
            }
        


Edito:

por lo que veo en tu último post, el error te indica claramente que no puede encontrar el archivo hola.txt, seguramente porque estás indicando mal la ruta.