多个附件中的FileSize

时间:2012-03-28 16:29:56

标签: php javascript upload phpmailer

我正在发送带有电子邮件的多个附件,现在我想检查附加文件的扩展名和大小,我正在使用该代码:

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 []是一个arrray ..如何立即检查数组类型的文件大小和扩展名表单代码是

<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>

1 个答案:

答案 0 :(得分:1)

这是使用脚本获取文件大小和扩展名的方法

$filesize = $_FILES['attachment']['size'][$key];
$extention = pathinfo ($_FILES['attachment']['name'][$key] ,PATHINFO_EXTENSION );

有关信息,请参阅

http://php.net/manual/en/reserved.variables.files.php

http://php.net/manual/en/function.pathinfo.php

由于

:)