Ver Mensaje Individual
  #1 (permalink)  
Antiguo 12/01/2008, 11:02
gesking
 
Fecha de Ingreso: diciembre-2006
Mensajes: 294
Antigüedad: 17 años, 4 meses
Puntos: 2
Usuarios + Img + MySQL

Hola, estoy creando un album de fotos, donde los usuarios se puede registrar, loguear y subir sus fotos. Todo está perfecto, las imagenes a subir se edita el nombre automáticamente con el md5, pero que tenga adelante el nombre del usuario. Por ejemplo:

nombre del usuario: pepe
nombre de la imagen generada: 0002e2a20f.jpg
lograr esto: pepe_0002e2a20f.jpg

Además que el nombre de la nueva imagen se registre en una bd que cree especialmente para almacenar el nombre de las imagenes... Entonces, cuando veo que una foto no cumple con las normas elimine el nombre de la bd y que se borre la imagen...

Tengo estos codigos generados, pero no se como agregar lo que quiero, se que no falta mucho, pero falta :S


subir_imagen.php
Código PHP:
session_start();
include(
"config_usuarios.php");

mysql_connect ($dbhost$dbusername$dbuserpass);
mysql_select_db($dbname) or die('Cannot select database');

$con mysql_query("SELECT * FROM hostimage_usuarios") or die("Error: ".mysql_error());
$mostrar mysql_fetch_array($con);

if (isset(
$_SESSION['s_username'])) {

echo 
'<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="81%"><table width="500" border="0" cellspacing="0" cellpadding="5">
<tr>
<td>
<div align="center">
<form action="index.php" method="post" enctype="multipart/form-data" name="form1">
<span class="Estilo2">Archivo</span>
<br>
<input name="archivo" type="file" id="archivo" class="file">
<br><br>
<input name="boton" id="boton" class="boton" type="submit" value="Subir Imagen!">
</form>
</div>
</td>
</tr>
<tr><td style="border: 1px solid #0099FF; background-color: #88CFFF">
<b>Solo se admiten archivo con formato JPG, GIF y PNG.</b>
<br>
Tamaño máximo aceptado:<span style="color:#FF0000"> 1MB</span>.</td>
</tr>
</table></td>
    <td width="19%">
<h3>Bienvenido  '
.$mostrar[username].'!</h3>
<b>></b>    <a href="subir_imagenes.php">Subir imágenes</a>
<br>
<b>></b>    <a href="album.php">Ver album</a>
<br>
<b>></b>    <a href="eliminar_imagenes.php">Eliminar imágenes</a>
<br>
<b>></b>    <a href="perfil.php">Perfil</a>
<br>
<b>></b>    <a href="logout.php">Salir</a>
   </td>
  </tr>
</table>
<br><br>'
;

if(isset(
$_POST['boton'])){

$tipo $_FILES['archivo']['type'];
$tamaño $_FILES['archivo']['size'];

if (
is_uploaded_file($HTTP_POST_FILES['archivo']['tmp_name'])) {

if(
$HTTP_POST_FILES['archivo']['size'] < 1000000) {

if(
$_FILES['archivo']['type']=="image/gif" || $_FILES['archivo']['type']=="image/jpeg" || $_FILES['archivo']['type']=="image/png") {

$info pathinfo($_FILES['archivo']['name']);
$imagen substr(md5(uniqid(rand())),0,10).".".strtolower($info['extension']);
copy($_FILES['archivo']['tmp_name'], $imagen);

$query "INSERT INTO hostimage_imagenes (username, imagen) VALUES('$imagen', '$mostrar[username]')";
mysql_query($query) or die(mysql_error());

$subio true;
}
}
}

   if(
$subio) {
      echo 
'<table width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td>
<div align="center">
<img src="'
.$imagen.'">
</div>
</td>
</tr>
<tr>
<td>&nbsp;
</td>
</tr>
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%">
<table width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
</tr>
<tr>
<td>
<div align="center">
<b>URL</b>
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
<textarea name="textarea4" cols="50" rows="2" class="file">http://www.host-image.com.ar/'
.$imagen.'</textarea>
</div>
</td>
</tr>
<tr>
<td>&nbsp;
</td>
</tr>
<tr>
<td>
<div align="center">
<b>C&oacute;digo para foros</b>
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
<textarea name="textarea4" cols="50" rows="2" class="file">[url=http://www.host-image.com.ar][img=http://www.host-image.com.ar/'
.$imagen.'][/url]</textarea>
</div>
</td>
</tr>
<tr>
<td>&nbsp;
</td>
</tr>
<tr>
<td>
<div align="center">
<b>C&oacute;digo HTML</b>
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
<textarea name="textarea4" cols="50" rows="2" class="file"><a href="http://www.host-image.com.ar"><img src="http://www.host-image.com.ar/'
.$imagen.'" border="0" /></a></textarea>
</div>
</td>
</tr>
</table>
</td>
<td width="50%" valign="top">
<br><br>
   <b>Nombre:</b>    <a href="'
.$imagen.'" target="_blank">'.$imagen.'</a>
<br><br>
   <b>Tipo:</b>    "'
.$tipo.'"
<br><br>
   <b>Tamaño:</b>    <span style="color:#FF0000">"'
.$tamaño.'"</span> bytes
</td>
</tr>
</table>
</td>
</tr>
</table>'
;
   } else {
      echo 
"El archivo no se pudo enviar. Recuerda que debe ser un archivo JPG, GIF o PNG y el tamaño no puede exceder 1MB";
   }

}else{
echo 
"Tu no estas autentificado. No puedes entrar aqui!!";
echo 
$_SESSION['s_username'];


eliminar_imagenes.php
Código PHP:
<?
$archivo 
$_POST['archivo'];
if(isset(
$_POST['borrar'])){
if(
unlink($archivo)) {
echo 
"El archivo <b>$archivo</b> ha sido borrado satisfactoriamente.<br><br><br><br>";
} else {
echo 
"El archivo <b>$archivo</b> no se pudo borrar.<br><br><br><br>";
}
}
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="30%" valign="top">
<form method="post" action="eliminar.php" enctype="multipart/form-data" name="formulario">
  <div align="center">
  <select name="archivo" size="15" class="form" id="archivo" onChange="abrir_imagen(this)">
    <?
$path 
".";
$dir opendir($path);
$element_list = array();
while (
$elemento readdir($dir))
{
    
$element strtolower($elemento);
    if (
$element != '.' && $element != '..' && strpos($element'.php') === false) {
        
$element_list[$element] = filemtime($path '/' $elemento);
    }
}
arsort($element_list);
foreach (
array_keys($element_list) as $elemento)
{
    echo 
"<option value='$elemento'>$elemento</option>\r\n";
}
closedir($dir); ?>
  </select>
  <br>
  <br>
  <input name="borrar" type="submit" class="form" id="borrar" value="Borrar">
  </div>
</form>
</td>
    <td width="70%" valign="top"><div align="center">
<script type="text/javascript">
function abrir_imagen(obj) {
    document.getElementById('imagen_grande').src = obj.value;
}
</script>
<img src="" id="imagen_grande">
<script type="text/javascript">
    abrir_imagen(document.getElementById('archivo'));
</script>
    </div></td>
  </tr>
</table>

Este ultimo de eliminar las imágenes no lo perfeccioné todavía... falta bloquearlo para que los usuarios no loguados no puedan entrar..