Ver Mensaje Individual
  #3 (permalink)  
Antiguo 14/12/2010, 14:42
rafaxusirati
 
Fecha de Ingreso: julio-2010
Ubicación: Cerca Sitges (Barcelona)
Mensajes: 98
Antigüedad: 14 años, 7 meses
Puntos: 4
Respuesta: Evitar duplicidad en archivos subidos al servidor

Cita:
Iniciado por rafaxusirati Ver Mensaje
Hola buenos días.
Me interesaría introducir un número aleatorio para evitar duplicidad en archivos subidos al servidor.
¿Dónde tengo que colocarlo? ""substr(md5(uniqid(rand())),0,6)""
Intento con este, pero puede ser otro.

Muchas gracias.

Código PHP:
Ver original
  1. <?php require_once('../../Connections/contactos.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6.   if (PHP_VERSION < 6) {
  7.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8.   }
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. mysql_select_db($database_contactos, $contactos);
  35. $query_Recordset1 = "SELECT * FROM contactos";
  36. $Recordset1 = mysql_query($query_Recordset1, $contactos) or die(mysql_error());
  37. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  38. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  39. ?>
  40. .......
  41. <?
  42. $db=mysql_connect("localhost","????","????",$db);
  43.     mysql_select_db("????",$db);
  44.  
  45. $nombre=$_POST[nombre];
  46.     $apellido=$_POST[apellido];
  47.     $ciudad=$_POST[ciudad];
  48.     $provincia=$_POST[provincia];
  49.     $web=$_POST[web];
  50.     $tituloanuncio=$_POST[tituloanuncio];
  51.     $precio=$_POST[precio];
  52.     $foto1 = $_FILES['foto1']['name'];
  53.    
  54.    // "directorio" es el directorio donde vas a subir la imagen.
  55.     mysql_query("insert into contactos(foto1 , nombre , apellido , ciudad , provincia , web , tituloanuncio , precio)
  56. values('/imagenes/$foto1','$nombre','$apellido','$ciudad','$provincia','$web','$tituloanuncio','$precio')",$db) or die("La clave introducida, ya existe. Elige otra");
  57.  
  58. //ahora subamos la imagen, "directorio" es el directorio donde la vas a subir.
  59. $copy = copy($_FILES['foto1']['tmp_name'], "../../imagenes/" . $_FILES['foto1']['name']);
  60. ?>&nbsp;</h1>
  61.     <!-- end #mainContent --></div>
  62.     <!-- Este elemento de eliminación siempre debe ir inmediatamente después del div #mainContent para forzar al div #container a que contenga todos los elementos flotantes hijos --><br class="clearfloat" />
  63.   <div id="footer">
  64.     <p>Pie</p>
  65.   <!-- end #footer --></div>
  66. <!-- end #container --></div>
  67. </body>
  68. </html>
  69. <?php
  70. mysql_free_result($Recordset1);
  71. ?>
Cita:
Iniciado por areslepra Ver Mensaje
Algo así debería quedar, lo pruebas y nos comentas que tal.

Código PHP:
$uniqueID substr(md5(uniqid(rand())),0,6);
$foto1 $uniqueID.$_FILES['foto1']['name'];
   
// "directorio" es el directorio donde vas a subir la imagen.
mysql_query("insert into contactos(foto1 , nombre , apellido , ciudad , provincia , web , tituloanuncio , precio)
values('/imagenes/$foto1','$nombre','$apellido','$ciudad','$provincia','$web','$tituloanuncio','$precio')"
,$db) or die("La clave introducida, ya existe. Elige otra");
 
//ahora subamos la imagen, "directorio" es el directorio donde la vas a subir.
$copy copy($_FILES['foto1']['tmp_name'], "../../imagenes/" $uniqueID.$_FILES['foto1']['name']); 
Muchas gracias, funciona correctamente