Question:
Date: 01-04-2020 08:54:43
(In Spanish)
Problemas con envio adjunto con PHP y HTML5[Unresolved]
Saludos a todos. Soy nuevo en esto, y no tengo mucha experiencia en PHP ni HTML, pero he estado procurando estudiar y hacer pequeños proyectos; y tengo un problema justo ahora. El formulario se llena y se adjunta el archivo, cuando el usuario le da enviar, el texto plano llega y se lee bien, pero el archivo adjunto llega como "noname" y no lo deja ver, si se descarga no se puede ver su contenido, y no encuentro el error. Si me pueden orientar, les agradezco. Dejo lo correspondiente a HTML (el form), el PHP y el script de Javascript con el que enlazo la petición de envío.<form action="#" id="formulario" method="post" enctype="multipart/form-data"> <div class="row"> <div class="col-md-8 col-md-offset-2 wow animated fadeInUp"> <div class="submit-form"> <div class="col-md-3"> <input name="name" type="text" id="name" required="" placeholder="Your name..."/> </div> <div class="col-md-3"> <input name="email" type="text" id="email" required="" placeholder="Your email..."/> </div> <div class="col-md-3"> <select name="opcion" id="opcion"> <option value="null">Interested</option> <option value="Embroidery">Embroidery</option> <option value="Screen Print">Screen Print</option> <option value="Digital Vinyl Print">Digital Vinyl Print</option> <option value="Private and Company Labels">Private and Company Labels</option> <option value="Combination">Combination</option> </select> </div> <div class="col-md-3"> <select name="tipo" id="tipo"> <option value="null">What kind?</option> <option value="Business">Business</option> <option value="Personal">Personal</option> </select> </div> <div class="col-md-3"> <textarea name="Notes" id="Notes" rows="4" maxlength="700" required="" placeholder="Design Details: Specific information on locations of print..."></textarea> </div> <div class="col-md-3"> <input type="file" name="archivo" id="real-file" accept=".pdf,.jpg,.png," hidden="hidden"/> </div> </div><!-- submit form --> </div> </div> <div class="row"> <div class="col-md-8 col-md-offset-2 wow animated fadeInUp"> <div class="col-md-3"> <button type="button" id="addfile" class="templatemo-botonfile">Choose a file</button> <span id="textboton">No file chosen, yet.</span> </div> <div class="col-md-3"> <input type="submit" value="Submit" class="templatemo-submit" /> </div> <div id="resultado"></div> </div> </div> </form> <script type="text/javascript"> const realfile = document.getElementById("real-file"); const boton = document.getElementById("addfile"); const textoboton = document.getElementById("textboton"); boton.addEventListener("click", function(){ realfile.click(); }); realfile.addEventListener("change", function(){ if(realfile.value){ textoboton.innerHTML = realfile.value; } else { textoboton.innerHTML = "No file chosen, yet." } }); </script>
$(document).ready(function(){ $("#formulario").submit(function(){ var url ="envioforma.php"; $.ajax({ type:"post", url:url, data: $("#formulario").serialize(), success: function(data) { $("#resultado").html(data); } }); return false; }); });
$name= isset($_POST['name']) ? $_POST['name'] : ''; $email= isset($_POST['email']) ? $_POST['email'] : ''; $gender= isset($_POST['gender']) ? $_POST['gender'] : ''; $age= isset($_POST['age']) ? $_POST['age'] : ''; $texto=isset($_POST['Notes']) ? $_POST['Notes'] : ''; //variables para los archivos adjuntos $filenombre=$_FILES['archivo']['name']; $filetam= $_FILES['archivo']['size']; $filetipo= $_FILES['archivo']['type']; $filetemp=$_FILES["archivo"]["tmp_name"]; $fecha= time(); $fechaFormato = date("j/n/Y",$fecha); $asunto= 'You have a new message, fromm Planet Embroidery Web Page'; //configuración de los encabezados del formulario. Envío $headers ="MINE-VERSION: 1.0\r\n"; $headers .="Content-type: multipart/mixed;"; $headers .="boundary=\"=C=T=E=C=\"\r\n"; $headers.= 'From: '.$name. "\r\n"; $headers.= 'Email: '.$email. "\r\n"; $contenido = "--=C=T=E=C=\r\n"; $contenido .= "Content-type: text/plain"; $contenido .= "charset=utf-8\r\n"; $contenido .= "Content-Transfer-Encoding: 8bit\r\n"; $contenido .= "\r\n"; // línea vacía $contenido .='Name: '.$name. ' Email contact: '.$email."\r\n"; $contenido .='Interested: '.$gender. ' For: '.$age."\r\n"; $contenido .=" con fecha: " . $fechaFormato . "\r\n"; $contenido .="\r\n"; $contenido .='Notes: '.$texto. "\r\n"; $contenido .="\r\n"; //Cuerpo con el archivo adjunto. $contenido .= "--=C=T=E=C=\r\n"; $contenido .= "Content-Type: application/octet-stream; "; $contenido .= "name=" . $filenombre . "\r\n"; $contenido .= "Content-Transfer-Encoding: base64\r\n"; $contenido .= "Content-Disposition: attachment; "; $contenido .= "filename=" . $filenombre . "\r\n"; $contenido .= "\r\n"; // línea vacía $fp = fopen($filetemp, "rb"); $file = fread($fp, $filetam, $filetipo); $file = chunk_split(base64_encode($file)); //complemento segunda parte para adjunto archivo. $contenido .= "$file\r\n"; $contenido .= "\r\n"; // línea vacía $contenido .= "--=C=T=E=C=--\r\n"; $envio = @mail('planetemby@gmail.com', $asunto, $contenido, $headers ); if(envio){ $resultado='<h4>Your message has been sent. Soon, we will contact you.</h4>'; }else{ $resultado='<h4>The message has not been sent. Try again.</h4>'; } echo $resultado;Votes: 0 - Answers: 3 - Views: 9 Share on: Google Facebook Twitter LinkedIn Link
Answers:
-
Date: 01-04-2020 13:24:11 Hola Miguel,
Noto un problema muy grande:
Estás usando el supresor de mensaje de error @ en la función mail.
Te recomiendo que no lo uses jamás en un sitio pues oculta mensajes de error y dificulta la resolución de problemas. Su uso se reduce a escenarios muy específicos.
Remuévelo e intenta de nuevo para ver si eso ayuda un poco más a encontrar el error. Votes: 1 - Link answer -
Date: 01-04-2020 13:59:05 Hola, muchas gracias por la respuesta. Voy a hacer la corrección que me ha indicado. Cualquier cosa volveré. Gracias nuevamente Votes: 1 - Link answer
-
Date: 31-08-2020 06:13:56 Porque te complicas la Vida... ??? te recomiendo que le des un Vistazo vas a aprender a Desarrollar sistemas web con lineas de codigo corto y tan facil de entender https://www.w3schools.com/php/DEFAULT.asp Votes: 0 - Link answer
To actively participate in the community first must authenticate, enter the system.Sign In