
11/03/2008, 09:24
|
| | Fecha de Ingreso: octubre-2005 Ubicación: Banyoles
Mensajes: 22
Antigüedad: 19 años, 5 meses Puntos: 0 | |
Re: Visualizar imagen de una base de datos Si, tienes razón.
No es un script desarrollado por mi, lo encontré en un site, pero es de libre utililización y lo quiero adaptar a mis necesidades...
Aquí va:
<?php
$db_host = 'localhost'; // don't forget to change
$db_user = 'jacovi88';
$db_pwd = '11068jcv';
$database = 'test';
$table = 'ae_gallery';
// use the same name as SQL table
// Usar el mismo nombre que el cuadro de SQL
$password = '123';
// simple upload restriction,
// to disallow uploading to everyone
// Simple filtro de restricción de carga,
// Para no permitir la subida de imágenes a todo el mundo
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// This function makes usage of
// $_GET, $_POST, etc... variables
// completly safe in SQL queries
//Esta función hace uso de
//$ _GET, $ _POST, Etc .. Variables
//completamente seguro en consultas SQL
function sql_safe($s)
{
if (get_magic_quotes_gpc())
$s = stripslashes($s);
return mysql_real_escape_string($s);
}
// If user pressed submit in one of the forms
//Si el usuario presiona 'Enviar' en uno de los formularios
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// cleaning title field
//Limpia el campo 'title'
$title = trim(sql_safe($_POST['title']));
if ($title == '') // if title is not set- Si el 'title' no está definido
$title = '(empty title)';// use (empty title) string- Usa 'empty title'
if ($_POST['password'] != $password) // cheking passwors- Chequea password
$msg = 'Error: wrong upload password';
else
{
if (isset($_FILES['photo']))
{
@list(, , $imtype, ) = getimagesize($_FILES['photo']['tmp_name']);
// Get image type.
// We use @ to omit errors
//Obtener un tipo de imagen.
//Utilizamos @ para omitir errores
if ($imtype == 3) // cheking image type- Chequea el tipo de imagen.
$ext="png"; // to use it later in HTTP headers- Usarlo más tarde en cabeceras HTTP
elseif ($imtype == 2)
$ext="jpeg";
elseif ($imtype == 1)
$ext="gif";
else
$msg = 'Error: unknown file format';
if (!isset($msg)) // If there was no error- Si no hay error
{
$data = file_get_contents($_FILES['photo']['tmp_name']);
$data = mysql_real_escape_string($data);
// Preparing data to be used in MySQL query- Preparación de los datos que se utilizarán en la consulta MySQL
mysql_query("INSERT INTO {$table}
SET ext='$ext', title='$title',
data='$data'");
$msg = 'Success: image uploaded';
}
}
elseif (isset($_GET['title'])) // isset(..title) needed
$msg = 'Error: file not loaded';// to make sure we've using
// upload form, not form
// for deletion
//isset (título ..) necesarios
//para asegurarse de que hemos utilizando
//formulario de subida, no forma
//de eliminación
if (isset($_POST['del'])) // If used selected some photo to delete
{ // in 'uploaded images form';
//Si se utilizan algunas fotos seleccionadas que se suprima
//en el 'formulario de subida de imágenes'
$id = intval($_POST['del']);
mysql_query("DELETE FROM {$table} WHERE id=$id");
$msg = 'Photo deleted';
}
}
}
elseif (isset($_GET['show']))
{
$id = intval($_GET['show']);
$result = mysql_query("SELECT ext, UNIX_TIMESTAMP(image_time), data
FROM {$table}
WHERE id=$id LIMIT 1");
if (mysql_num_rows($result) == 0)
die('no image');
list($ext, $image_time, $data) = mysql_fetch_row($result);
$send_304 = false;
if (php_sapi_name() == 'apache') {
// if our web server is apache
// we get check HTTP
// If-Modified-Since header
// and do not send image
// if there is a cached version
// Si nuestro servidor web es Apache
// Obtenemos comprobar HTTP
// Si se modificó desde la cabecera
// y no envíó la imagen
// Si existe una versión en caché
$ar = apache_request_headers();
if (isset($ar['If-Modified-Since']) && // If-Modified-Since should exists
($ar['If-Modified-Since'] != '') && // not empty
(strtotime($ar['If-Modified-Since']) >= $image_time)) // and grater than
$send_304 = true; // image_time
}
if ($send_304)
{
// Sending 304 response to browser
// "Browser, your cached version of image is OK
// we're not sending anything new to you"
// Enviar respuesta 304 al navegador
// "El archivo, su versión en caché de la imagen está bien
// No estamos enviando algo nuevo para usted "
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $ts).' GMT', true, 304);
exit(); // bye-bye- Adiós.
}
// outputing Last-Modified header- Salida última modificación cabecera
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $image_time).' GMT',
true, 200);
// Set expiration time +1 year
// We do not have any photo re-uploading
// so, browser may cache this photo for quite a long time
//Establecer tiempo de caducidad +1 años
// No tenemos ninguna foto para volver a cargar
// Así, navegador tiene en caché esta foto durante un largo tiempo
header('Expires: '.gmdate('D, d M Y H:i:s', $image_time + 86400*365).' GMT',
true, 200);
// outputing HTTP headers- Cabeceras HTTP de salida
header('Content-Length: '.strlen($data));
header("Content-type: image/{$ext}");
// outputing image- Imagen de salida
echo $data;
echo "Hola Mundo";
exit();
}
?>
<html><head>
<title>MySQL Blob Image Gallery Example</title>
</head>
<body>
<?php
if (isset($msg)) // this is special section for
// outputing message
//Sección especial para mensaje de salida
{
?>
<p style="font-weight: bold;"><?=$msg?>
<br>
<a href="<?=$PHP_SELF?>">recargar página</a>
<!-- I've added reloading link, because
refreshing POST queries is not good idea
He añadido vínculo de recarga, porque refrescar
las consutas con POST no es buena idea -->
</p>
<?php
}
?>
<h1>Galería de imágenes en campo blob.</h1>
<h2>Imágenes cargadas en la base de datos:</h2>
<form action="<?=$PHP_SELF?>" method="post">
<!-- This form is used for image deletion
Este form se utiliza para borrar las imágenes-->
<?php
$result = mysql_query("SELECT id, image_time, title FROM {$table} ORDER BY id DESC");
if (mysql_num_rows($result) == 0) // table is empty
echo '<ul><li>No images loaded</li></ul>';
else
{
echo '<ul>';
while(list($id, $image_time, $title) = mysql_fetch_row($result))
{
// outputing list- Listado de salida
echo "<li><input type='radio' name='del' value='{$id}'>";
echo "<a href='{$PHP_SELF}?show={$id}'>{$title}</a> –";
echo "<small>{$image_time}</small></li>";
}
echo '</ul>';
echo '<label for="password">Password:</label><br>';
echo '<input type="password" name="password" id="password"><br><br>';
echo '<input type="submit" value="Delete selected">';
}
?>
</form>
<h2>Subir una imagen:</h2>
<form action="<?=$PHP_SELF?>" method="POST" enctype="multipart/form-data">
<label for="title">Descripción:</label><br>
<input type="text" name="title" id="title" size="64"><br><br>
<label for="photo">Imagen:</label><br>
<input type="file" name="photo" id="photo"><br><br>
<label for="password">Password:</label><br>
<input type="password" name="password" id="password"><br><br>
<input type="submit" value="upload">
</form>
</body>
</html> |