【发布时间】:2012-03-28 16:29:55
【问题描述】:
我正在使用电子邮件发送多个附件,现在我想检查附件的扩展名和大小,为此我使用代码:
foreach(array_keys($_FILES['attachment']['name']) as $key) {
$file=$_FILES['attachment'];
$filesize = (filesize($file) * .0009765625) * .0009765625;
echo $filesize;
if ($FileSize > 100) {
echo "<script type='text/javascript'>parent.document.getElementById('sizeerrormessage').style.display = 'inline';</script>";
}
$source = $_FILES['attachment']['tmp_name'][$key];
$filename = $_FILES['attachment']['name'][$key];
$mail->AddAttachment($source, $filename);
}
我给出了计算文件大小和扩展名的错误,给出了一个数组,需要字符串。
对于多个附件,attachment[] 是一个数组。现在如何检查数组类型的文件大小和扩展名表单代码是
<input type="file" name="attachment[]" id="attachment" size="30"
onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
<div id="moreUploads"></div>
<div id="sizeerrormessage" style="display:none;margin-left:30px">
<font color=#990000 size=1>File exceeded maximum allowed limit of 100 Kb</font>
</div>
<div id="typeerrormessage" style="display:none;margin-left:30px">
<font color=#990000 size=1>Only png, jpg and gif are allowed extensions.</font>
</div>
<div id="moreUploadsLink" style="display:none;"><a
href="javascript:addFileInput();">Attach another File</a></div>
而 javascript 是
<script type="text/javascript">
var upload_number = 1;
var attachmentlimit = 5;
function addFileInput() {
var d = document.createElement("div");
var file = document.createElement("input");
file.setAttribute("type", "file");
file.setAttribute("name", "attachment[]");
/* file.setAttribute("name", "attachment"+upload_number);*/
d.appendChild(file);
document.getElementById("moreUploads").appendChild(d);
upload_number++;
if(upload_number == attachmentlimit) {
document.getElementById('moreUploadsLink').style.display='none';
}
}
</script>
【问题讨论】:
标签: php javascript upload phpmailer