Ver Mensaje Individual
  #3 (permalink)  
Antiguo 12/10/2011, 10:55
Avatar de CesarHC
CesarHC
 
Fecha de Ingreso: junio-2011
Ubicación: localhost
Mensajes: 566
Antigüedad: 12 años, 11 meses
Puntos: 56
Respuesta: Subir fichero por php

Yo empece a subir archivos con este script, como todo esta en un solo archivo se entiende como se sube el archivo,despues puedes mejorarlo.

De paso ves las funciones que usa y las rebisas como "is_uploaded_file" "move_uploaded_file" y "$_FILES".


frmnotas_prensag.php

Código PHP:
Ver original
  1. <?php  
  2. if (isset($_POST['submit'])) {  
  3.     if(is_uploaded_file($_FILES['fichero']['tmp_name'])) {
  4.      
  5.       // me verifica haya sido cargado el archivo  
  6.         $ruta_destino = "fotos/";
  7.         $ruta_destino2 = "http://localhost/presentacion/fotos/";
  8.         $namefinal= trim ($_FILES['fichero']['name']); //linea nueva devuelve la cadena sin espacios al principio o al final
  9.        // $namefinal= preg_replace (" ", "", $namefinal);// linea nueva devuelve la cadena sin espacios entre palabtas
  10.         $imagen= $ruta_destino . $namefinal;  
  11.         $url= $ruta_destino2 . $namefinal;  
  12.  
  13.         if(move_uploaded_file($_FILES['fichero']['tmp_name'], $imagen)) { // se coloca en su lugar final  
  14.                    
  15.                    
  16. //conectamos a la base de datos para almacenar los datos y la ruta del archivo
  17.  
  18. $server="localhost";
  19. $usuario="root";
  20. $pass="";
  21. $bd="tubd";
  22. $link=mysql_connect($server,$usuario,$pass)or die (exit(mysql_error()));
  23.                  
  24.                    $fecha  = $_POST["fecha"];
  25.                    $titulo  = $_POST["titulo"];
  26.                    $descripcion  = $_POST["descripcion"];
  27.                    
  28.                     if (isset($fecha) && $fecha!="" && isset($titulo) && $titulo!="" && isset($descripcion) && $descripcion!="") {
  29.                   $query = "INSERT INTO notas_prensa  
  30.   VALUES (0,'$fecha','$titulo','$descripcion', '$url','$imagen')";
  31.    echo "<script languaje='javascript'>alert('Datos Guardados.')
  32.             document.location = ('../presentacion/frmnotas_prensag.php');
  33.             </script>";
  34.        
  35. }
  36. else {
  37.             echo "<script languaje='javascript'>alert('No llenado todos los campos.')
  38.             </script>"; }
  39.                    
  40.                    
  41.    
  42.  
  43.        mysql_query($query) or die(mysql_error());
  44.         }  
  45.     }  
  46.  }
  47. //Formulario  
  48.  
  49. ?>

Código HTML:
Ver original
  1. <form action="frmnotas_prensag.php" method="post" enctype="multipart/form-data">  
  2. <h1 align="center" class="titulo">NUEVA NOTA DE PRENSA</h1>
  3. <table  align="center" id="datos" >
  4. <tr>
  5.     <th></th>
  6.     <td><input name="id" type="hidden" size="430"> </td>
  7.   </tr>
  8.   <tr>
  9.     <th>Imagen</th>
  10.     <td><input name="fichero" type="file" size="30" maxlength="150"> </td>
  11.   </tr>
  12.   <tr>
  13.     <th>Fecha</th>
  14.     <td> <input type="text" name="fecha" id="sel4" size="30"  ></tr>
  15.   <tr>
  16.     <th>Titulo</th>
  17.    
  18.     <td><input type="text" id="titulo"  size="30" name="titulo"/>
  19.       <input type="button" id="chequeo" value="Verificar"/>
  20.       <br/>
  21.       <div id="resultado"></div></td>
  22. </td>
  23.   </tr>
  24.   <tr>
  25.     <th>Descripcion</th>
  26.     <td ><textarea name="descripcion" cols="40" rows="10"></textarea> </td>
  27.   </tr>
  28.   <tr>
  29.     <td colspan="2" align="center"><input name="submit" type="submit" value="Guardar" id="button"></td>
  30.     </tr>
  31.  
  32. </form>
__________________
Solo la práctica no te traicionara ¡¡¡¡¡¡

Seguir el camino tu debes PHP The Right Way.