Foros del Web » Programando para Internet » PHP »

Tengo algo mal en este codigo, por favor ayudarme

Estas en el tema de Tengo algo mal en este codigo, por favor ayudarme en el foro de PHP en Foros del Web. Mi problema es el siguiente, tengo un codigo que me almacena todo tipos de archivos (jpg,pdf..) en la base de datos, el tema es que ...
  #1 (permalink)  
Antiguo 10/05/2012, 13:38
 
Fecha de Ingreso: mayo-2012
Mensajes: 5
Antigüedad: 11 años, 11 meses
Puntos: 0
Tengo algo mal en este codigo, por favor ayudarme

Mi problema es el siguiente, tengo un codigo que me almacena todo tipos de archivos (jpg,pdf..) en la base de datos, el tema es que cuando el usuario registra el documento o la foto todo transcurre sin problema, el tema lo tengo cuando yo quiero ver ese documento, pense que desde el hosting se podia extraer pero es imposible, y tengo este codigo pero creo que hay algo mal.

A VER SI ALGUIEN ME PUEDE ECHAR UNA MANO

<?php


//CONSTRUIMOS EL QUERY PARA OBTENER LOS ARCHIVOS
$qry="select
docs.*,
CASE docs.tipo
WHEN 'image/png' then
'image'
WHEN 'image/jpg' then
'image'
WHEN 'image/gif' then
'image'
WHEN 'image/jpeg' then
'image'
ELSE
'file'
END as display
from tbl_documentos AS docs";
//EJECUTAMOS LA CONSULTA
$res=mysql_query($qry) or die("Query: $qry ".mysql_error());
//RECORREMOS LA CONSULTA
//*********NOTA DONDE DICE alt='$obj-/>titulo' QUITA LA BARRA PARA QUE QUEDE ASI:
// alt='$obj->titulo'
// EL WORDPRESS ME ESTA REMPLAZANDO EL CODIGO
while ($obj=mysql_fetch_object($res)) {
//SI EL TIPO DE DOCUMENTO ES UMAGEN LA MOSTRAMOS SI NO SOLO HACEMOS EL LINK
switch ($obj->display){
case "image":
echo "<div>
<a href='getfile.php?id_documento={$obj->id_documento}'>
<img alt='$obj->titulo' src='getfile.php?id_documento={$obj->id_documento}'/>
</a>
</div><hr />";
break;
case "file":
echo "<div>
<a href='getfile.php?id_documento={$obj->id_documento}'>$obj->titulo</a>
</div><hr />";
break;
}
}

//CERRAMOS LA CONEXION
mysql_close();
?>
  #2 (permalink)  
Antiguo 10/05/2012, 14:07
 
Fecha de Ingreso: diciembre-2010
Mensajes: 788
Antigüedad: 13 años, 4 meses
Puntos: 51
Respuesta: Tengo algo mal en este codigo, por favor ayudarme

El contenido del archivo tenes que volcarlo en un php exclusivo, con sus respectivas cabeceras.

Por lo que supongo que el problema esta en getfile.php. Postea el codigo de ese archivo, sino es imposible ayudarte...
  #3 (permalink)  
Antiguo 10/05/2012, 14:14
 
Fecha de Ingreso: mayo-2012
Mensajes: 5
Antigüedad: 11 años, 11 meses
Puntos: 0
Respuesta: Tengo algo mal en este codigo, por favor ayudarme

Este es el getfile...


//CONSTRUIMOS LA CONSULTA PARA OBTENER EL DOCUMENTO
<?php
//CONSTRUIMOS LA CONSULTA PARA OBTENER EL DOCUMENTO
$qry="Select * from tbl_documentos where id_documento='{$_REQUEST['id_documento']}'";$res=mysql_query($qry) or die(mysql_error()." qry::$qry");
$obj=mysql_fetch_object($res);
//OBTENEMOS EL TIPO MIME DEL ARCHIVO ASI EL NAVEGADOR SABRA DE QUE SE TRATA
header("Content-type: {$obj->tipo}");
//OBTENEMOS EL NOMBRE DEL ARCHIVO POR SI LO QUE SE REQUIERE ES DESCARGARLO
header('Content-Disposition: attachment; filename="'.$obj->nombre_archivo.'"');
//Y PO ULTIMO SIMPLEMENTE IMPRIMIMOS EL CONTENIDO DEL ARCHIVO
print $obj->contenido;
//CERRAMOS LA CONEXION
mysql_close();
?>
  #4 (permalink)  
Antiguo 10/05/2012, 14:16
 
Fecha de Ingreso: mayo-2012
Mensajes: 5
Antigüedad: 11 años, 11 meses
Puntos: 0
Respuesta: Tengo algo mal en este codigo, por favor ayudarme

Basicamente la pregunta que tengo es que si tengo archivo pdf en mysql, como los puedo ver o extraer, existe algun codigo? porque desde mi hosting no me deja. Y el codigo este visualiza fotos pero no pdf
  #5 (permalink)  
Antiguo 10/05/2012, 14:26
 
Fecha de Ingreso: diciembre-2010
Mensajes: 788
Antigüedad: 13 años, 4 meses
Puntos: 51
Respuesta: Tengo algo mal en este codigo, por favor ayudarme

Todos los archivos pueden tener diferentes cabeceras.

Para el pdf deberias usar esto:

application/octet-stream y no "file"

Ademas, deberias agregar la cabecera para tamaño de archivos:

Código PHP:
Ver original
  1. header ("Content-Length: ".strlen($obj->contenido));
  #6 (permalink)  
Antiguo 11/05/2012, 06:00
 
Fecha de Ingreso: mayo-2012
Mensajes: 5
Antigüedad: 11 años, 11 meses
Puntos: 0
Respuesta: Tengo algo mal en este codigo, por favor ayudarme

Perdona mi ignorancia, pero es que con lo que me propusistes sigo sin conseguir extraer el pdf de la base datos.

Podrias indicarme exactamente los cambios?

list


<?php
//NOS CONECAMOS A LA BASE DE DATOS
//REMPLAZEN SUS VALOS POR LOS MIOS

//CONSTRUIMOS EL QUERY PARA OBTENER LOS ARCHIVOS
$qry="select
docs.*,
CASE docs.tipo
WHEN 'image/png' then
'image'
WHEN 'image/jpg' then
'image'
WHEN 'image/gif' then
'image'
WHEN 'image/jpeg' then
'image'
ELSE
'application/octet-stream'
END as display
from tbl_documentos AS docs";
//EJECUTAMOS LA CONSULTA
$res=mysql_query($qry) or die("Query: $qry ".mysql_error());
//RECORREMOS LA CONSULTA
//*********NOTA DONDE DICE alt='$obj-/>titulo' QUITA LA BARRA PARA QUE QUEDE ASI:
// alt='$obj->titulo'
// EL WORDPRESS ME ESTA REMPLAZANDO EL CODIGO
while ($obj=mysql_fetch_object($res)) {
//SI EL TIPO DE DOCUMENTO ES UMAGEN LA MOSTRAMOS SI NO SOLO HACEMOS EL LINK
switch ($obj->display){
case "image":
echo "<div>
<a href='getfile.php?id_documento={$obj->id_documento}'>
<img alt='$obj->titulo' src='getfile.php?id_documento={$obj->id_documento}'/>
</a>
</div><hr />";
break;
case "application/octet-stream":
echo "<div>
<a href='getfile.php?id_documento={$obj->id_documento}'>$obj->titulo</a>
</div><hr />";
break;
}
}

//CERRAMOS LA CONEXION
mysql_close();
?>

getfile

<?php
//CONSTRUIMOS LA CONSULTA PARA OBTENER EL DOCUMENTO

//CONSTRUIMOS LA CONSULTA PARA OBTENER EL DOCUMENTO
$qry="Select * from tbl_documentos where id_documento='{$_REQUEST['id_documento']}'";$res=mysql_query($qry) or die(mysql_error()." qry::$qry");
$obj=mysql_fetch_object($res);
//OBTENEMOS EL TIPO MIME DEL ARCHIVO ASI EL NAVEGADOR SABRA DE QUE SE TRATA
header("Content-type: {$obj->tipo}");
//OBTENEMOS EL NOMBRE DEL ARCHIVO POR SI LO QUE SE REQUIERE ES DESCARGARLO
header ("Content-Length: ".strlen($obj->contenido));
//Y PO ULTIMO SIMPLEMENTE IMPRIMIMOS EL CONTENIDO DEL ARCHIVO
print $obj->contenido;
//CERRAMOS LA CONEXION
mysql_close();
?>

Esto es lo que yo hice pero no funciona y por ciertos mil gracias por constestar

Etiquetas: favor, mysql, sql, usuarios
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 15:40.