Ver Mensaje Individual
  #13 (permalink)  
Antiguo 10/12/2006, 01:22
Avatar de ginitofl
ginitofl
 
Fecha de Ingreso: diciembre-2006
Ubicación: Lima Perú
Mensajes: 349
Antigüedad: 17 años, 4 meses
Puntos: 22
Re: Subir Imagenes en Noticias

HOLA A TODOS EN ESPECIAL A FUMAKAO Y ESPERO LLENAR TUS ESPECTATIVAS, BUENO EL SUBIR UNA IMAGEN ES ALGO SENCILLO, YO TE SUGIERO QUE LO HAGAS EN DOS PARTES UNA UN FORMULARIO SIMPLE EN HTML COMO EJEMPLO: formulario.htm Y OTRO DONDE RECIBAS LOS DATOS DEL FORMULARIO POR EJEMPLO: creanoticia.php ADEMAS DE ESTO NECESITAS UNA CARPETA COMO POR EJEMPLO: fotos RECUERDA QUE TODOS DEBEN ESTAR EN UN MISMO DIRECTORIO CASO CONTRARIO NO FUNCIONARA LUEGO NECESITAS DARLE PERMISO DE CHMOD A TU CARPETA CON PERMISSIONS: 775 BUENO YA QUE TENEMOS TODO PREPARADO AHI TE VA LOS EJEMPLOS:

PRIMERO TCREAS TU TABLA LLAMADA: noticias Y ES LA SIGUIENTE:

CREATE TABLE `noticias` (
`id` TINYINT NOT NULL AUTO_INCREMENT ,
`titulo` VARCHAR( 80 ) NOT NULL ,
`mensaje` TEXT NOT NULL ,
`foto` VARCHAR( 100 ) NOT NULL ,
`fecha` VARCHAR( 20 ) NOT NULL ,
PRIMARY KEY ( `id` )
);

LISTA NUESTRA TABLA PROCEDEMOS CON EL: formulario.htm QUE ES EL SIGUIENTE:

<html>

<head>
<title>ENVIO DE NOTICIAS POR Gino'FL</title>
</head>

<body>

<form name="form1" action="creanoticia.php" method="post"enctype="multipart/form-data">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
<tr>
<td width="25%">&nbsp;</td>
<td width="50%" colspan="2">
<p align="center"><font color="#FF0000" size="5"><b>ENVIÓ DE NOTICIAS</b></font></td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">Titulo de Noticia</td>
<td width="25%">
<input type="text" name="titulo" size="17"></td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">Mensaje</td>
<td width="25%" rowspan="6">
<textarea name="mensaje" cols="25" rows="7"></textarea></td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%" rowspan="6" valign="middle">
<p align="center">
<img src="escudo.jpg" name="imagen" width="130" height="150" class="cajatexto1"></td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">
<p align="center">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">Buscar Imagen</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">
<input name="userfile" type="file" class="cajatexto1" onChange="muestra();" size="16" style="float: left">
<input type="hidden" name="MAX_FILE_SIZE" value="100000"></td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%" valign="top">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="50%" colspan="2">
<p align="center">
<input name="grabar" type="submit" class="cajatexto1" value="Enviar Datos" style="font-weight: bold">&nbsp;
<input type="reset" value="Cancelar" style="font-weight: bold"></td>
<td width="25%">&nbsp;</td>
</tr>
</table>

</form>

</body>

</html>
<script language="javascript">
function muestra()
{
document.form1.imagen.src=document.form1.userfile. value;
}
</script>


LUEGO LO PASAMOS A LO MAS IMPORTANTE Y EL QUE HACE FUNCIONAR TODO QUE ES EL: creanoticia.php Y ES ESTA:

Código PHP:
<? 
/*****Creado por: [email protected]*****/ 
/* ginitofl */ 
/* Gino Flores Lopez  */ 

$server="servidor";              /* Nuestro server mysql:  */ 
$database="basededatos";               /* Nuestra base de datos */ 
$dbpass="contraseña";               /*Nuestro password */ 
$dbuser="usuario";                 /* Nuestro user  */


//datos del arhivo 
$nombre_archivo $HTTP_POST_FILES["userfile"]["name"]; 
$tipo_archivo $HTTP_POST_FILES["userfile"]["type"]; 
$tamano_archivo $HTTP_POST_FILES["userfile"]["size"]; 

//compruebo si las características del archivo son las que deseo 

if (!((strpos($tipo_archivo"gif") || strpos($tipo_archivo"jpeg")) && (    $tamano_archivo 100000)))

    echo 
"La extensión o el tamaño de los archivos no es correcta. <br><br><table><tr><td><li>Se permiten archivos .gif o .jpg<br><li>se permiten archivos de 100 Kb máximo.</td></tr></table>";
?>
    <script language="javascript">
        setTimeout("url()",3000);
        function url()
        {
        window.history.back();
        }
    </script>           
<?    
}
else

    
$nom_img$nombre_archivo;
    
    
$directorio 'fotos/';

    if (
move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'],$directorio "/" $nom_img))
    { 
            
$diaactualdate("d"); 
        
$mesactualdate("m"); 
        
$anoactualdate("Y");
        
$fecha$diaactual "/" $mesactual "/" $anoactual;
        
        
//NOS CONECTAMOS A LA BASE DE DATOS

        
$link=mysql_connect($server,$dbuser,$dbpass);
                        
        
$query="INSERT INTO noticias (titulo,mensaje,foto,fecha) VALUES ('$titulo','$mensaje','$nom_img','$fecha')"

        
$result=mysql_db_query($database,$query,$link);
        
        if(
mysql_affected_rows($link))
        { 
            echo 
"Noticia ingresada correctamente"
            
        } else 
        { 
            echo 
"Error introduciendo la Noticia"
        } 
/* Cierre del else */ 




    
}
    else
    {
        echo 
"error al subir la foto";
    }
}

?>


BIEN ESO ES TODO Y ESPERO HABER HABER DESPEJADO ALGUNAS DUDAS AL RESPECTO CUALQUIER CONSULTA ESCRIBANME.