I’m busy with a PHP upload and mail function basically it takes uploaded images and puts it in an email as an attachment and then also uploads it to a directory, how would i rename the image according to the date but keep the extension, for people may upload images with the same file name…
here’s the upload function:
$newdirectory = "applicants";
$count = 0;
foreach ($_FILES['myfile']['name'] as $filename)
{
$temp = $_FILES['myfile']['tmp_name'][$count];
move_uploaded_file($temp, $newdirectory . '/' . $filename);
$count++;
}
and then the entire php function:
<?php
function printMember($member) {
foreach($member as $key=>$value) {
//Fill the aux string first
$str.= "$key : $value <br />";
}
//string that will be added to $msg variable inside the loop
return $str;
}
$json = $_POST['parameters'];
$json_string = stripslashes($json);
$data = json_decode($json_string, true);
$depCount = count($data["dependants"]);
$msg .= "<h2>Main member data:</h2>";
$msg .= printMember($data["mainmember"]);
$msg .= "<h2>There are $depCount Dependants</h2>";
foreach ($data["dependants"] as $index => $dependant) {
$msg .= "<h2>Dependant $index</h2>";
$msg .= printMember($dependant);
}
$strTo = "chante@jamfactory.co.za";
$strSubject = "Image Testing";
$strMessage = nl2br($msg);
//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));
$strHeader = "";
$strHeader .= "From: Dawid<xxx@xxx.co.za>\nReply-To:xxxx@xxx.co.za";
$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";
//*** Attachment ***//
$count = 0;
foreach($_FILES['myfile']['name'] as $filename)
{
$temp = $_FILES['myfile']['tmp_name'][$count];
$strFilesName = $filename;
$strContent = chunk_split(base64_encode(file_get_contents($temp)));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$strHeader .= $strContent."\n\n";
$count++;
}
$flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error //
if($flgSend)
{
echo "Mail send completed.";
$newdirectory = "applicants";
$count = 0;
foreach ($_FILES['myfile']['name'] as $filename)
{
$temp = $_FILES['myfile']['tmp_name'][$count];
move_uploaded_file($temp, $newdirectory . '/' . $filename);
$count++;
}
}
else
{
echo "Cannot send mail.";
}
?>
I’d use
pathinfofor excluding the file extension, plus adding an extratimestampto prevent the file name conflicts and replacing the existing files.