0

Moodle 1.9.7中,是否可以以某种方式为用户上传的文件指定允许的扩展名的白名单?

4

1 回答 1

0

查看源代码,当模块使用 upload_manager 处理上传的文件时,没有内置的方法来限制文件类型。也没有使用基于文件内容的任何 mimetype 检测。Moodle 中的 filelib 库基于文件扩展名的 mimetype。

在使用 moodle 上传管理器对象时为模块执行此操作的一种巧妙方法是编写一个扩展现有 upload_manager 类的新类,并添加一些基于文件内容的验证。

类似于以下内容 - 您需要稍微整理一下,并使用您自己的验证代码完成它。

class upload_manager_strict extends upload_manager {
  var $allowed_types
  function upload_manager_strict($inputname='', $deleteothers=false, $handlecollisions=false, $course=null, $recoverifmultiple=false, $modbytes=0, $silent=false, $allownull=false, $allownullmultiple=true, $allowed_types=null) {
    $this->allowed_types = $allowed_types;
    parent::upload_manager_strict($inputname, $deleteothers, $handlecollisions, $course, $recoverifmultiple, $modbytes, $silent, $allownull, $allownullmultiple)
  }

  function validate_file(&$file) {
     $status = parent::validate_file(&$file);
     if ($status) {
         // do some mimetype checking and validation on the file $file['tmp_name'] 
         // could use $this->allowedtypes 
     }
     return $status;
  }
}
于 2010-07-15T14:27:37.367 回答