Estoy siguiendo un script para subir distintos archivos y queden almacenados en una BD y luego listarlos para su descarga.
El tema es que agregue la opción de listar también la descripción del archivo, pero estos salen lineales y no muestran los saltos de linea. Por ahí me dijieron que se utiliza el nl2br, pero como esta diseñado el script me complica.
La BD
Código SQL:
Ver original
CREATE TABLE tbl_documentos ( id_documento INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, titulo VARCHAR(50) NULL, descripcion MEDIUMTEXT NULL, contenido LONGBLOB NULL, tamanio INTEGER UNSIGNED NULL, tipo VARCHAR(50) NULL, nombre_archivo VARCHAR(55) NULL, tamanio_unidad VARCHAR(50) NULL, PRIMARY KEY(id_documento) );
form.html
Código PHP:
Ver original
<form id="test_upload" name="test_upload" action="upload.php" enctype="multipart/form-data" method="post"> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td> Titulo </td> <td> <input type="text" id="titulo" name="titulo"/> </td> </tr> <tr> <td colspan="2"> Descripcion </td> </tr> <tr> <td colspan="2"> <textarea id="descripcion" name="descripcion" cols="50" rows="5"></textarea> </td> </tr> <tr> <td colspan="2"> Archivo <input type="file" id="archivo" name="archivo"/> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="Registrar Documento"/> </td> </tr> </table> </form>
upload.php
Código PHP:
Ver original
<?php //ESTA FUNCION LA USAREMOS PARA OBTENER EL TAMAÑO DE NUESTRO ARCHIVO function filesize_format($bytes, $format = '', $force = ''){ $bytes=(float)$bytes; if ($bytes <1024){ } if ($bytes <1048576){ } if ($bytes>= 1048576){ } } //VERIFICAMOS QUE SE SELECCIONO ALGUN ARCHIVO echo "No se puede subir el archivo"; } // EN ESTA VARIABLE ALMACENAMOS EL NOMBRE TEMPORAL QU SE LE ASIGNO ESTE NOMBRE ES GENERADO POR EL SERVIDOR // ASI QUE SI NUESTRO ARCHIVO SE LLAMA foto.jpg el tmp_name no sera foto.jpg sino un nombre como SI12349712983.tmp por decir un ejemplo $archivo = $_FILES["archivo"]["tmp_name"]; //Definimos un array para almacenar el tamaño del archivo //OBTENEMOS EL TAMAÑO DEL ARCHIVO $tamanio = $_FILES["archivo"]["size"]; //OBTENEMOS EL TIPO MIME DEL ARCHIVO $tipo = $_FILES["archivo"]["type"]; //OBTENEMOS EL NOMBRE REAL DEL ARCHIVO AQUI SI SERIA foto.jpg $nombre_archivo = $_FILES["archivo"]["name"]; //PARA HACERNOS LA VIDA MAS FACIL EXTRAEMOS LOS DATOS DEL REQUEST //VERIFICAMOS DE NUEVO QUE SE SELECCIONO ALGUN ARCHIVO if ( $archivo != "none" ){ //ABRIMOS EL ARCHIVO EN MODO SOLO LECTURA // VERIFICAMOS EL TAÑANO DEL ARCHIVO //LEEMOS EL CONTENIDO DEL ARCHIVO //CON LA FUNCION addslashes AGREGAMOS UN \ A CADA COMILLA SIMPLE ' PORQUE DE OTRA MANERA //NOS MARCARIA ERROR A LA HORA DE REALIZAR EL INSERT EN NUESTRA TABLA //CERRAMOS EL ARCHIVO // VERIFICAMOS EL TAÑANO DEL ARCHIVO if ($tamanio <1048576){ //HACEMOS LA CONVERSION PARA PODER GUARDAR SI EL TAMAÑO ESTA EN b ó MB $tamanio=filesize_format($tamanio); } //CREAMOS NUESTRO INSERT $qry = "INSERT INTO tbl_documentos ( titulo,nombre_archivo, descripcion, contenido, tamanio,tamanio_unidad, tipo ) VALUES ('$titulo','$nombre_archivo', '$descripcion','$contenido','{$tamanio[0]}','{$tamanio[1]}', '$tipo')"; //NOS CONECAMOS A LA BASE DE DATOS //REMPLAZEN SUS VALOS POR LOS MIOS //SELECCIONAMOS LA BASE DE DATOS CON LA CUAL VAMOS A TRABAJAR CAMBIEN EL VALOR POR LA SUYA //EJECUTAMOS LA CONSULTA //CERRAMOS LA CONEXION //NOTIFICAMOS AL USUARIO QUE EL ARCHVO SE HA ENVIADO O REDIRIGIMOS A OTRO LADO ETC. echo "Archivo Agregado Correctamente<br>"; echo '<a href="form.html">Subir Otro Archivo</a><br > '; }else{ echo "No fue posible subir el archivo"; echo '<a href="form.html">Subir Otro Archivo</a><br > '; } ?>
getfile.php
Código PHP:
Ver original
<?php //NOS CONECAMOS A LA BASE DE DATOS //REMPLAZEN SUS VALOS POR LOS MIOS //SELECCIONAMOS LA BASE DE DATOS CON LA CUAL VAMOS A TRABAJAR CAMBIEN EL VALOR POR LA SUYA //CONSTRUIMOS LA CONSULTA PARA OBTENER EL DOCUMENTO $qry="Select * from tbl_documentos where id_documento={$_REQUEST['id_documento']}"; //OBTENEMOS EL TIPO MIME DEL ARCHIVO ASI EL NAVEGADOR SABRA DE QUE SE TRATA //OBTENEMOS EL NOMBRE DEL ARCHIVO POR SI LO QUE SE REQUIERE ES DESCARGARLO //Y PO ULTIMO SIMPLEMENTE IMPRIMIMOS EL CONTENIDO DEL ARCHIVO print $obj->contenido; //CERRAMOS LA CONEXION ?>
list.php
Código PHP:
Ver original
<?php //NOS CONECAMOS A LA BASE DE DATOS //REMPLAZEN SUS VALOS POR LOS MIOS //SELECCIONAMOS LA BASE DE DATOS CON LA CUAL VAMOS A TRABAJAR CAMBIEN EL VALOR POR LA SUYA //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"; $prueba="select descripcion from tbl_documentos"; //EJECUTAMOS LA CONSULTA //RECORREMOS LA CONSULTA //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 src='getfile.php?id_documento={$obj->id_documento}' alt='$obj->titulo' /> </a> </div><hr />"; break; case "file": echo "<div> <a href='getfile.php?id_documento={$obj->id_documento}'>$obj->titulo</a> <br>$obj->descripcion<br> </div><hr />"; break; } } //CERRAMOS LA CONEXION ?>
En esta ultima $obj->descripcion fue la única forma de poder llamar la descripcion de los archivos subidos.
Ojala y me ayuden se los agradeceria mucho :D