Validar tipo de extensi贸n en un attach
Hola, tengo un problema, resulta que me estan pidiendo que mi aplicaci贸n de mail valide y solo permita adjuntar un solo tipo de extensi贸n (.jpg) y la verdad no se bien como hacerlo.
Alguien podr铆a ayudarme, no se bien como delimitar esto.
Alguien podr铆a ayudarme, no se bien como delimitar esto.
hola
te dejo el codigo completo de un formulario. Copialo a un archivo de texto y llamalo envio.php, solo toma los jpg y le podes limitar el tama帽o maximo:
<html>
<head>
<title>Attach en un php</title>
</head>
<body>
<title>Titulo de la pagina</title><form enctype="multipart/form-data" name=doit action="envio.php" method=POST>
<table cellpadding=0 cellspacing=0>
<tr><td><b>Tu e-mail:</b></td><td><input type="text" name="from" size=40></td></tr>
<tr><td><b>Para:</b></td><td><input type="text" name="to" size=40></td></tr>
<tr><td><b>Fichero:</b></td><td><input type=file name="attach" size=40></td></tr>
<tr><td><b>Asunto:</b></td><td><input type="text" name="subject" size=40>
</td></tr></table>
Mensaje:<br>
<textarea name="body" rows=2 cols=10 wrap="message">
</textarea>
<input type="submit" name="sendmail" value="Send">
</form>
<?
$mailheaders = "Return-Path: $fromn";
$mailheaders .= "From: $fromn";
$mailheaders .= "Reply-To: $fromn";
$mailheaders .= "X-Mailer: Support byt ark-angeln";
$msg_body = stripslashes($body);
if ($attach != "none")
{
$file = fopen($attach, "r");
$contents = fread($file, $attach_size);
$encoded_attach = chunk_split(base64_encode($contents));
fclose($file);
$mailheaders .= "MIME-version: 1.0n";
$mailheaders .= "Content-type: multipart/mixed; ";
$mailheaders .= "boundary="Message-Boundary"n";
$mailheaders .= "Content-transfer-encoding: 7BITn";
$mailheaders .= "X-attachments: $attach_name";
$body_top = "--Message-Boundaryn";
$body_top .= "Content-type: text/plain; charset=US-ASCIIn";
$body_top .= "Content-transfer-encoding: 7BITn";
$body_top .= "Content-description: Mail message bodynn";
$msg_body = $body_top . $msg_body;
$msg_body .= "nn--Message-Boundaryn";
$msg_body .= "Content-type: $attach_type; name="$attach_name"n";
$msg_body .= "Content-Transfer-Encoding: BASE64n";
$msg_body .= "Content-disposition: attachment; filename="$attach_name"nn";
$msg_body .= "$encoded_attachn";
$msg_body .= "--Message-Boundary--n";
}
$extension = explode(".",$attach_name);
$num = count($extension)-1;
if($extension[$num] == "jpg")
{
if($attach_size < 40000)
{
$extra="-f".$from;
mail($to, stripslashes($subject), $msg_body, $mailheaders, $extra);
echo "<h3> Bueno parece que has conseguido enviar el mail </h3>";
}
else echo "<h3> El tama帽o del archivo sobrepasa el l铆mite de 40Kb </h3>";
}
else echo "<h3> Solo se admiten archivos en formato JPG </h3>";
?>
</body>
</html>
FIN
te dejo el codigo completo de un formulario. Copialo a un archivo de texto y llamalo envio.php, solo toma los jpg y le podes limitar el tama帽o maximo:
<html>
<head>
<title>Attach en un php</title>
</head>
<body>
<title>Titulo de la pagina</title><form enctype="multipart/form-data" name=doit action="envio.php" method=POST>
<table cellpadding=0 cellspacing=0>
<tr><td><b>Tu e-mail:</b></td><td><input type="text" name="from" size=40></td></tr>
<tr><td><b>Para:</b></td><td><input type="text" name="to" size=40></td></tr>
<tr><td><b>Fichero:</b></td><td><input type=file name="attach" size=40></td></tr>
<tr><td><b>Asunto:</b></td><td><input type="text" name="subject" size=40>
</td></tr></table>
Mensaje:<br>
<textarea name="body" rows=2 cols=10 wrap="message">
</textarea>
<input type="submit" name="sendmail" value="Send">
</form>
<?
$mailheaders = "Return-Path: $fromn";
$mailheaders .= "From: $fromn";
$mailheaders .= "Reply-To: $fromn";
$mailheaders .= "X-Mailer: Support byt ark-angeln";
$msg_body = stripslashes($body);
if ($attach != "none")
{
$file = fopen($attach, "r");
$contents = fread($file, $attach_size);
$encoded_attach = chunk_split(base64_encode($contents));
fclose($file);
$mailheaders .= "MIME-version: 1.0n";
$mailheaders .= "Content-type: multipart/mixed; ";
$mailheaders .= "boundary="Message-Boundary"n";
$mailheaders .= "Content-transfer-encoding: 7BITn";
$mailheaders .= "X-attachments: $attach_name";
$body_top = "--Message-Boundaryn";
$body_top .= "Content-type: text/plain; charset=US-ASCIIn";
$body_top .= "Content-transfer-encoding: 7BITn";
$body_top .= "Content-description: Mail message bodynn";
$msg_body = $body_top . $msg_body;
$msg_body .= "nn--Message-Boundaryn";
$msg_body .= "Content-type: $attach_type; name="$attach_name"n";
$msg_body .= "Content-Transfer-Encoding: BASE64n";
$msg_body .= "Content-disposition: attachment; filename="$attach_name"nn";
$msg_body .= "$encoded_attachn";
$msg_body .= "--Message-Boundary--n";
}
$extension = explode(".",$attach_name);
$num = count($extension)-1;
if($extension[$num] == "jpg")
{
if($attach_size < 40000)
{
$extra="-f".$from;
mail($to, stripslashes($subject), $msg_body, $mailheaders, $extra);
echo "<h3> Bueno parece que has conseguido enviar el mail </h3>";
}
else echo "<h3> El tama帽o del archivo sobrepasa el l铆mite de 40Kb </h3>";
}
else echo "<h3> Solo se admiten archivos en formato JPG </h3>";
?>
</body>
</html>
FIN