Email attachment form and folder upload

Hi! I have an email form and I need to add the option of attachments to it. If possible I would like for the attachments (up to 10) to be uploaded to a folder and for the email to send the file names along with the rest of the information from the form. This means that there should also be a way to automatically rename the pictures so that they don’t replace pictures already in the folder.

If this is too complicated, then as an alternative I would like to add the attachment options to the email form so that it sends the pictures along with the rest of the information. Also, the email form currently sends codes, no any method used to send it (if it must absolutely be remade) should be able to handle sending code. I prefer the first option, but I’ll settle for the second if the first is not possible. Thanks for your help!

Hi Milia,

The first option is perfectly feasible. You can find some useful examples to get you started with uploading files here in the PHP manual. Example 3 specifically deals with uploading multiple files. Your suggestion about avoiding duplicate file names is a good one - what you could do is use the [fphp]md5[/fphp] function to hash the user’s email address, generating a unique string to use as a folder name to store the user’s uploaded files.

That should point you in the right direction, but if you get stuck with any aspect then please post back and I (or another member) will be glad to help.

Thanks for the help! I updated the form but the files still won’t upload. The form is

 enctype="multipart/form-data" 

like it says on the page.

I tried to test it by emailing the files to see if I could get it to work from there, tried different methods, but the files wouldn’t send. I already have a directory chmod to 777 for the files to be saved at and the direct path, though I can’t seem to get to that step. :frowning: I’ve tried all all of these (separately)

 $sfile = $_FILES['userfile']; 

$sfile = $_POST['userfile']; 

$_FILES = $_POST['userfile']; 

Could you post the HTML for your form, and the PHP code that processes it?

 <?php include("header.php"); ?>

<form enctype="multipart/form-data" name="mailthis" action="mailer.php" method="post"> 

<table><tr><td>

<select class="fill" id="uraza" name="uraza">
<option value="raza" class="fill">Raza</option>
<option value="bulldog" class="fill">Bulldog</option>
<option value="terrier" class="fill">Terrier</option>
<option value="shepherd" class="fill">Shepherd</option>
<option value="boxer" class="fill">Boxer</option>
<option value="chihuahua" class="fill">Chihuahua</option>
<option value="labrador" class="fill">Labrador</option>
<option value="husky" class="fill">Husky</option>
<option value="schnauzer" class="fill">Schnauzer</option>
<option value="dalmatian" class="fill">Dalmatian</option>
<option value="shihtzu" class="fill">ShihTzu</option>
<option value="retriever" class="fill">Retriever</option>
<option value="otro" class="fill">Otro</option>
</select> 
<br /><br />

<input class="fill" name="ucost" id="ucost" value="Precio: " type="text" />
<br /><br />

<input class="fill" name="uname" id="uname" value="Nombre: " type="text" />
<br /><br />

<input class="fill" name="umail" id="umail" value="Email: " type="text" /> 
<br /><br />

<input class="fill" name="urtel" id="urtel" value="Telefono: " type="text" /> 
<br /><br />

<input class="fill" name="utown" id="utown" value="Pueblo: " type="text" /> 
<br /><br />

<textarea class="fill" name="utalk" id="utalk" rows="3" cols="50">Descripcion: </textarea> 

</td><td> <div style="border:0px none; width:50px;"> &nbsp; </div> </td><td>

<input type="hidden" name="MAX_FILE_SIZE" value="15700700" />
Enviar foto 1: <input name="userfile[]" type="file" class="upfiles" />
<br />

<input type="hidden" name="MAX_FILE_SIZE" value="15700700" />
Enviar foto 2: <input name="userfile[]" type="file" class="upfiles" />
<br />

<input type="hidden" name="MAX_FILE_SIZE" value="15700700" />
Enviar foto 3: <input name="userfile[]" type="file" class="upfiles" />
<br />

<input type="hidden" name="MAX_FILE_SIZE" value="15700700" />
Enviar foto 4: <input name="userfile[]" type="file" class="upfiles" />
<br />

<input type="hidden" name="MAX_FILE_SIZE" value="15700700" />
Enviar foto 5: <input name="userfile[]" type="file" class="upfiles" />
<br />

<input type="hidden" name="MAX_FILE_SIZE" value="15700700" />
Enviar foto 6: <input name="userfile[]" type="file" class="upfiles" />
<br />

<input type="hidden" name="MAX_FILE_SIZE" value="15700700" />
Enviar foto 7: <input name="userfile[]" type="file" class="upfiles" />
<br />

<input type="hidden" name="MAX_FILE_SIZE" value="15700700" />
Enviar foto 8: <input name="userfile[]" type="file" class="upfiles" />
<br />

<input type="hidden" name="MAX_FILE_SIZE" value="15700700" />
Enviar foto 9: <input name="userfile[]" type="file" class="upfiles" />
<br />

<input type="hidden" name="MAX_FILE_SIZE" value="15700700" />
Enviar foto 10: <input name="userfile[]" type="file" class="upfiles" />

</td></tr> <tr> <td colspan="3" style="text-align:center;">

<input type="submit" style="margin:0px auto 0px auto;" value="Enviar" id="sendform" class="clickit" />

</td> </tr> </table> </form>

<?php include("footer.php"); ?> 
 <?php 

$uploaddir = '/users/procomics.net/htdocs/tfa/codegen/image/userpic/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

$sraza = $_POST['uraza']; 

$scost = $_POST['ucost']; 

$sname = $_POST['uname']; 

$smail = $_POST['umail']; 

$setel = $_POST['urtel']; 

$stown = $_POST['utown']; 

$stalk = $_POST['utalk']; 

$sfile = $_FILES['userfile']; 

$to = 'pixelmi@yahoo.com';

$email_subject = "The Fish Aquarium";
	
$email_body = "<!-- START --> \
 <!-- Replace 000 with a number up to 100 and set the expiration date. --> \
 \
 <?php \\$dogid = \\"000\\"; \
 \
 \\$exp_date = \\"2000-01-25\\"; \
 \
 \\$todays_date = date(\\"Y-m-d\\"); \\$today = strtotime(\\$todays_date); \\$expiration_date = strtotime(\\$exp_date); \
 if (\\$expiration_date < \\$today) { \\$show = \\" style=\\\\\\"display:none;\\\\\\"\\"; } \
 else { \\$show = \\" style=\\\\\\"display:block;\\\\\\"\\"; } ?> \
 <div id=\\"p<?php echo \\$dogid; ?>\\"<?php echo \\$show; ?>> <div class=\\"schnauzer\\"> \
 <?php \\$dir = \\"image/\\" . \\$dogid . \\"/\\"; \\$images = scandir(\\$dir); \\$i = rand(2, sizeof(\\$images)-1); ?> \
 <img src=\\"image/<?php echo \\$dogid; ?>/<?php echo \\$images[\\$i]; ?>\\" alt=\\"\\" class=\\"preview\\" /> \
 <br /><span class=\\"idnum\\">ID #: </span> <?php echo \\$dogid; ?> \
 <br /><span class=\\"fotos\\"><a href=\\"pics.php?page=<?php echo \\$dogid; ?>\\">Ver mas fotos</a></span> \
 \
 <br /><span class=\\"raza\\"> Raza: $sraza </span> \
 <br /><span class=\\"precio\\"> $scost </span> \
 <br /><span class=\\"nombre\\"> $sname </span> \
 <br /><span class=\\"pueblo\\"> $stown </span> \
 <br /><span class=\\"telefono\\"> $setel </span> \
 <br /><span class=\\"email\\"> $smail </span> \
 <br /><span class=\\"precio\\"> $stalk </span> \
 </div></div> \
 \
 <!-- END --> \
"; 
	
mail($to,$email_subject,$email_body,$sfile);
	
header('Location: anuncio.php'); ?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title> The Fish Aquarium </title>

<link rel="shortcut icon" href="image/site/fish1.ico" />

<style type="text/css">

body { background-color:#002dff; text-align:center; color:#ffffff; cursor:crosshair; font-family: Courier New; text-transform:uppercase; font-size:15px; margin-top:200px; }

a:link, a:visited, a:active { color:#ffffff; font-weight:bold; text-decoration:underline; }
a:hover { color:#ffffff; font-weight:bold; text-decoration:underline; font-style:italic; }

*:focus { outline:0; }

</style> </head> <body>

Blue Screen of DOOM! D:

<br /><br />

Your message was not sent. :( 

<br /><br />

<a href="publi.php">Click here</a> to try again.

</body> </html> 

Hi Milia, thanks for posting your code.

The first thing to mention is, in your form HTML, you only need the MAX_FILE_SIZE input tag once as it applies to all file inputs within the form.

Next, in your mailer.php file, you need to do something like this:


$uploaddir = 'uploads';

$sraza = $_POST['uraza'];
$scost = $_POST['ucost'];
$sname = $_POST['uname'];
$smail = $_POST['umail'];
$setel = $_POST['urtel'];
$stown = $_POST['utown'];
$stalk = $_POST['utalk'];

$userfolder = 'uploads/' . md5($smail);
if (!is_dir($userfolder)) {
    mkdir($userfolder, 0777, true);
}

foreach ($_FILES["userfile"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["userfile"]["tmp_name"][$key];
        $name = $_FILES["userfile"]["name"][$key];
        move_uploaded_file($tmp_name, "$userfolder/$name");
    }
}

// Code to send the email and redirect...

This is based on the example from the PHP manual that I referred you to in my first post, and basically loops through the array of uploaded files checking if they uploaded successfully and moving them into a unique subfolder of the uploads directory.

Be aware that this isn’t a complete example - you still need to validate the input from the form to make sure that users submit sensible data (make sure that they supply an email address, for example).

I also notice that you have PHP code in the body of your email:


$email_body = "<!-- START --> \
 <!-- Replace 000 with a number up to 100 and set the expiration date. --> \
 \
 <?php \\$dogid = \\"000\\"; \
 \
 \\$exp_date = \\"2000-01-25\\"; \
 \
 \\$todays_date = date(\\"Y-m-d\\"); \\$today = strtotime(\\$todays_date); \\$expiration_date = strtotime(\\$exp_date); \
 if (\\$expiration_date < \\$today) { \\$show = \\" style=\\\\\\"display:none;\\\\\\"\\"; } \
 else { \\$show = \\" style=\\\\\\"display:block;\\\\\\"\\"; } ?> \
 <div id=\\"p<?php echo \\$dogid; ?>\\"<?php echo \\$show; ?>> <div class=\\"schnauzer\\"> \
 <?php \\$dir = \\"image/\\" . \\$dogid . \\"/\\"; \\$images = scandir(\\$dir); \\$i = rand(2, sizeof(\\$images)-1); ?> \
 <img src=\\"image/<?php echo \\$dogid; ?>/<?php echo \\$images[\\$i]; ?>\\" alt=\\"\\" class=\\"preview\\" /> \
 <br /><span class=\\"idnum\\">ID #: </span> <?php echo \\$dogid; ?> \
 <br /><span class=\\"fotos\\"><a href=\\"pics.php?page=<?php echo \\$dogid; ?>\\">Ver mas fotos</a></span> \
 \
 <br /><span class=\\"raza\\"> Raza: $sraza </span> \
 <br /><span class=\\"precio\\"> $scost </span> \
 <br /><span class=\\"nombre\\"> $sname </span> \
 <br /><span class=\\"pueblo\\"> $stown </span> \
 <br /><span class=\\"telefono\\"> $setel </span> \
 <br /><span class=\\"email\\"> $smail </span> \
 <br /><span class=\\"precio\\"> $stalk </span> \
 </div></div> \
 \
 <!-- END --> \
";

This won’t work, as email clients don’t execute PHP code, so you won’t get the output you expect.

Thank you! This works great. :slight_smile:

The php in the form isn’t supposed to execute, it’s just a code to be sent as text, so no problem there.