I am currently building a php upload page which allows the user to upload 3 images and also upload details. I have managed to get the image upload working I think, but could somebody show me how to add 5 more form sections for price, colour, size, age & description that could be emailed to me with the images when they upload to their folder on my server? Thank you my coding so far is below.
<?php
// filename: upload.form.php
// first let's set some variables
// make a note of the current working directory relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
// make a note of the location of the upload handler
$uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.processor.php';
// set a max file size for the html upload form
$max_file_size = 30000; // size in bytes
// now echo the html page
?>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<title>Upload form</title>
</head>
<body id="body">
<form id="Upload" action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">
<h1>
Sell-A-Car
</h1>
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
</p>
<p>
<label for="file1">Item Image 1:</label>
<input id="file1" type="file" name="file[]">
</p>
<p>
<label for="file2">Item image 2:</label>
<input id="file2" type="file" name="file[]">
</p>
<p>
<label for="file3">Item image 3:</label>
<input id="file3" type="file" name="file[]">
</p>
<p>
<label for="submit">Press to...</label>
<input id="submit" type="submit" name="submit" value="SELL IT!">
</p>
</form>
</body>
</html>
second php file is
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 100000) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
You already have the correct enctype
on the form, you simply need to add the required text or whatever else inputs to your form. For example:
<input type="text" name="myTextField"/>
And then in your PHP you could access it like this:
$myTextFieldValue = $_POST['myTextField'];
If you're looking to attach the images, I would suggest taking a look at PHPMailer. It has built in attachment functionality. So on your second page your code would look something like:
include("class.phpmailer.php");
include("class.smtp.php");
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = 'yourmailserver';
$mail->Username = 'user';
$mail->Password = 'pw';
$mail->Timeout = 60;
$mail->From = 'no-reply@yourdomain.com';
$mail->AddAddress($data['email']);
foreach($_FILES["file"] as $f){
$mail->AddAttachment($f['tmp_name']);
}
$mail->Subject = "Subject";
$mail->Body = "Price" . $_POST['price']"
";
$mail->Body .= "Color" . $_POST['color']"
";
$mail->Body .= "Size" . $_POST['size']"
";
if(!$mail->Send()) {
$mail->SMTPDebug = true;
$mail->Debugoutput = 'echo';
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
And make sure you add the parameters in your fist page html something like:
<p>
<label for="color">Color:</label>
<input id="color" type="text" name="color">
</p>