duanliaolan6178 2012-03-28 16:29
浏览 38
已采纳

多个附件中的FileSize

I am sending multiple attachments with an Email Message, Now i want to check the Extension and Size of the Attached File, For which i am using the Code:

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);
}

I gives error that for calculating File Size and Extension an array is given, string required.

For Multiple Attachments, attachment[] is an arrray.. How to check the file size and extension of an array type now The Form Code is

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

And the javascript is

<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条回答 默认 最新

  • doudong8713 2012-03-29 09:30
    关注

    This is how you get the filesize and extension using your script

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

    For information please see

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

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

    Thanks

    :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算