<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[form contatti con allegato: problemi con estensione allegato]]></title><description><![CDATA[<p dir="auto">Buonasera a tutti!<br />
Premettendo che sono una capra con il PHP (parlatemi come avessi 2 anni :)!), mi serviva un form con dei campi+allegato che una volta inviati, andassero dritti ad un indirizzo mail.</p>
<p dir="auto">Fin qui, ho trovato ciò che cercavo e l'ho fatto funzionare (anche se ciò che ricevo mi và nella posta indesiderata, c'è un modo per evitare ciò?)...ma...</p>
<p dir="auto">Vorrei mettere una restrizione per quanto riguarda le estensioni del file che si può allegare (che si possano allegare solo zip e rar)..</p>
<p dir="auto">Vi allego il codice, la stringa della restrizione c'è ma non và, riesco a spedire anche i jpeg ad esempio....</p>
<pre><code>// prints form
function print_form(){
?&gt;
	&lt;form method="post" action="&lt;?php echo $_SERVER[?PHP_SELF?];?&gt;" id="form" enctype="multipart/form-data"&gt;
	&lt;label for="nome"&gt;nome / nome gruppo&lt;/label&gt;
	&lt;input name="nome" id="nome" type="text" value="&lt;?= $_SESSION['myForm']['nome']; ?&gt;" tabindex="1"/&gt;
	
	&lt;label for="email"&gt;e-mail&lt;/label&gt;
	&lt;input name="email" id="email" type="text" value="&lt;?= $_SESSION['myForm']['email']; ?&gt;" tabindex="2"/&gt;
	
	&lt;label for="citta"&gt;città&lt;/label&gt;
	&lt;input name="citta" id="citta" type="text" value="&lt;?= $_SESSION['myForm']['citta']; ?&gt;" tabindex="3"/&gt;
	
	&lt;label for="titolo"&gt;titolo opera&lt;/label&gt;
	&lt;input name="titolo" id="titolo" type="text" value="&lt;?= $_SESSION['myForm']['titolo']; ?&gt;" tabindex="4"/&gt;
	
	&lt;label for="descrizione"&gt;breve descrizione dell'opera&lt;/span&gt;&lt;/label&gt;
	&lt;textarea name="descrizione" id="descrizione" cols="45" rows="7" tabindex="5"/&gt;&lt;?= $_SESSION['myForm']['descrizione']; ?&gt;&lt;/textarea&gt;
	
	&lt;label for="scheda"&gt;scheda tecnica&lt;/label&gt;
	&lt;textarea name="scheda" id="scheda" rows="7" cols="45" tabindex="6"&gt;&lt;?= $_SESSION['myForm']['scheda']; ?&gt;&lt;/textarea&gt;
    
    &lt;label for="curriculum"&gt;breve curriculum&lt;/label&gt;
	&lt;textarea name="curriculum" id="curriculum" rows="7" cols="45" tabindex="7"&gt;&lt;?= $_SESSION['myForm']['curriculum']; ?&gt;&lt;/textarea&gt;
	
	&lt;label for="allegato"&gt;carica immagine della tua opera e la liberatoria firmata che trovi nel bando (&lt;a class="black" href="02_01.html"&gt;qui&lt;/a&gt;)&lt;br /&gt;
&lt;span class="grey"&gt;(sono ammessi solo file .zip&lt;br /&gt;o .rar, max 3mb)&lt;/span&gt;&lt;/label&gt;
	&lt;input name="allegato" id="allegato" type="file" tabindex="8"&gt;
	
	&lt;input class="pulsante" type="submit" name="submit" id="submit" value="INVIA"  tabindex="9"/&gt;
	&lt;input type="hidden" name="submitted"  value="true" /&gt;
	&lt;/form&gt;
&lt;?php
}

// enquiry form validation

function process_form() {
	// Read POST request params into global vars
	// FILL IN YOUR EMAIL
	$to = "indirizzoalqualearriverà@mail dot it";
	$subject = 'nuovo iscritto al contest';
	$nome = trim($_POST['nome']);
	$email = trim($_POST['email']);
	$citta = trim($_POST['citta']);
	$titolo = trim($_POST['titolo']);
	$descrizione = trim($_POST['descrizione']);
	$scheda = trim($_POST['scheda']);
	$curriculum = trim($_POST['curriculum']);
	
	
	// Require a file to be attached: false = Do not allow attachments true = allow only 1 file to be attached
	$requirefile="true";
	
	// Allowed file types. add file extensions WITHOUT the dot.
	$allowtypes=array("zip", "rar");
	
	// Require a file to be attached: false = Do not allow attachments true = allow only 1 file to be attached
	$requirefile="true";
	
	// Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it,
	// post_max_size, upload_max_filesize, file_uploads, max_execution_time!
	// 2048kb = 2MB,       1024kb = 1MB,     512kb = 1/2MB etc..
	$max_file_size="3072";
	$max_execution_time="360";
	
	// Thank you message
	$thanksmessage="Grazie per aver partecipato! Riceverai a breve la nostra conferma di avvenuta ricezione del materiale. In caso contrario, mandaci una mail. &lt;hr /&gt;";

	$errors = array(); //Initialize error array

	//checks for a nome
	if (empty($_POST['nome']) ) {
		$errors[]='Hai dimenticato di inserire il tuo nome o il nome del gruppo.';
		}

	//checks for an email
	if (empty($_POST['email']) ) {
		$errors[]='Hai dimenticato di inserire il tuo indirizzo e-mail.&lt;hr /&gt;';
		} else {

		if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['email'])))) {
			$errors[]='Per favore, inserisci un indirizzo mail valido.';
		} // if eregi
	} // if empty email

	//checks for a citta
	if (empty($_POST['citta']) ) {
		$errors[]='Hai dimenticato di inserire la tua città di provenienza.';
		}

	//checks for a titolo
	if (empty($_POST['titolo']) ) {
		$errors[]='Hai dimenticato di inserire il titolo della tua opera.';
		}
		
	//checks for a descrizione
	if (empty($_POST['descrizione']) ) {
		$errors[]='Hai dimenticato di inserire una descrizione della tua opera.';
		}	
		
		//checks for a scheda
	if (empty($_POST['scheda']) ) {
		$errors[]='Hai dimenticato di inserire una scheda della tua opera.';
		}	
		
		//checks for a curriculum
	if (empty($_POST['curriculum']) ) {
		$errors[]='Hai dimenticato di inserire il tuo curriculum.';
		}	
					
		
 	// checks for required file
	// htt://amiworks.co.in/talk/handling-file-uploads-in-php/
	if($requirefile=="true") {
		if($_FILES['allegato']['error']==4) {
			$errors[]='Hai dimenticato di allegare la tua opera.';
		}
	}
		
	//checks attachment file
	// checks that we have a file
	if((!empty($_FILES["allegato"])) &amp;&amp; ($_FILES['allegato']['error'] == 0)) {
			// basename -- Returns filename component of path
			$filename = basename($_FILES['allegato']['nome']);
			$ext = substr($filename, strrpos($filename, '.') + 1);
			$filesize=$_FILES['allegato']['size'];
			$max_bytes=$max_file_size*1024;
			
			
			if($filesize &gt; $max_bytes) {
				$errors[]= "Il tuo file è troppo grande. La dimensione max consentita è di 3mb!";
			}
			
	} // if !empty FILES

	if (empty($errors)) { //If everything is OK
		
		// send an email
		// Obtain file upload vars
		$fileatt      = $_FILES['allegato']['tmp_name'];
		$fileatt_type = $_FILES['allegato']['type'];
		$fileatt_name = $_FILES['allegato']['name'];
		
		// Headers
		$headers = "From: $emailfrom";
		
		// create a boundary string. It must be unique
		  $semi_rand = md5(time());
		  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

		  // Add the headers for a file attachment
		  $headers .= "\nMIME-Version: 1.0\n" .
		              "Content-Type: multipart/mixed;\n" .
		              " boundary=\"{$mime_boundary}\"";

		  // Add a multipart boundary above the plain message
		  $message ="This is a multi-part message in MIME format.\n\n";
		  $message.="--{$mime_boundary}\n";
		  $message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
		  $message.="Content-Transfer-Encoding: 7bit\n\n";
		  $message.="Da: ".$nome."\n";
		  $message.="E-mail: ".$email."\n";
		  $message.="Città: ".$citta."\n";
		  $message.="Titolo opera: ".$titolo."\n";
		  $message.="Descrizione: ".$descrizione."\n";
		  $message.="Scheda tecnica: ".$scheda."\n";
		  $message.="Curriculum: ".$curriculum."\n\n";
		
		if (is_uploaded_file($fileatt)) {
		  // Read the file to be attached ('rb' = read binary)
		  $file = fopen($fileatt,'rb');
		  $data = fread($file,filesize($fileatt));
		  fclose($file);

		  // Base64 encode the file data
		  $data = chunk_split(base64_encode($data));

		  // Add file attachment to the message
		  $message .= "--{$mime_boundary}\n" .
		              "Content-Type: {$fileatt_type};\n" .
		              " name=\"{$fileatt_name}\"\n" .
		              //"Content-Disposition: attachment;\n" .
		              //" filename=\"{$fileatt_name}\"\n" .
		              "Content-Transfer-Encoding: base64\n\n" .
		              $data . "\n\n" .
		              "--{$mime_boundary}--\n";
		}
		
		
		// Send the completed message
		
		$envs = array("HTTP_USER_AGENT", "REMOTE_ADDR", "REMOTE_HOST");
		foreach ($envs as $env)
		$message .= "$env: $_SERVER[$env]\n";
		
		if(!mail($to,$subject,$message,$headers)) {
			exit("Spiacenti, ma non siamo riusciti a spedire la tua candidatura. Per favore, riprova o spediscici il tutto via mail, segnalandoci il problema.\n");
		} else {
			echo '&lt;div id="formfeedback"&gt;&lt;h3&gt;Grazie!&lt;/h3&gt;&lt;p&gt;'. $thanksmessage .'&lt;/p&gt;&lt;/div&gt;';
			unset($_SESSION['myForm']);
			print_form();
			
		} // end of if !mail
		
	} else { //report the errors
		echo '&lt;div id="formfeedback"&gt;&lt;h3&gt;Errore!&lt;/h3&gt;&lt;p&gt;Si sono riscontrati i seguenti errori:&lt;br /&gt;';
		foreach ($errors as $msg) { //prints each error
				echo " - $msg&lt;br /&gt;\n";
			} // end of foreach
		echo '&lt;/p&gt;&lt;p&gt;Riprova&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;';
		print_form();
	} //end of if(empty($errors))

} // end of process_form()
?&gt;
</code></pre>
<p dir="auto">Altra cosa, per la limitazione del peso, vorrei far sì che l'annuncio del file troppo pesante compaia subito appena si tenta di caricare il file, e non una volta a file caricato...</p>
<p dir="auto">Chiedo troppo?<br />
<img src="https://dev.connect.gt/assets/plugins/nodebb-plugin-emoji/emoji/android/1f604.png?v=2190ae3dadd" class="not-responsive emoji emoji-android emoji--smile" style="height:1.4em;width:auto;vertical-align:middle" title=":D" alt="😄" /></p>
]]></description><link>https://dev.connect.gt/topic/168177/form-contatti-con-allegato-problemi-con-estensione-allegato</link><generator>RSS for Node</generator><lastBuildDate>Sat, 28 Nov 2026 19:20:00 GMT</lastBuildDate><atom:link href="https://dev.connect.gt/topic/168177.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 18 Mar 2012 19:23:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to form contatti con allegato: problemi con estensione allegato on Mon, 19 Mar 2012 10:36:48 GMT]]></title><description><![CDATA[<p dir="auto">Ho provato ma non và....ora non mi accetta nemmeno zip e rar, in pratica non accetta nulla e dà sempre errore di estensione <img src="https://dev.connect.gt/assets/plugins/nodebb-plugin-emoji/emoji/android/1f61e.png?v=2190ae3dadd" class="not-responsive emoji emoji-android emoji--disappointed" style="height:1.4em;width:auto;vertical-align:middle" title=":(" alt="😞" /></p>
]]></description><link>https://dev.connect.gt/post/1059319</link><guid isPermaLink="true">https://dev.connect.gt/post/1059319</guid><dc:creator><![CDATA[peekaboo]]></dc:creator><pubDate>Mon, 19 Mar 2012 10:36:48 GMT</pubDate></item><item><title><![CDATA[Reply to form contatti con allegato: problemi con estensione allegato on Mon, 19 Mar 2012 10:15:18 GMT]]></title><description><![CDATA[<p dir="auto">Ciao,<br />
basta aggiungere sotto:</p>
<pre><code class="language-php">$ext =  substr($filename, strrpos($filename, '.') + 1);
</code></pre>
<p dir="auto">Questo:</p>
<pre><code class="language-php">if(!in_array($ext, $allowtypes))
    $errors[]='Estensione file non valida';
</code></pre>
]]></description><link>https://dev.connect.gt/post/1059311</link><guid isPermaLink="true">https://dev.connect.gt/post/1059311</guid><dc:creator><![CDATA[thedarkita]]></dc:creator><pubDate>Mon, 19 Mar 2012 10:15:18 GMT</pubDate></item></channel></rss>