Foros del Web » Programando para Internet » PHP »

Adjuntar archivo en formulario PHP

Estas en el tema de Adjuntar archivo en formulario PHP en el foro de PHP en Foros del Web. Hola, mi problema es que tengo un formulario en PHP (del que no se gran cosa) que me paso un amigo, y le inserte un ...
  #1 (permalink)  
Antiguo 19/09/2008, 06:57
 
Fecha de Ingreso: abril-2007
Mensajes: 31
Antigüedad: 17 años
Puntos: 0
Adjuntar archivo en formulario PHP

Hola, mi problema es que tengo un formulario en PHP (del que no se gran cosa) que me paso un amigo, y le inserte un campo de envió de archivo, cuando envió el formulario, me llega el mail al correo, pero no llega el archivo adjunto que cargue en el formulario, y mi pregunta es.

Como debo de hacerlo? y como puedo definir el tipo de archivo y tamaño maximo de este? Me sirve el código que ya existe y solo debo añadir mas? o debo partir de 0?

Os dejo aquí el código, vuestra ayuda sera de mucha utilidad eso seguro.

Archivo formulario:

Código:
<head>

<title>Envío de sugerencias</title> 

 <?
    echo "<script type=\"text/javascript\">\n";
    echo "<!--\n";
    echo "function validar(form1) {\n";
    echo "if (form1.name.value.length < 2) {\n";
    echo "alert('";
    echo "Inserte el nombre";;
    echo "')\n";
echo "form1.name.focus();\n";
  echo "return (false);}\n";
echo "var checkOK = \"ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ \" + \"abcdefghijklmnñopqrstuvwxyzáéíóú \";\n";
  echo "var checkStr = form1.name.value;\n";
  echo "var allValid = true;\n";
  echo "var uword = hex_md5(document.getElementById('uword').value)\n";
  echo "for (i = 0; i < checkStr.length; i++) {";
  echo "ch = checkStr.charAt(i);\n";
  echo "for (j = 0; j < checkOK.length; j++)\n";
  echo "if (ch == checkOK.charAt(j))\n";
  echo "break;\n";
  echo "if (j == checkOK.length) {\n";
  echo "allValid = false;\n";
  echo "break;";
  echo "}}\n";
  echo "if (!allValid) {\n";
  echo "alert('";
  echo "inserte el nombre";
  echo "');\n";
  echo "form1.name.focus();\n";
  echo "return (false);}\n";
echo "if ((form1.email.value.indexOf ('@', 0) == -1)||(form1.email.value.length < 9) || 

form1.email.value.indexOf ('.', 0)== -1 ){\n";
  echo "alert('";
  echo "inserte el email";
  echo "');\n";
  echo "form1.email.focus();";
  echo "return (false);}\n";
echo "if (form1.message.value.length < 7) {\n";
  echo "alert('";
  echo "inserte el mensaje";
  echo "');\n";
  echo "form1.message.focus();";
  echo "return (false);}\n";

echo "if (uword==cword[anum-1]) {\n";
  echo "return true;}\n";
  echo "else {\n";
  echo "alert('";
  echo "inserte el código de la imagen";
  echo "');\n";
  echo "document.getElementById('uword').focus();\n";
  echo "return false;}\n";
  echo "return (true);}\n";
  echo "-->\n";
  echo "</script>\n";
  ?>
  
<script type="text/javascript">
<!--
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' . Comprueba que esta bien escrito.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+'. Solo numeros.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' es obligatorio.\n'; }
    } if (errors) alert('Comprueba los siguientes campos:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
//-->
</script>
</head>

<body onLoad="document.form1.name.focus();">
<div align="center">
  <table width="842" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="842" height="40"><img src="../img/carrito.jpg" width="842" height="40" border="0" usemap="#Map" /></td>
    </tr>
  </table>
  <table width="842" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="842" height="161" background="../img/cabecera.jpg"></td>
    </tr>
  </table>
  <table width="842" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="842" height="176" background="../img/cabecera2.jpg"></td>
    </tr>
  </table>
  <table width="842" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="196" height="625" background="../img/dosbotones.jpg"></td>
      <td width="646" height="625" align="left" valign="top" background="../img/cuadrado-logos-form.jpg">
      <form action="gracias_comunidad.php" method="post" enctype="multipart/form-data"  name="form1" id="form1" onsubmit="return validar(this)">
      <div align="center">
      <table width="40" border="0" align="center" cellpadding="2">
        <tr>
          <td colspan="2"><div align="justify" class="Estilo8">
            <p></p>
            </div></td>
            </tr>
        <tr>
          <td width="157" align="right" class="Estilo8"><div>Nombre:</div></td>
            <td width="229"><div align="left">
              <input  name="name" type="text" class="TAForm" value="" size="30" />
              <span class="Estilo10">*</span></div></td>
          </tr>
        <tr>
          <td width="157" align="right" class="Estilo8"><div>Edad::</div></td>
            <td width="229"><div align="left">
              <input  name="edad" type="text" class="TAForm" value="" size="2" maxlength="2" id="edad" />
              <span class="Estilo10">*</span></div></td>
          </tr>  
        <tr>
          <td align="right" class="Estilo8"><div align="right">E-mail:</div></td>
            <td><div align="left">
              <input  name="email" type="text" class="TAForm" value="" size="30" />
              <span class="Estilo8">Visible?</span>
              <label>
            <input name="emailpublico" type="checkbox" id="emailpublico" />
            </label>
            </div></td>
          </tr>
          <tr>
          <td align="right" class="Estilo8"><div align="right">Messenger:</div></td>
            <td><div align="left">
              <input  name="msn" type="text" class="TAForm" value="" size="30" />
            </div></td>
          </tr>
        <tr>
          <td align="right" class="Estilo8"><div align="right">Poblacíon: </div></td>
            <td><div align="left">
              <input  name="poblacion" type="text" class="TAForm" value="" size="30" />
              <span class="Estilo10">*</span></div></td>
          </tr>
        <tr>
          <td align="right"><div align="right"><span class="Estilo8">Zona en la comunidad:
          </span></div>
            <span class="Estilo8"><label></label>
          </span>            <label> </label></td>
            <td><div align="left">
              <select name="phone" class="TAForm" id="phone">
                <option value="Palabra">Palabra</option>
                <option value="Producto">Producto</option>
                <option value="Lo mas solidario">Accíon para Se solidario</option>
              </select>
              <span class="Estilo10">*</span></div></td>
          </tr>
        <tr>
          <td colspan="2">
            <div align="center">
              <textarea  name="message" cols="85" rows="7" class="TAForm">Escribe aqui la descripcion de la foto.</textarea>
            </div></td>
          </tr>
        <tr>
          <td align="right" class="Estilo8"><div align="right">Subir foto:: </div></td>
            <td><div align="left">
              <label>
              <input name="userfile" type="file" class="TAForm" id="imagen" />
              </label>
              <span class="Estilo10">*</span></div></td>
          </tr>  
        <tr>
          <td colspan="2"><div align="center"><span class="Estilo8">Inserta el código de la imagen que aparece debajo.</span></div>          
          </tr>
        <tr>
          <td  align="right" colspan="2">
            <div align="center">
              <input name="uword"  type="text" class="TAForm" id="uword" value="" />
              </div></td>
          </tr>
        <tr>
          <td colspan="2"><div align="center"><script type="text/javascript">cimg()</script></div></td>
            </tr>
        <tr>
          <td colspan="2"><div align="center">
            <input type="submit" class="TAForm" value="enviar"  />
          </div></td>
        </tr>
      </table>
   </form>
      <br />
          <br>
      </td>
    </tr>
  </table>
  <table width="842" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="842" height="36"><img src="../img/pie.jpg" width="842" height="36" /></td>
    </tr>
  </table>
</div>

<map name="Map" id="Map"><area shape="rect" coords="660,3,830,37" href="../tienda/contact_us.html" alt="Contacto" />
</map></body>
</html>
  #2 (permalink)  
Antiguo 19/09/2008, 06:58
 
Fecha de Ingreso: abril-2007
Mensajes: 31
Antigüedad: 17 años
Puntos: 0
Respuesta: Adjuntar archivo en formulario PHP

Y este es el codigo PHP del Archivo confirmación envió:

Código:
<?
  @import_request_variables("gpc");
    $youremail = "[email protected]"; //YOUR MAIL HERE 
    $subject = "Formulario sugerencias "; // TITLE OF DE SUBJECT IN MAIL
    $redirect = "index.html";//URL TO BE REDIRECTED IN 5 SECONDS
    $secs = "5";// TIME TO BE REDIRECTED

if(eregi("MIME-Version:",$postVars)) {
mail("[email protected]"/*YOUR MAIL HERE*/, "Form Hijack Attempt", "A spam relay was attempted from the Web site and was blocked.", "From:SpamMonitor");
die();
} //BLOCKING spam


 $secret = 'ssshhitsasecret';  
    $token = md5(rand(1, 1000).$secret);
    $_SESSION['token'] = $token;
    ?> 
<?
//
  $name = stripslashes($name);
  $poblacion = stripslashes($poblacion);
  $message = stripslashes($message);
  $headers .= "From: " . $email . "\r\n\r\n"; 
//
    mail("$youremail", "$subject","
Nombre : $name

Edad : $edad
	
Poblacion : $poblacion
	
Email : $email

Publico : $emailpublico

Msn : $msn
	
Zona comunidad : $phone

userfile : $userfile
	
Detalles : $message
	 
",$headers);
   // Strip \r and \n from the email address - DESACTIVADOS POR ALBERTO POR QUE DAN ERROR DE LINEA
   $_POST['email'] = str_replace("\r", "", $_POST['email']);
   $_POST['email'] = str_replace("\n", "", $_POST['email']);

//*****COMMENT: if you have problems with the lines 40/41, replace these for:
//$_POST['email'] = str_replace("\r", "", $_POST['email']);
//$_POST['email'] = str_replace("\n", "", $_POST['email']);
//*****

   $_SESSION['token'] = $token; 
   $token = md5(rand(1, 1000).$secret);
   $secret = 'ssshhitsasecret';
   $field = preg_replace( "/[\n\r]+/", " ", $field );
    // Remove injected headers
    $find = array("/bcc\:/i","/Content\-Type\:/i","/cc\:/i","/to\:/i");
    $_POST['email'] = preg_replace($find, "", $_POST['email']);
    $message = preg_replace($find, "", message); 
    $email=str_replace("\r","\n",$email);
    $name=str_replace("\r","\n",$name);
	$poblacion=str_replace("\r","\n",$poblacion);
    $message=str_replace("\r","\n",$message);
    $phone=str_replace("\r","\n",$phone);
   
   
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
ob_clean();
mail("$youremail", "Message Killed", "$message", "From: $name <$email>");
exit("Message killed.");
}
 if (eregi('^(bcc$|content-type|mime-version|--)',$key))
print_error("Field names indicate exploit.");  //BLOCKING SPAM

?>
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:54.