Buongiorno di nuovo. Il mio secondo form (riferimento post precedente) invia gli allegati direttamente per email. L'aiuto che mi serve è sempre per poter caricare più file con un unico campo di input. Ecco il codice che utilizzo e che permette l'upload di un solo file alla volta. Grazie mille ancora per l'aiuto.
PHP:
HTML:
<form class="form-contact" action="upload.php" method="post" enctype="multipart/form-data">
<p><label>Full name:</label>
<br><input class="field" type="text" name="name" required /></p>
<p><label>E-mail:
<br></label><input class="field" type="email" name="email" required /></p>
<p><label>Message: </label>
<br><textarea name="message" required ></textarea></p>
<p><label>File:</label>
<input type="file" name="attachFile" />
</p>
<p class="p-captcha">Antispam: <br><img class="img-captcha" id="captcha" src="./captcha.php" alt="captcha"/>
<a class="a-captcha" onclick="reload()"> ↻</a><br>
<label><span class="span-captcha">Type the numbers</span></label><br>
<input type="text" name="captcha" />
</p>
<div class="button-container">
<input class="contact-button" type="reset" value="Reset">
<input class="contact-button" type="submit" value="Send">
</div>
</form>
PHP:
PHP:
session_start();
if ($_POST['captcha'] != $_SESSION['captcha']) {
echo '<script type="text/javascript">alert("Captcha errato / Incorrect captcha");window.history.go(-1);</script>';
die();
}
//Defining mail settings
$to = "mail@mail";
$subject= "Invio di file / Sending files";
$todayis = date("l, F j, Y, g:i a") ;
$name = $_POST['name'];
$email = $_POST['email'];
$message = ($_POST['message']);
$message = "
Date: $todayis
Name: $name
Email: $email
Message: $message
";
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$headers = "From: $email\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
// impostare il set di caratteri come nella casella di posta
"Content-Type: text/plain; charset=\"utf-8\"\n" .
//...............................................
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
foreach($_FILES as $userfile)
{
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
if (file_exists($tmp_name))
{
if(is_uploaded_file($tmp_name))
{
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
$message.="--{$mime_boundary}--\n";
// Check filesize
if($_FILES['file_upload']['size'] > 500000){
//die('File uploaded exceeds maximum upload size.');
echo '<script type="text/javascript">alert("Il file supera le dimensioni consentite");window.history.go(-1);</script>';
}
if (mail($to, $subject, $message, $headers))
echo '<script type="text/javascript">alert("Successo!");window.history.go(-1);</script>';
exit;
?>