public function create_file_from_string($newfilename, $content, $userid = NULL) {
        if (!$this->is_writable() or !$this->lf->is_directory()) {
            return null;
        }

        $newfilename = clean_param($newfilename, PARAM_FILE);
        if ($newfilename === '') {
            return null;
        }

        $fs = get_file_storage();

        $now = time();

        $newrecord = new stdClass();
        $newrecord->contextid = $this->lf->get_contextid();
        $newrecord->component = $this->lf->get_component();
        $newrecord->filearea  = $this->lf->get_filearea();
        $newrecord->itemid    = $this->lf->get_itemid();
        $newrecord->filepath  = $this->lf->get_filepath();
        $newrecord->filename  = $newfilename;

        if ($fs->file_exists($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename)) {
            // file already exists, sorry
            return null;
        }

        $newrecord->timecreated  = $now;
        $newrecord->timemodified = $now;
        $newrecord->mimetype     = mimeinfo('type', $newfilename);
        $newrecord->userid       = $userid;

        if ($file = $fs->create_file_from_string($newrecord, $content)) {
            return $this->browser->get_file_info($this->context, $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
        }
        return null;
    }
public function create_file_from_string($newfilename, $content, $userid = NULL) {
        if (!$this->is_writable() or !$this->lf->is_directory()) {
            return null;
        }

        $newfilename = clean_param($newfilename, PARAM_FILE);
        if ($newfilename === '') {
            return null;
        }

        $fs = get_file_storage();

        $now = time();

        $newrecord = new stdClass();
        $newrecord->contextid = $this->lf->get_contextid();
        $newrecord->component = $this->lf->get_component();
        $newrecord->filearea  = $this->lf->get_filearea();
        $newrecord->itemid    = $this->lf->get_itemid();
        $newrecord->filepath  = $this->lf->get_filepath();
        $newrecord->filename  = $newfilename;

        if ($fs->file_exists($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename)) {
            // file already exists, sorry
            return null;
        }

        $newrecord->timecreated  = $now;
        $newrecord->timemodified = $now;
        $newrecord->mimetype     = mimeinfo('type', $newfilename);
        $newrecord->userid       = $userid;

        if ($file = $fs->create_file_from_string($newrecord, $content)) {
            return $this->browser->get_file_info($this->context, $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
        }
        return null;
    }
public function create_file_from_string ( $ newfilename , $ content , $ userid = NULL ) { if ( ! $ this -> is_writable ( ) or ! $ this -> lf -> is_directory ( ) ) { return null ; } $ newfilename = clean_param ( $ newfilename , PARAM_FILE ) ; if ( $ newfilename === '' ) { return null ; } $ fs = get_file_storage ( ) ; $ now = time ( ) ; $ newrecord = new stdClass ( ) ; $ newrecord -> contextid = $ this -> lf -> get_contextid ( ) ; $ newrecord -> component = $ this -> lf -> get_component ( ) ; $ newrecord -> filearea = $ this -> lf -> get_filearea ( ) ; $ newrecord -> itemid = $ this -> lf -> get_itemid ( ) ; $ newrecord -> filepath = $ this -> lf -> get_filepath ( ) ; $ newrecord -> filename = $ newfilename ; if ( $ fs -> file_exists ( $ newrecord -> contextid , $ newrecord -> component , $ newrecord -> filearea , $ newrecord -> itemid , $ newrecord -> filepath , $ newrecord -> filename ) ) { // file already exists, sorry return null ; } $ newrecord -> timecreated = $ now ; $ newrecord -> timemodified = $ now ; $ newrecord -> mimetype = mimeinfo ( 'type' , $ newfilename ) ; $ newrecord -> userid = $ userid ; if ( $ file = $ fs -> create_file_from_string ( $ newrecord , $ content ) ) { return $ this -> browser -> get_file_info ( $ this -> context , $ file -> get_component ( ) , $ file -> get_filearea ( ) , $ file -> get_itemid ( ) , $ file -> get_filepath ( ) , $ file -> get_filename ( ) ) ; } return null ; }
Create new file from string - make sure
params are valid.

@param string $newfilename name of new file
@param string $content of file
@param int $userid id of author, default $USER->id
@return file_info|null new file's file_info instance or null if failed
Create new file from string - make sure params are valid .