Foros del Web » Programando para Internet » PHP »

problema con cannot modify header

Estas en el tema de problema con cannot modify header en el foro de PHP en Foros del Web. Hola, ya se que este tema se ha hablado algunas veces pero no lo acabo de entender jeje. Tengo lo siguiente: Código PHP: ...</head> <body> ...
  #1 (permalink)  
Antiguo 04/01/2011, 15:15
 
Fecha de Ingreso: diciembre-2009
Mensajes: 20
Antigüedad: 14 años, 4 meses
Puntos: 2
problema con cannot modify header

Hola, ya se que este tema se ha hablado algunas veces pero no lo acabo de entender jeje.
Tengo lo siguiente:
Código PHP:
...</head>
<body>
<?php
if(!empty($_GET['id']) && $_GET['a']=="down" && !empty($_GET['photo'])){
    
$root "images/album/".$_GET['id']."/";
    
$file basename($_GET['photo']);
    
$path $root.$file;
    
$type '';
    
    
$size filesize($path); 
    if (
function_exists('mime_content_type')) {
        
$type mime_content_type($path);
    } else if (
function_exists('finfo_file')) {
        
$info finfo_open(FILEINFO_MIME);
        
$type finfo_file($info$path);
        
finfo_close($info);  
    }
    if (
$type == ''){
        
$type "application/force-download";
    }
    
header("Content-Type: $type");
    
header("Content-Disposition: attachment; filename=\"$file\"");
    
header("Content-Transfer-Encoding: binary");
    
header("Content-Length: ".$size);
    
readfile($path);
    
mysql_close();
    
redirect("galeria.php?id=".$_GET['id']);
    exit;
}
?>
    <div id="page" align="center">...
Y me devuelve lo siguiente:
Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/22_shiva/galeria.php:23) in /Applications/XAMPP/xamppfiles/htdocs/22_shiva/galeria.php on line 45

Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/22_shiva/galeria.php:23) in /Applications/XAMPP/xamppfiles/htdocs/22_shiva/galeria.php on line 46

Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/22_shiva/galeria.php:23) in /Applications/XAMPP/xamppfiles/htdocs/22_shiva/galeria.php on line 47

Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/22_shiva/galeria.php:23) in /Applications/XAMPP/xamppfiles/htdocs/22_shiva/galeria.php on line 48
�����JFIF��������C�      ���C    ��������

Alguien me puede ayudar :<
Muchas gracias!
  #2 (permalink)  
Antiguo 04/01/2011, 15:57
Avatar de quinqui  
Fecha de Ingreso: agosto-2004
Ubicación: Chile!
Mensajes: 776
Antigüedad: 19 años, 8 meses
Puntos: 56
Respuesta: problema con cannot modify header

Holas, kyeownz

Como ya lo habrás leído en otros lados, no puedes escribir nada antes de invocar a una instrucción header(). (Una vez leí que había una forma, pero debes entrar a parametrizar ciertas cosas en tu php, que ahora no recuerdo.)

El caso es que ese código HTML previo a tu PHP no debe estar ahí (de hecho PHP te reclamará incluso por un mísero espacio, si es que queda antes del PHP).

Bien puedes mover todo el código PHP al inicio del archivo para que se ejecute bien, porque si en general estás cargando un archivo para descarga no tiene sentido cargar todo el HTML de una página antes. Hay que comprender que cuando invocas a header(), es para inicializar la página de algún modo, y los inicios siempre deben ir ahí, al inicio ^^.

Por cierto, el código raro que te aparece tras los Warning es el esqueleto del archivo que PHP intentó cargar con el header(), pero que no se formó bien por hallar ese código previo a la instrucción.

Saludos :)
__________________
pipus.... vieeeeeji plomius!!!
*quinqui site*

Etiquetas: header, solucion
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:32.