私はPHPのアップロードとメール機能で忙しくしています。基本的には、アップロードされた画像を添付ファイルとして電子メールに入れ、ディレクトリにアップロードします。日付に応じて画像の名前を変更し、拡張子を保持するにはどうすればよいですか?人々は同じファイル名で画像をアップロードする可能性があります...
アップロード機能は次のとおりです。
$newdirectory = "applicants";
$count = 0;
foreach ($_FILES['myfile']['name'] as $filename)
{
$temp = $_FILES['myfile']['tmp_name'][$count];
move_uploaded_file($temp, $newdirectory . '/' . $filename);
$count++;
}
そしてphp関数全体:
<?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.";
}
?>