Upload + Mail (Anexo)
Olá pessoal, tenho pesquisado para tranformar um formulário que eu tenho em um formulário com upload de arquivos, só que estou apanhando um pouco. Vou colocar o código aqui do formulário e da função que ele chama para vocês me darem uma luz:
<form id="form1" name="form1" method="post" action="funcoes.php" enctype="multipart/form-data">
<table width="540" border="0" class="Calibri">
<tr>
<td width="653"><label>
<strong>Título do Trabalho:</strong>
<input name="trabalho" type="text" id="trabalho" size="50" />
</label></td>
</tr>
<tr>
<td><strong>Autor:
<label>
<input name="autor" type="text" id="autor" size="50" />
</label>
RG:</strong>
<input name="rgautor" type="text" id="rgautor" size="8" maxlength="8" /></td>
</tr>
<tr>
<td><strong>Co-Autor 1:</strong>
<label>
<input name="co1" type="text" id="co1" size="50" />
<strong>RG:</strong>
<input name="rgco1" type="text" id="rgco1" size="8" maxlength="8" />
(Opcional)</label></td>
</tr>
<tr>
<td><strong>Co-Autor 2:</strong>
<label>
<input name="co2" type="text" id="co2" size="50" />
<strong>RG:</strong>
<input name="rgco2" type="text" id="rgco2" size="8" maxlength="8" />
(Opcional)</label></td>
</tr>
<tr>
<td><strong>Co-Autor 3:</strong>
<label>
<input name="co3" type="text" id="co3" size="50" />
<strong>RG:</strong>
<input name="rgco3" type="text" id="rgco3" size="8" maxlength="8" />
(Opcional)</label></td>
</tr>
<tr>
<td><strong>Co-Autor 4:</strong>
<label>
<input name="co4" type="text" id="co4" size="50" />
<strong>RG:</strong>
<input name="rgco4" type="text" id="rgco4" size="8" maxlength="8" />
(Opcional)</label></td>
</tr>
<tr>
<td><strong>Instituição:
<label>
<input name="inst" type="text" id="inst" size="70" />
</label>
</strong></td>
</tr>
<tr>
<td><strong>Área:
<label>
<select name="area" id="area">
<option selected="selected">Selecione a área do seu trabalho...</option>
<option value="Geriatria">Geriatria</option>
<option value="Dermatofuncional">Dermatofuncional</option>
<option value="Saúde Coletiva">Saúde Coletiva</option>
<option value="Ortopedia/Traumatologia/Reumatologia">Ortopedia/Traumatologia/Reumatologia</option>
<option value="Neurofuncional">Neurofuncional</option>
<option value="Cardiopulmonar">Cardiopulmonar</option>
<option value="Terapias Manuais e Holísticas">Terapias Manuais e Holísticas</option>
<option value="Saúde da Mulher">Saúde da Mulher</option>
<option value="Outras áreas">Outras áreas</option>
</select>
</label>
</strong></td>
</tr>
<tr>
<td><strong>Cópia Original:
<label></label>
<input name="original" type="file" id="original" size="50" />
</strong></td>
</tr>
<tr>
<td><strong>Cópia Cega:
<label>
<input name="ccega" type="file" id="ccega" size="50" />
</label>
</strong></td>
</tr>
<tr>
<td><strong>Email para contato:</strong>
<input name="email" type="text" class="Centralizar" id="email" size="50" /></td>
</tr>
</table>
<p align="center">
<input type="submit" name="button" id="button" value="Enviar" />
</p>
</form>
FUNÇÃO QUE O FORM CHAMA (Tá sem todas as variáveis só para teste):
<?
header("Content-Type: text/html; charset=UTF-8", true);
include("global.inc.php");
pt_register('POST','titulo');
pt_register('POST','autor');
pt_register('POST','rgautor');
pt_register('POST','co1');
pt_register('POST','co2');
pt_register('POST','co3');
pt_register('POST','co4');
pt_register('POST','rgco1');
pt_register('POST','rgco2');
pt_register('POST','rgco3');
pt_register('POST','rgco4');
pt_register('POST','inst');
pt_register('POST','area');
pt_register('POST','original');
pt_register('POST','ccega');
pt_register('POST','email');
header("Content-Type: text/html; charset=utf-8");
$boundary = "XYZ-" . date(dmyhms) . "-ZYX";
$formato = "MIME-Version: 1.0\r\n";
$formato .= "Content-type: multipart/mixed; boundary=\"$boundary\"\r\n";
$formato .= "From: ".$email." \r\n";
$msg .= "Content-type: text/html; charset=UTF-8\r\n";
$msg .= "Content-Transfer-Encoding: 8bits\n";
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$msg .= "<b>Trabalho:</b> $trabalho<br>";
$msg .= "<b>RG:</b> $rgautor<br>";
$msg .= "<b>Autor:</b> $autor<br>";
$msg .= "<b>Email:</b> $email<br>";
$msg .= "--$boundary--\r\n";
$attachments[1] = $original;
$attachments[2] = $ccega;
foreach ($attachments as $key => $full_path) {
if ($full_path !='') {
if (file_exists($full_path)){
//try to open
if ($fp = fopen($full_path,"rb")) {
//get the file name from the path
$filename = array_pop(explode(chr(92),$full_path));
$contents = fread($fp,filesize($full_path));
//encode data
$encoded = base64_encode($contents);
//*****SPLIT THE ENCODED DATA*****
$encoded_split = chunk_split($encoded);
fclose($fp);
$message .= "--$boundary\n";
$message .= "Content-Type: $anexo_type\n";
$message .= "Content-Disposition: attachment; filename=\"$anexo_name\" \n";
$message .= "Content-Transfer-Encoding: base64\n\n";
$message .= "$encoded_split\n";
}
else {
echo "Cannot open file$key: $filename";
$ABORT = TRUE;
}
}
else {
echo "File$key does not exist: $filename";
$ABORT = TRUE;
}
}
}
$envio = mail("trabalhos@******.com.br", "Formulário de Inscrição", $msg, $formato);
if ($envio) {
echo "
<META HTTP-EQUIV=REFRESH CONTENT='0; URL=http://******.com.br'>
<script type=\"text/javascript\">
alert(\"Trabalho inscrito com sucesso!\");
window.close();
</script>
";
} else {
echo "
<script type=\"text/javascript\">
alert(\"A inscrição do trabalho falhou, tente novamente.\");
window.close();
</script>
";
}
?>Discussão (1)
Carregando comentários...