Ver Mensaje Individual
  #1 (permalink)  
Antiguo 26/01/2011, 07:39
javy
 
Fecha de Ingreso: septiembre-2002
Mensajes: 153
Antigüedad: 21 años, 7 meses
Puntos: 1
¿Álguien me puede echar un cable con esto...? - Validar 2 campos

Hola a todos de nuevo.

Estoy intentando hacerme una página desde la cual poder subir una imagen.

En esta página hay dos campos: uno para meter texto descriptivo y otro para subir un archivo.

Lo que intento hacer es que me valide los campos de la siguiente manera:

1- si no hay texto ni archivo de imagen: mensaje de aviso y no manda el formulario.

2- si hay texto pero no hay archivo de imagen: mensaje de aviso y no manda el formulario.

3- si hay archivo de imagen pero no hay texto: mensaje de aviso y no manda el formulario.

Bueno: paso 1 - funciona / paso 2 - funciona / paso 3 - pasa de todo y manda la imagen sin el texto. Ya no sé ni las vueltas que le he dado al código.

Código:
$cnx = conectar ();

			$mensajes = array(); // ALMACENA LOS MENSAJES QUE SE QUIEREN MOSTRAR

			// SI EL FORMULARIO HA SIDO ENVIADO, SE VALIDA			
			if(isset($_POST['submit'])){
			{
			// SE CREAN LAS VARIABLES
			$textoarchivos = $_POST['textoarchivos'];
			$fotosarchivos = $_FILES['imagen']['name']; // NOMBRE DEL INPUT DE LA IMAGEN
			// SE VALIDAN LAS VARIABLES: SI SE MANDAN VACÍAS...
			if($textoarchivos =='' || ($_FILES['imagen']['name']) ==''){
			echo "<tr>
					<td colspan='2' class='texto_paginas_alertas'>¡Debe introducir el texto y seleccionar la imagen o archivo!</td>
				</tr>\n";			
			}
			if (is_uploaded_file($_FILES['imagen']['tmp_name'])) { // NOMBRE TEMPORAL DE LA IMAGEN
			$ext = substr($_FILES['imagen']['name'],-3); // SE OBTIENE LA EXTENSIÓN
			// COMPRUEBA QUE LAS EXTENSIONES SEAN LAS CORRECTAS
			$extensiones = array (
			'image/jpeg', // imagen jpg
			'image/pjpeg', // imagen jpg
			'image/jpg', // imagen jpg
			'image/png', // imagen png			
			'image/gif', // imagen gif
			'application/x-rar-compressed', // rar
			'application/x-rar', // rar
			'application/rar', // rar
			'application/x-zip-compressed', // zip
			'application/x-zip', // zip
			'application/zip', // zip
			);
			if(!in_array($_FILES['imagen']['type'],$extensiones)){ // MUESTRA UN AVISO SI LA EXTENSIÓN NO ES CORRECTA 
			$mensajes[] = 'El fichero '.$_FILES['imagen']['name'].' no tiene una extensión válida';
			} else {
			// NUEVO NOMBRE PARA LA IMAGEN
			/* SI SE QUITA, DEJAR EL NOMBRE COMO ESTÁ, PERO SE PUEDEN DUPLICAR DATOS EN LA
			BASE DE DATOS, PUDIENDO DAR ERRORES */
			// SELECCIONAR SÓLO UNA DE ELLAS
			// $nuevoNombre = time().$_FILES['imagen']['name']; // Nombre con la fecha
			// $nuevoNombre = 'Nombre_De_La_Imagen.'.$ext; // Nombre personalizado
			$nuevoNombre = $_FILES['imagen']['name']; // Nombre original
			
			// CARPETA DE ALMACÉN
			move_uploaded_file($_FILES['imagen']['tmp_name'], "../pagina/imagenes_privado/$nuevoNombre");
			// OBTENER LA INFORMACIÓN
			$data = GetImageSize("../pagina/imagenes_privado/$nuevoNombre");
			
			$sql = "INSERT INTO archivos SET ";
			$sql .= "textoarchivos ='".$_POST['textoarchivos']."',";
			$sql .= "fotosarchivos = '../pagina/imagenes_privado/$nuevoNombre'";
			
			$res = mysql_query($sql) or die(mysql_error());	
			
			echo "NUEVA IMAGEN AÑADIDA.\n";
					
			mysql_close($idcnx);
			exit;	
			}
			}
			}
			}
?>

<form enctype="multipart/form-data" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<textarea name="textoarchivos" cols="100" rows="30" id="textoarchivos"><?php echo $_POST['textoarchivos']; ?></textarea>
<input name="imagen" type="file" size="70" value="mierdas" />
<input name="submit" type="image" src="images/aceptar.png" value="submit" />
</form>
Siento mucho la chapa, y gracias a todos.

Javy