jhaydon
18 May 2011, 06:00 PM
I have a php form which allows uploading files. However, the files go to the server and I need them to go with the rest of the form via email to the specified email address. Can someone please help?
Here is the code I am currently using, and just need to add a section to have it send the uploaded file with the email:
<?php
//--------------------------Set these paramaters--------------------------
// Subject of email sent to you.
$subject = 'Quote Form';
// Your email address. This is where the form information will be sent.
$emailadd = '';
// Where to redirect after form is processed.
$url = 'l';
// Where to direct file upload.
$file= '';
// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';
// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';{
}
if ((($_FILES["file"]["type"] == "application/ai")
|| ($_FILES["file"]["type"] == "application/postscript")
|| ($_FILES["file"]["type"] == "application/cdr")
|| ($_FILES["file"]["type"] == "application/dxf")
|| ($_FILES["file"]["type"] == "applicationj/eps")
|| ($_FILES["file"]["type"] == "application/pdf"))
&& ($_FILES["file"]["size"] < 900000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
I have removed the email address and URL in the code for posting on this forum. I am fairly new to PHP and any help would be greatly appreciated! Thank you.
Here is the code I am currently using, and just need to add a section to have it send the uploaded file with the email:
<?php
//--------------------------Set these paramaters--------------------------
// Subject of email sent to you.
$subject = 'Quote Form';
// Your email address. This is where the form information will be sent.
$emailadd = '';
// Where to redirect after form is processed.
$url = 'l';
// Where to direct file upload.
$file= '';
// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';
// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';{
}
if ((($_FILES["file"]["type"] == "application/ai")
|| ($_FILES["file"]["type"] == "application/postscript")
|| ($_FILES["file"]["type"] == "application/cdr")
|| ($_FILES["file"]["type"] == "application/dxf")
|| ($_FILES["file"]["type"] == "applicationj/eps")
|| ($_FILES["file"]["type"] == "application/pdf"))
&& ($_FILES["file"]["size"] < 900000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
I have removed the email address and URL in the code for posting on this forum. I am fairly new to PHP and any help would be greatly appreciated! Thank you.