public function migrate_directory($rootpath, $relpath='/') {

        // Check the trailing slash in the $rootpath
        if (substr($rootpath, -1) === '/') {
            debugging('moodle1_file_manager::migrate_directory() expects $rootpath without the trailing slash', DEBUG_DEVELOPER);
            $rootpath = substr($rootpath, 0, strlen($rootpath) - 1);
        }

        if (!file_exists($this->basepath.'/'.$rootpath.$relpath)) {
            return array();
        }

        $fileids = array();

        // make the fake file record for the directory itself
        $filerecord = $this->make_file_record(array('filepath' => $relpath, 'filename' => '.'));
        $this->stash_file($filerecord);
        $fileids[] = $filerecord['id'];

        $items = new DirectoryIterator($this->basepath.'/'.$rootpath.$relpath);

        foreach ($items as $item) {

            if ($item->isDot()) {
                continue;
            }

            if ($item->isLink()) {
                throw new moodle1_convert_exception('unexpected_symlink');
            }

            if ($item->isFile()) {
                $fileids[] = $this->migrate_file(substr($item->getPathname(), strlen($this->basepath.'/')),
                    $relpath, $item->getFilename(), 0, $item->getCTime(), $item->getMTime());

            } else {
                $dirname = clean_param($item->getFilename(), PARAM_PATH);

                if ($dirname === '') {
                    throw new moodle1_convert_exception('unsupported_chars_in_filename');
                }

                // migrate subdirectories recursively
                $fileids = array_merge($fileids, $this->migrate_directory($rootpath, $relpath.$item->getFilename().'/'));
            }
        }

        return $fileids;
    }