Trong khi duyệt Moodle's mã nguồn tôi stumbled trên này:base64_encode và serialize php function params
kho/gần đây/lib.php
public function get_listing($encodedpath = '', $page = '') {
global $OUTPUT;
$ret = array();
$ret['dynload'] = true;
$ret['nosearch'] = true;
$ret['nologin'] = true;
$list = array();
$files = $this->get_recent_files(0, $this->number);
try {
foreach ($files as $file) {
$params = base64_encode(serialize($file));
// Check that file exists and accessible
$filesize = $this->get_file_size($params);
if ($file['filename'] == 'image.png') {
var_dump($filesize);
}
if (!empty($filesize)) {
$node = array(
'title' => $file['filename'],
'size' => $filesize,
'date' => '',
'source'=> $params,
'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file['filename'], 32))->out(false),
);
$list[] = $node;
}
}
} catch (Exception $e) {
throw new repository_exception('emptyfilelist', 'repository_recent');
}
$ret['list'] = array_filter($list, array($this, 'filter'));
return $ret;
}
kho/lib.php
public function get_file_size($source) {
$browser = get_file_browser();
$params = unserialize(base64_decode($source));
$contextid = clean_param($params['contextid'], PARAM_INT);
$fileitemid = clean_param($params['itemid'], PARAM_INT);
$filename = clean_param($params['filename'], PARAM_FILE);
$filepath = clean_param($params['filepath'], PARAM_PATH);
$filearea = clean_param($params['filearea'], PARAM_AREA);
$component = clean_param($params['component'], PARAM_COMPONENT);
$context = get_context_instance_by_id($contextid);
$file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
if (!empty($file_info)) {
$filesize = $file_info->get_filesize();
} else {
$filesize = null;
}
return $filesize;
}
Câu hỏi của tôi là mục đích của mã hóa base64 và thứ tự tuần tự là gì là khi nó ngay lập tức được hoàn tác một lần bên trong chức năng? Có một lý do hợp lệ để làm điều này hoặc là điều này chỉ hơn thiết kế?
Không có lý do hợp lệ để làm điều này, trừ khi đây là một ngoại lệ thực sự lớn và cách sử dụng phổ biến nhất của 'get_file_size' là lấy kích thước tệp của đối tượng' serialize'd, 'base64_encode'd. – Ryan
['function get_file_size'] (http://xref.schoolsict.net/moodle/2.2/nav.html?repository/lib.php.source.html#l1367) trong nguồn moodle – air4x
Bạn có thể hiển thị mã theo sau gọi tới '$ this-> get_file_size()'? – NullUserException