Ver Mensaje Individual
  #1 (permalink)  
Antiguo 29/05/2013, 17:48
Avatar de pampa_
pampa_
 
Fecha de Ingreso: mayo-2006
Mensajes: 334
Antigüedad: 18 años
Puntos: 1
adjuntar varios archivos formulario php

Hola!! llevo bastante rato mirando hilos de este foro y googleando para buscar una solucion a mi problema.

El tema es que quiero adjuntar en mi formulario php varios archivos por ahora puedo adjuntar el primero... pero me pierdo un poco con el codigo...

Código HTML:
Ver original
  1. <TR>
  2.                     <TD width='250'><P class="celdas" align='right' style='font-family:arial;color:white;font-size:14px;padding-right:4px;padding-bottom:6px;'>Ficha t&eacute;cnica:</P></TD>
  3.                     <TD><input type="file" name="ficha[]" /></TD>  
  4.                     <TD></TD>
  5.                 </TR>
  6.                 <TR>
  7.                     <TD width='250'><P class="celdas" align='right' style='font-family:arial;color:white;font-size:14px;padding-right:4px;padding-bottom:6px;'>Imagen 1:</P></TD>
  8.                     <TD><input type="file" name="ficha[]" /></TD>  
  9.                     <TD></TD>
  10.                 </TR>
  11.                 <TR>
  12.                     <TD width='250'><P class="celdas" align='right' style='font-family:arial;color:white;font-size:14px;padding-right:4px;padding-bottom:6px;'>Imagen 2:</P></TD>
  13.                     <TD><input type="file" name="ficha[]" /></TD>  
  14.                     <TD></TD>
  15.                 </TR>
  16.                 <TR>
  17.                     <TD width='250'><P class="celdas" align='right' style='font-family:arial;color:white;font-size:14px;padding-right:4px;padding-bottom:6px;'>Imagen 3:</P></TD>
  18.                     <TD><input type="file" name="ficha[]" /></TD>  
  19.                     <TD></TD>


Esta seria una pequeña parte del formulario donde veis que los campos se llaman ficha[]
después tengo el siguiente codigo que funciona solo para el primer archivo.
Código PHP:
Ver original
  1. <?php
  2.  
  3.         $nombre_archivo = $_FILES['ficha']['name'];
  4.         $tipo_archivo = $_FILES['ficha']['type'];
  5.         $tamano_archivo = $_FILES['ficha']['size'];
  6. $email = "[email protected]"; 
  7. $subject = "Contacto";
  8. $mensaje= "---------------------------------- <br />";
  9.         $mensaje.= "            Contacto               <br />";
  10.         $mensaje.= "---------------------------------- <br />";
  11.        
  12.         if($_POST['empresa'] != ""){
  13.             $mensaje.= "NOMBRE Y APELLIDOS:  ".$_POST['empresa']."<br />";
  14.         }else{
  15.             $_POST['empresa'] == "";
  16.         }
  17.        
  18.        
  19.        
  20.         if($_POST['email'] != ""){
  21.             $mensaje.= "EMAIL:    ".$_POST['email']."<br />";
  22.         }else{
  23.             $_POST['email'] == "";
  24.         }
  25.        
  26.         if($_POST['telefono'] != ""){
  27.             $mensaje.= "TELEFONO: ".$_POST['telefono']."<br />";
  28.         }else{
  29.             $_POST['telefono'] == "";
  30.         }
  31.        
  32.       if($_POST['marca'] != ""){
  33.             $mensaje.= "MARCA:   ".$_POST['marca']."<br />";
  34.         }else{
  35.             $_POST['marca'] == "";
  36.         }
  37.        
  38.         if($_POST['modelo'] != ""){
  39.             $mensaje.= "MODELO:   ".$_POST['modelo']."<br />";
  40.         }else{
  41.             $_POST['modelo'] == "";
  42.         }
  43.        
  44.         if($_POST['matricula'] != ""){
  45.             $mensaje.= "MATRICULA:   ".$_POST['matricula']."<br />";
  46.         }else{
  47.             $_POST['matricula'] == "";
  48.         }
  49.        
  50.         if($_POST['refpieza'] != ""){
  51.             $mensaje.= "REFERENCIA PIEZA:   ".$_POST['refpieza']."<br />";
  52.         }else{
  53.             $_POST['refpieza'] == "";
  54.         }
  55.          
  56.         $mensaje.= "---------------------------------- <br /><br />";
  57.         $mensaje.= $_POST['mensaje']."<br /><br />";
  58.  
  59. $mensaje = utf8_decode($mensaje);
  60.         $nombref = $_FILES["ficha"]["name"];        
  61.            
  62.         $headers .= "From: ".$_POST['email']."\r\n";  
  63.         $headers = "MIME-Version: 1.0\r\n";
  64.         $headers .= "Content-type: text/html; charset=UTF-8\r\n";
  65.        
  66.         $headers .= "Content-type: multipart/mixed; ";
  67.         $headers .= "boundary=\"Message-Boundary\"\n";
  68.         $headers .= "Content-transfer-encoding: 7BIT\n";
  69.         $headers .= "X-attachments: $nombref";
  70.                
  71.         $body_top  = "--Message-Boundary\n";
  72.         $body_top .= "Content-type: text/html; charset=UTF-8\n";
  73.         $body_top .= "Content-transfer-encoding: 7BIT\n";
  74.         $body_top .= "Content-description: Mail message body\n\n";
  75.                
  76.         $mensaje = $body_top.$mensaje;
  77.  
  78.         if($tamano_archivo>0)
  79.         {
  80.            //Leo el fichero
  81.            $oFichero = fopen($_FILES["ficha"]["tmp_name"], 'r');
  82.            $sContenido = fread($oFichero, filesize($_FILES["ficha"]["tmp_name"]));
  83.            $sAdjuntos .= chunk_split(base64_encode($sContenido));
  84.            fclose($oFichero);
  85.            //Adjunto el fichero
  86.            $mensaje .= "\n\n--Message-Boundary\n";
  87.            $mensaje .= "Content-type: Binary; name=\"$nombref\"\n";
  88.            $mensaje .= "Content-Transfer-Encoding: BASE64\n";
  89.            $mensaje .= "Content-disposition: attachment; filename=\"$nombref\"\n\n";
  90.            $mensaje .= "$sAdjuntos\n";
  91.            $mensaje .= "--Message-Boundary--\n";
  92.         }
  93.  
  94.    mail($email, $subject, $mensaje, $headers);
  95.        
  96.      header ('Location: http://levanteautomocion.es');
  97.        
  98.  
  99.  
  100. ?>

Espero que no sea mucho lio
Gracias