前幾天才剛將moodle升級到1.8.2,剛剛突然想到中文檔名的問題,剛剛試著上傳中文檔名的檔案,果然檔案上傳後,中文又不見了。

所以根據之前的文章「 修正moodle上傳中文檔案問題」,想說再改一次吧!改好了,又恢復可以上傳中文檔名了。不過,改法有些不同,簡單一些了。

一、修改config.php

根據之前的文章,原本要改 /lib/moodlelib.php 這檔案,將這兩行 「$string = convert_high...」、「string = preg_replace(...」給註解掉。

不過,剛剛去找,卻找不到,程式已有些不同。還好看到那「function clean_filename($string) 」上面有說明:

/**
 * Cleans a given filename by removing suspicious or troublesome characters
 * Only these are allowed: alphanumeric _ - .
 * Unicode characters can be enabled by setting $CFG->unicodecleanfilename = true in config.php
 *
 * WARNING: unicode characters may not be compatible with zip compression in backup/restore,
 *          because native zip binaries do weird character conversions. Use PHP zipping instead.
 *
 * @param string $string  file name
 * @return string cleaned file name
 */
function clean_filename($string) {
    global $CFG;
    if (empty($CFG->unicodecleanfilename)) {
        $textlib = textlib_get_instance();
        $string = $textlib->specialtoascii($string);
        $string = preg_replace('/[^\.a-zA-Z\d\_-]/','_', $string ); // only allowed chars

所以再去看看config.php,找到了下面這一段,將下面紅色字那一行前面的註解 // 給拿掉 

// Allow unicode characters in uploaded files, generated reports, etc.
// This setting is new and not much tested, there are known problems
// with backup/restore that will not be solved, because native infozip
// binaries are doing some weird conversions - use internal PHP zipping instead.
// NOT RECOMMENDED FOR PRODUCTION SITES
     $CFG->unicodecleanfilename = true;

這樣就可以上傳中文檔名了。但在下載中文檔名檔案時,還是一樣跟「 修正moodle上傳中文檔案問題」裡說到的在IE下,中文檔名的檔案還是會變成隨機英文字串的檔名,所以再繼續修改吧!

二、修改 /moodle/file.php 

約在第174行(倒數第9行) ,將底下紅色字那一行註解掉,這樣就可以了。


    // ========================================
    // finally send the file
    // ========================================
    session_write_close(); // unlock session during fileserving
//    $filename = $args[count($args)-1];
    send_file($pathname, $filename, $lifetime, $CFG->filteruploadedfiles, false, $forcedownload);

    function not_found($courseid) {
        global $CFG;
        header('HTTP/1.0 404 not found');
        error(get_string('filenotfound', 'error'), $CFG->wwwroot.'/course/view.php?id='.$courseid); //this is not displayed on IIS??
    }
?>

 
PS:我不確定這樣的改法,會不會造成其他影響,不過至少可以在moodle上正常上傳、下載中文檔名的檔案。