本文整理汇总了PHP中stored_file类的典型用法代码示例。如果您正苦于以下问题:PHP stored_file类的具体用法?PHP stored_file怎么用?PHP stored_file使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了stored_file类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: can_import_file
/**
* Check if the given file is capable of being imported by this plugin.
* As {@link file_storage::mimetype()} now uses finfo PHP extension if available,
* the value returned by $file->get_mimetype for a .dat file is not the same on all servers.
* So we must made 2 checks to verify if the plugin can import the file.
* @param stored_file $file the file to check
* @return bool whether this plugin can import the file
*/
public function can_import_file($file) {
$mimetypes = array(
mimeinfo('type', '.dat'),
mimeinfo('type', '.zip')
);
return in_array($file->get_mimetype(), $mimetypes) || in_array(mimeinfo('type', $file->get_filename()), $mimetypes);
}
示例2: send_file
/**
* Repository method to serve the referenced file
*
* @param stored_file $storedfile the file that contains the reference
* @param int $lifetime Number of seconds before the file should expire from caches (null means $CFG->filelifetime)
* @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
* @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
* @param array $options additional options affecting the file serving
*/
public function send_file($storedfile, $lifetime = null, $filter = 0, $forcedownload = false, array $options = null)
{
$ref = unserialize(self::convert_to_valid_reference($storedfile->get_reference()));
header('Location: ' . $ref->downloadurl);
}
示例3: scorm_parse_scorm
/**
* Sets up SCORM 1.2/2004 packages using the manifest file.
* Called whenever SCORM changes
* @param object $scorm instance - fields are updated and changes saved into database
* @param stored_file|string $manifest - path to manifest file or stored_file.
* @return bool
*/
function scorm_parse_scorm(&$scorm, $manifest)
{
global $CFG, $DB;
// load manifest into string
if ($manifest instanceof stored_file) {
$xmltext = $manifest->get_content();
} else {
require_once "{$CFG->libdir}/filelib.php";
$xmltext = download_file_content($manifest);
}
$defaultorgid = 0;
$firstinorg = 0;
$pattern = '/&(?!\\w{2,6};)/';
$replacement = '&';
$xmltext = preg_replace($pattern, $replacement, $xmltext);
$objXML = new xml2Array();
$manifests = $objXML->parse($xmltext);
$scoes = new stdClass();
$scoes->version = '';
$scoes = scorm_get_manifest($manifests, $scoes);
$newscoes = array();
$sortorder = 0;
if (count($scoes->elements) > 0) {
$olditems = $DB->get_records('scorm_scoes', array('scorm' => $scorm->id));
foreach ($scoes->elements as $manifest => $organizations) {
foreach ($organizations as $organization => $items) {
foreach ($items as $identifier => $item) {
$sortorder++;
// This new db mngt will support all SCORM future extensions
$newitem = new stdClass();
$newitem->scorm = $scorm->id;
$newitem->manifest = $manifest;
$newitem->organization = $organization;
$newitem->sortorder = $sortorder;
$standarddatas = array('parent', 'identifier', 'launch', 'scormtype', 'title');
foreach ($standarddatas as $standarddata) {
if (isset($item->{$standarddata})) {
$newitem->{$standarddata} = $item->{$standarddata};
} else {
$newitem->{$standarddata} = '';
}
}
if (!empty($defaultorgid) && !empty($scoes->defaultorg) && empty($firstinorg) && $newitem->parent == $scoes->defaultorg) {
$firstinorg = $sortorder;
}
if (!empty($olditems) && ($olditemid = scorm_array_search('identifier', $newitem->identifier, $olditems))) {
$newitem->id = $olditemid;
// Update the Sco sortorder but keep id so that user tracks are kept against the same ids.
$DB->update_record('scorm_scoes', $newitem);
$id = $olditemid;
// Remove all old data so we don't duplicate it.
$DB->delete_records('scorm_scoes_data', array('scoid' => $olditemid));
$DB->delete_records('scorm_seq_objective', array('scoid' => $olditemid));
$DB->delete_records('scorm_seq_mapinfo', array('scoid' => $olditemid));
$DB->delete_records('scorm_seq_ruleconds', array('scoid' => $olditemid));
$DB->delete_records('scorm_seq_rulecond', array('scoid' => $olditemid));
$DB->delete_records('scorm_seq_rolluprule', array('scoid' => $olditemid));
$DB->delete_records('scorm_seq_rolluprulecond', array('scoid' => $olditemid));
// Now remove this SCO from the olditems object as we have dealt with it.
unset($olditems[$olditemid]);
} else {
// Insert the new SCO, and retain the link between the old and new for later adjustment
$id = $DB->insert_record('scorm_scoes', $newitem);
}
$newscoes[$id] = $newitem;
// Save this sco in memory so we can use it later.
if ($optionaldatas = scorm_optionals_data($item, $standarddatas)) {
$data = new stdClass();
$data->scoid = $id;
foreach ($optionaldatas as $optionaldata) {
if (isset($item->{$optionaldata})) {
$data->name = $optionaldata;
$data->value = $item->{$optionaldata};
$dataid = $DB->insert_record('scorm_scoes_data', $data);
}
}
}
if (isset($item->sequencingrules)) {
foreach ($item->sequencingrules as $sequencingrule) {
$rule = new stdClass();
$rule->scoid = $id;
$rule->ruletype = $sequencingrule->type;
$rule->conditioncombination = $sequencingrule->conditioncombination;
$rule->action = $sequencingrule->action;
$ruleid = $DB->insert_record('scorm_seq_ruleconds', $rule);
if (isset($sequencingrule->ruleconditions)) {
foreach ($sequencingrule->ruleconditions as $rulecondition) {
$rulecond = new stdClass();
$rulecond->scoid = $id;
$rulecond->ruleconditionsid = $ruleid;
$rulecond->referencedobjective = $rulecondition->referencedobjective;
$rulecond->measurethreshold = $rulecondition->measurethreshold;
$rulecond->operator = $rulecondition->operator;
//.........这里部分代码省略.........
示例4: sync_reference
/**
* Performs synchronisation of an external file if the previous one has expired.
*
* This function must be implemented for external repositories supporting
* FILE_REFERENCE, it is called for existing aliases when their filesize,
* contenthash or timemodified are requested. It is not called for internal
* repositories (see {@link repository::has_moodle_files()}), references to
* internal files are updated immediately when source is modified.
*
* Referenced files may optionally keep their content in Moodle filepool (for
* thumbnail generation or to be able to serve cached copy). In this
* case both contenthash and filesize need to be synchronized. Otherwise repositories
* should use contenthash of empty file and correct filesize in bytes.
*
* Note that this function may be run for EACH file that needs to be synchronised at the
* moment. If anything is being downloaded or requested from external sources there
* should be a small timeout. The synchronisation is performed to update the size of
* the file and/or to update image and re-generated image preview. There is nothing
* fatal if syncronisation fails but it is fatal if syncronisation takes too long
* and hangs the script generating a page.
*
* Note: If you wish to call $file->get_filesize(), $file->get_contenthash() or
* $file->get_timemodified() make sure that recursion does not happen.
*
* Called from {@link stored_file::sync_external_file()}
*
* @uses stored_file::set_missingsource()
* @uses stored_file::set_synchronized()
* @param stored_file $file
* @return bool false when file does not need synchronisation, true if it was synchronised
*/
public function sync_reference(stored_file $file)
{
if ($file->get_repository_id() != $this->id) {
// This should not really happen because the function can be called from stored_file only.
return false;
}
if ($this->has_moodle_files()) {
// References to local files need to be synchronised only once.
// Later they will be synchronised automatically when the source is changed.
if ($file->get_referencelastsync()) {
return false;
}
$fs = get_file_storage();
$params = file_storage::unpack_reference($file->get_reference(), true);
if (!is_array($params) || !($storedfile = $fs->get_file($params['contextid'], $params['component'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename']))) {
$file->set_missingsource();
} else {
$file->set_synchronized($storedfile->get_contenthash(), $storedfile->get_filesize(), 0, $storedfile->get_timemodified());
}
return true;
}
return false;
}
示例5: data_preset_get_file_contents
/**
* Retreive the contents of a file. That file may either be in a conventional directory of the Moodle file storage
* @param file_storage $filestorage. should be null if using a conventional directory
* @param stored_file $fileobj the directory to look in. null if using a conventional directory
* @param string $dir the directory to look in. null if using the Moodle file storage
* @param string $filename the name of the file we want
* @return string the contents of the file
*/
public function data_preset_get_file_contents(&$filestorage, &$fileobj, $dir, $filename) {
if(empty($filestorage) || empty($fileobj)) {
if (substr($dir, -1)!='/') {
$dir .= '/';
}
return file_get_contents($dir.$filename);
} else {
$file = $filestorage->get_file(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA, 0, $fileobj->get_filepath(), $filename);
return $file->get_content();
}
}
示例6: file_restore_source_field_from_draft_file
/**
* Restore the original source field from draft files
*
* Do not use this function because it makes field files.source inconsistent
* for draft area files. This function will be deprecated in 2.6
*
* @param stored_file $storedfile This only works with draft files
* @return stored_file
*/
function file_restore_source_field_from_draft_file($storedfile)
{
$source = @unserialize($storedfile->get_source());
if (!empty($source)) {
if (is_object($source)) {
$restoredsource = $source->source;
$storedfile->set_source($restoredsource);
} else {
throw new moodle_exception('invalidsourcefield', 'error');
}
}
return $storedfile;
}
示例7: get_response_file_url
/**
* Get the URL of a file that belongs to a response variable of this
* question_attempt.
* @param stored_file $file the file to link to.
* @return string the URL of that file.
*/
public function get_response_file_url(stored_file $file)
{
return file_encode_url(new moodle_url('/pluginfile.php'), '/' . implode('/', array($file->get_contextid(), $file->get_component(), $file->get_filearea(), $this->usageid, $this->slot, $file->get_itemid())) . $file->get_filepath() . $file->get_filename(), true);
}
示例8: get_pretected_pathname
public static function get_pretected_pathname(stored_file $file)
{
return $file->get_pathname_by_contenthash();
}
示例9: replace_file_with
/**
* Replaces the fields that might have changed when file was overriden in filepicker:
* reference, contenthash, filesize, userid
*
* Note that field 'source' must be updated separately because
* it has different format for draft and non-draft areas and
* this function will usually be used to replace non-draft area
* file with draft area file.
*
* @param stored_file $newfile
* @throws coding_exception
*/
public function replace_file_with(stored_file $newfile)
{
if ($newfile->get_referencefileid() && $this->fs->get_references_count_by_storedfile($this)) {
// The new file is a reference.
// The current file has other local files referencing to it.
// Double reference is not allowed.
throw new moodle_exception('errordoublereference', 'repository');
}
$filerecord = new stdClass();
$contenthash = $newfile->get_contenthash();
if ($this->fs->content_exists($contenthash)) {
$filerecord->contenthash = $contenthash;
} else {
throw new file_exception('storedfileproblem', 'Invalid contenthash, content must be already in filepool', $contenthash);
}
$filerecord->filesize = $newfile->get_filesize();
$filerecord->referencefileid = $newfile->get_referencefileid();
$filerecord->userid = $newfile->get_userid();
$this->update($filerecord);
}
示例10: file_overwrite_existing_draftfile
/**
* Overwrite an existing file in a draft area.
*
* @param stored_file $newfile the new file with the new content and meta-data
* @param stored_file $existingfile the file that will be overwritten
* @throws moodle_exception
* @since Moodle 3.2
*/
function file_overwrite_existing_draftfile(stored_file $newfile, stored_file $existingfile)
{
if ($existingfile->get_component() != 'user' or $existingfile->get_filearea() != 'draft') {
throw new coding_exception('The file to overwrite is not in a draft area.');
}
$fs = get_file_storage();
// Remember original file source field.
$source = @unserialize($existingfile->get_source());
// Remember the original sortorder.
$sortorder = $existingfile->get_sortorder();
if ($newfile->is_external_file()) {
// New file is a reference. Check that existing file does not have any other files referencing to it
if (isset($source->original) && $fs->search_references_count($source->original)) {
throw new moodle_exception('errordoublereference', 'repository');
}
}
// Delete existing file to release filename.
$newfilerecord = array('contextid' => $existingfile->get_contextid(), 'component' => 'user', 'filearea' => 'draft', 'itemid' => $existingfile->get_itemid(), 'timemodified' => time());
$existingfile->delete();
// Create new file.
$newfile = $fs->create_file_from_storedfile($newfilerecord, $newfile);
// Preserve original file location (stored in source field) for handling references.
if (isset($source->original)) {
if (!($newfilesource = @unserialize($newfile->get_source()))) {
$newfilesource = new stdClass();
}
$newfilesource->original = $source->original;
$newfile->set_source(serialize($newfilesource));
}
$newfile->set_sortorder($sortorder);
}
示例11: copy_existing_file
/**
* Copies a file from somewhere else in moodle
* to the portfolio temporary working directory
* associated with this export
*
* @param stored_file $oldfile existing stored file object
* @return stored_file|bool new file object
*/
public function copy_existing_file($oldfile)
{
if (array_key_exists($oldfile->get_contenthash(), $this->newfilehashes)) {
return $this->newfilehashes[$oldfile->get_contenthash()];
}
$fs = get_file_storage();
$file_record = $this->new_file_record_base($oldfile->get_filename());
if ($dir = $this->get('format')->get_file_directory()) {
$file_record->filepath = '/' . $dir . '/';
}
try {
$newfile = $fs->create_file_from_storedfile($file_record, $oldfile->get_id());
$this->newfilehashes[$newfile->get_contenthash()] = $newfile;
return $newfile;
} catch (file_exception $e) {
return false;
}
}
示例12: export_file_for_engine
/**
* Export the data for the given file in relation to this document.
*
* @param \stored_file $file The stored file we are talking about.
* @return array
*/
public function export_file_for_engine($file)
{
$data = $this->export_for_engine();
// Content is index in the main document.
unset($data['content']);
unset($data['description1']);
unset($data['description2']);
// Going to append the fileid to give it a unique id.
$data['id'] = $data['id'] . '-solrfile' . $file->get_id();
$data['type'] = \core_search\manager::TYPE_FILE;
$data['solr_fileid'] = $file->get_id();
$data['solr_filecontenthash'] = $file->get_contenthash();
$data['solr_fileindexstatus'] = self::INDEXED_FILE_TRUE;
$data['title'] = $file->get_filename();
$data['modified'] = self::format_time_for_engine($file->get_timemodified());
return $data;
}
示例13: send_file
/**
* Repository method to serve the referenced file
*
* @param stored_file $storedfile the file that contains the reference
* @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
* @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
* @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
* @param array $options additional options affecting the file serving
*/
public function send_file($storedfile, $lifetime=86400 , $filter=0, $forcedownload=false, array $options = null) {
$ref = $storedfile->get_reference();
// Let box.net serve the file. It will return 'no such file' content if file not found
// also if file has the different name than alias, it will be returned with the box.net filename
header('Location: ' . $ref);
}
示例14: get_references_count_by_storedfile
/**
* Returns the number of aliases that link to the given stored_file
*
* Aliases in user draft areas are not counted.
*
* @param stored_file $storedfile
* @return int
*/
public function get_references_count_by_storedfile(stored_file $storedfile)
{
global $DB;
$params = array();
$params['contextid'] = $storedfile->get_contextid();
$params['component'] = $storedfile->get_component();
$params['filearea'] = $storedfile->get_filearea();
$params['itemid'] = $storedfile->get_itemid();
$params['filename'] = $storedfile->get_filename();
$params['filepath'] = $storedfile->get_filepath();
return $this->search_references_count(self::pack_reference($params));
}
示例15: send_file
/**
* Repository method to serve the referenced file
*
* @see send_stored_file
*
* @param stored_file $storedfile the file that contains the reference
* @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
* @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
* @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
* @param array $options additional options affecting the file serving
*/
public function send_file($storedfile, $lifetime=86400 , $filter=0, $forcedownload=false, array $options = null) {
$reference = $storedfile->get_reference();
if ($reference{0} == '/') {
$file = $this->root_path.substr($reference, 1, strlen($reference)-1);
} else {
$file = $this->root_path.$reference;
}
if (is_readable($file)) {
$filename = $storedfile->get_filename();
if ($options && isset($options['filename'])) {
$filename = $options['filename'];
}
$dontdie = ($options && isset($options['dontdie']));
send_file($file, $filename, $lifetime , $filter, false, $forcedownload, '', $dontdie);
} else {
send_file_not_found();
}
}