Ver Mensaje Individual
  #1 (permalink)  
Antiguo 31/08/2011, 11:17
rafaxusirati
 
Fecha de Ingreso: julio-2010
Ubicación: Cerca Sitges (Barcelona)
Mensajes: 98
Antigüedad: 13 años, 9 meses
Puntos: 4
¿cómo utilizar $tamaño_max="500000"; // Tamaño maximo permitido?

Hola a todos. Tengo los siguientes formularios:

1º : Se encarga de recoger los datos de la persona interesada, Nombre... y foto.
2º : Se encarga de validar el primer formulario.
3º : Me interesaría añadir este código u otro parecido al segundo formulario, para poder limitar el peso de la foto subida al servidor.

¿Existe algún formulario que se encargue de reducir el tamaño de la foto adjuntada?
¿Algún código mejorado al propuesto?
Gracias.


Formulario 1º
Código PHP:
Ver original
  1. //...
  2. </div>
  3. <div id="mainContent">
  4.     <h1><form action="BORRAR1f_ok.php" method="post" enctype="multipart/form-data" name="procedim" onSubmit="MM_validateForm('nombre_usuario','','R','password','','R','nombre','','R','mailcontacto','','R','ciudad','','R','tituloanuncio','','R','descripcionanuncio','','R');return document.MM_returnValue" ondblclick="Submit_seguro(this)">
  5.            
  6.      
  7.         <input name = "nombre_usuario" type = "text" id="nombre_usuario" />
  8.       </div></td>
  9.      
  10.     </tr>
  11.     <tr>
  12.       <td><div align="right"><span style="font-size: small; color: #F00;">Password:</span></div></td>
  13.       <td><div align="left">
  14.         <input name = "password" type = "password" id="password" />
  15.       </div></td>
  16.     </tr>
  17.     <tr>
  18.       <td><div align="right"><span style="font-size: small; color: #F00;">Nombre:</span></div></td>
  19.       <td><div align="left">
  20.         <input name = "nombre" type = "text" id="nombre" />
  21.  //...
  22.  
  23.     <tr>
  24.       <td height="0" colspan="2"><div align="center">
  25.         <div align="center">
  26.       <div align="center">      
  27.     <div align="center"><form name="form1" method="post" action="">
  28. <div align="center">
  29.   <table width="51%" border="0">
  30.     <tr>
  31.       <td><div align="left">
  32.         <input type="file" name="foto1" />
  33.       </div></td>
  34.       </tr>
  35.     <tr>
  36.       <td>&nbsp;</td>
  37.       </tr>
  38.   </table>
  39. </div>
  40. <div align="left"></div>
  41.       </form>
  42.       &nbsp;
  43.       </p></div>
  44. </div>
  45. </div></td>
  46.       </tr>
  47.     <tr>
  48.       <td height="26" colspan="2"><div align="center">
  49.         <input type = "submit" name = "boton" value = "Enviar" />
  50.        
  51.          
  52.       </div></td>
  53.     </tr>
  54.     </table>
  55. <br>
  56.        
  57.       </form>
  58.     </h1>
  59.     &nbsp;
  60.     <!-- end #mainContent -->
  61. </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 -->
  63.  <br class="clearfloat" />
  64.   <!-- end #container -->
  65. </div>
  66. </body>
  67. </html>

Formulario 2º
Código PHP:
Ver original
  1. <?php
  2. header ("Location: http://../spain/ok.php");
  3. ?>
  4. <?php require_once('../../Connections/contactos.php'); ?>
  5. <?php
  6. if (!function_exists("GetSQLValueString")) {
  7. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  8. {
  9.   if (PHP_VERSION < 6) {
  10.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  11.   }
  12.  
  13.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  14.  
  15.   switch ($theType) {
  16.     case "text":
  17.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  18.       break;    
  19.     case "long":
  20.     case "int":
  21.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  22.       break;
  23.     case "double":
  24.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  25.       break;
  26.     case "date":
  27.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  28.       break;
  29.     case "defined":
  30.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  31.       break;
  32.   }
  33.   return $theValue;
  34. }
  35. }
  36.  
  37. mysql_select_db($database_contactos, $contactos);
  38. $query_Recordset1 = "SELECT * FROM contactos";
  39. $Recordset1 = mysql_query($query_Recordset1, $contactos) or die(mysql_error());
  40. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  41. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  42. ?>
  43. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  44. //...
  45.  
  46.  
  47. <?
  48. $db=mysql_connect("...","...","...",$db);
  49.     mysql_select_db("...",$db);
  50.    
  51.    
  52. //...
  53. $nombre=$_POST[nombre];
  54.  
  55.     $foto1 = $_FILES['foto1']['name'];
  56.     $prefijo = substr(md5(uniqid(rand())),0,10);
  57.  
  58.  
  59. $uniqueID = substr(md5(uniqid(rand())),0,10);
  60. $foto1=$uniqueID.".".substr($foto1,-3);
  61.  
  62.  
  63. // "directorio" es el directorio donde vas a subir la imagen.
  64. mysql_query("insert into contactos( foto1 , nombre )
  65. values('/imagenes/$foto1','$nombre_usuario','$password'...)",$db) or die("La clave introducida, ya existe. Elige otra");  
  66.  
  67. //ahora subamos la imagen, "directorio" es el directorio donde la vas a subir.
  68. $copy = copy($_FILES['foto1']['tmp_name'], "../../imagenes/".$foto1);
  69.  
  70.  
  71. ?>&nbsp;
  72.  
  73. //..
  74. <?php
  75. mysql_free_result($Recordset1);
  76. ?>

3º Código que quiero añadir al anterior formulario, sería posible, ¿Cómo quedaría el formulario 2º?
Código PHP:
Ver original
  1. $tamano = $_FILES [ 'file' ][ 'size' ]; // Leemos el tamaño del fichero
  2. $tamaño_max="500000"; // Tamaño maximo permitido
  3. if( $tamano < $tamaño_max){ // Comprovamos el tamaño
  4.     $destino = '../images/' ; // Carpeta donde se guardata
  5.     $sep=explode('image/',$_FILES["file"]["type"]); // Separamos image/
  6.     $tipo=$sep[1]; // Optenemos el tipo de imagen que es
  7.     if($tipo == "gif" || $tipo == "jpeg" || $tipo == "jpg" || $tipo == "png"){ // Si el tipo de imagen a subir es el mismo de los permitidos, segimos. Puedes agregar mas tipos de imagen
  8.         move_uploaded_file ( $_FILES [ 'file' ][ 'tmp_name' ], $destino . '/'.$cad.'.'.$tipo);  // Subimos el archivo
  9.             $cont++;
  10.             $ruta = $cad.'.'.$tipo; //nombre completo de la imagen para almacenarlo en la base de datos
  11.             $query = mysqli_query($conexion, "INSERT INTO asodepa.fotos (ruta, categoria, fecha)
  12.            VALUES('$ruta','$categoria',now())") or die (mysqli_error($conexion));
  13.  
  14.         echo 'Fotografía Cargada Exitosamente. <br /><br /><a href="imagen.php">haz click aqui</a>
  15.            ';
  16. }
  17. else echo 'El Tipo de Archivo no es de los permitidos. <br /><br /><a href="imagen.php">haz click aqui</a>
  18.            ';// Si no es el tipo permitido lo desimos
  19. }
  20. else echo 'El Archivo Supera el Peso Máximo. <br /><br /><a href="imagen.php">haz click aqui</a>
  21.            ';// Si supera el tamaño de permitido lo desimos
  22. }
  23. ?>