本文整理汇总了PHP中stored_file::get_filename方法的典型用法代码示例。如果您正苦于以下问题:PHP stored_file::get_filename方法的具体用法?PHP stored_file::get_filename怎么用?PHP stored_file::get_filename使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stored_file
的用法示例。
在下文中一共展示了stored_file::get_filename方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: is_valid_filename_for_import
/**
* Is this filename valid (contains a unique participant ID) for import?
*
* @param sepl $seplment - The seplment instance
* @param stored_file $fileinfo - The fileinfo
* @param array $participants - A list of valid participants for this module indexed by unique_id
* @param stdClass $user - Set to the user that matches by participant id
* @param sepl_plugin $plugin - Set to the plugin that exported the file
* @param string $filename - Set to truncated filename (prefix stripped)
* @return true If the participant Id can be extracted and this is a valid user
*/
public function is_valid_filename_for_import($seplment, $fileinfo, $participants, &$user, &$plugin, &$filename)
{
if ($fileinfo->is_directory()) {
return false;
}
// Ignore hidden files.
if (strpos($fileinfo->get_filename(), '.') === 0) {
return false;
}
// Ignore hidden files.
if (strpos($fileinfo->get_filename(), '~') === 0) {
return false;
}
$info = explode('_', $fileinfo->get_filename(), 5);
if (count($info) < 5) {
return false;
}
$participantid = $info[1];
$filename = $info[4];
$plugin = $seplment->get_plugin_by_type($info[2], $info[3]);
if (!is_numeric($participantid)) {
return false;
}
if (!$plugin) {
return false;
}
// Convert to int.
$participantid += 0;
if (empty($participants[$participantid])) {
return false;
}
$user = $participants[$participantid];
return true;
}
示例2: 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);
}
示例3: resolve_file_name
/**
* Given a stored file, get its file name, but make
* sure that it does not conflict with any other
* attachments in the file pool.
*
* @param \stored_file $file
* @return string
*/
protected function resolve_file_name(\stored_file $file)
{
$filename = $file->get_filename();
$pathinfo = pathinfo($filename);
$count = 1;
while (array_key_exists('attachments/' . $filename, $this->archivefiles)) {
$filename = $pathinfo['filename'] . '_' . $count . '.' . $pathinfo['extension'];
$count++;
}
return $filename;
}
示例4: archive_stored
/**
* Perform archiving file from stored file.
*
* @param zip_archive $ziparch zip archive instance
* @param string $archivepath file path to archive
* @param stored_file $file stored_file object
* @param file_progress $progress Progress indicator callback or null if not required
* @return bool success
*/
private function archive_stored($ziparch, $archivepath, $file, file_progress $progress = null)
{
$result = $file->archive_file($ziparch, $archivepath);
if (!$result) {
return false;
}
if (!$file->is_directory()) {
return true;
}
$baselength = strlen($file->get_filepath());
$fs = get_file_storage();
$files = $fs->get_directory_files($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), true, true);
foreach ($files as $file) {
// Record progress for each file.
if ($progress) {
$progress->progress();
}
$path = $file->get_filepath();
$path = substr($path, $baselength);
$path = $archivepath . '/' . $path;
if (!$file->is_directory()) {
$path = $path . $file->get_filename();
}
// Ignore result here, partial zipping is ok for now.
$file->archive_file($ziparch, $path);
}
return true;
}
示例5: 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);
}
示例6: snap_pluginfile_url
/**
* Make url based on file for theme_snap components only.
*
* @param stored_file $file
* @return \moodle_url | bool
*/
private static function snap_pluginfile_url($file)
{
if (!$file) {
return false;
} else {
return \moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_timemodified(), $file->get_filepath(), $file->get_filename());
}
}
示例7: label_generate_resized_image
/**
* Resize the image, if required, then generate an img tag and, if required, a link to the full-size image
* @param stored_file $file the image file to process
* @param int $maxwidth the maximum width allowed for the image
* @param int $maxheight the maximum height allowed for the image
* @return string HTML fragment to add to the label
*/
function label_generate_resized_image(stored_file $file, $maxwidth, $maxheight)
{
global $CFG;
$fullurl = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename());
$link = null;
$attrib = array('alt' => $file->get_filename(), 'src' => $fullurl);
if ($imginfo = $file->get_imageinfo()) {
// Work out the new width / height, bounded by maxwidth / maxheight
$width = $imginfo['width'];
$height = $imginfo['height'];
if (!empty($maxwidth) && $width > $maxwidth) {
$height *= (double) $maxwidth / $width;
$width = $maxwidth;
}
if (!empty($maxheight) && $height > $maxheight) {
$width *= (double) $maxheight / $height;
$height = $maxheight;
}
$attrib['width'] = $width;
$attrib['height'] = $height;
// If the size has changed and the image is of a suitable mime type, generate a smaller version
if ($width != $imginfo['width']) {
$mimetype = $file->get_mimetype();
if ($mimetype === 'image/gif' or $mimetype === 'image/jpeg' or $mimetype === 'image/png') {
require_once $CFG->libdir . '/gdlib.php';
$tmproot = make_temp_directory('mod_label');
$tmpfilepath = $tmproot . '/' . $file->get_contenthash();
$file->copy_content_to($tmpfilepath);
$data = generate_image_thumbnail($tmpfilepath, $width, $height);
unlink($tmpfilepath);
if (!empty($data)) {
$fs = get_file_storage();
$record = array('contextid' => $file->get_contextid(), 'component' => $file->get_component(), 'filearea' => $file->get_filearea(), 'itemid' => $file->get_itemid(), 'filepath' => '/', 'filename' => 's_' . $file->get_filename());
$smallfile = $fs->create_file_from_string($record, $data);
// Replace the image 'src' with the resized file and link to the original
$attrib['src'] = moodle_url::make_draftfile_url($smallfile->get_itemid(), $smallfile->get_filepath(), $smallfile->get_filename());
$link = $fullurl;
}
}
}
} else {
// Assume this is an image type that get_imageinfo cannot handle (e.g. SVG)
$attrib['width'] = $maxwidth;
}
$img = html_writer::empty_tag('img', $attrib);
if ($link) {
return html_writer::link($link, $img);
} else {
return $img;
}
}
示例8: 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;
}
示例9: 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();
}
}
示例10: util_get_file_url
/**
* Utility function for getting a file URL
*
* @param stored_file $file
* @return string file url
*/
private function util_get_file_url($file)
{
return moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename(), false);
}
示例11: fread
static function ensure_pdf_compatible(stored_file $file)
{
global $CFG;
$fp = $file->get_content_file_handle();
$ident = fread($fp, 10);
if (substr_compare('%PDF-', $ident, 0, 5) !== 0) {
return false;
// This is not a PDF file at all
}
$ident = substr($ident, 5);
// Remove the '%PDF-' part
$ident = explode('\\x0A', $ident);
// Truncate to first '0a' character
list($major, $minor) = explode('.', $ident[0]);
// Split the major / minor version
$major = intval($major);
$minor = intval($minor);
if ($major == 0 || $minor == 0) {
return false;
// Not a valid PDF version number
}
if ($major = 1 && $minor <= 4) {
return true;
// We can handle this version - nothing else to do
}
$temparea = $CFG->dataroot . '/temp/uploadpdf';
$hash = $file->get_contenthash();
$tempsrc = $temparea . "/src-{$hash}.pdf";
$tempdst = $temparea . "/dst-{$hash}.pdf";
if (!file_exists($temparea)) {
if (!mkdir($temparea, 0777, true)) {
die("Unable to create temporary folder {$temparea}");
}
}
$file->copy_content_to($tempsrc);
// Copy the file
$gsexec = $CFG->gs_path;
$command = "{$gsexec} -q -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -sOutputFile=\"{$tempdst}\" \"{$tempsrc}\" 2>&1";
$result = exec($command);
if (!file_exists($tempdst)) {
return false;
// Something has gone wrong in the conversion
}
$file->delete();
// Delete the original file
$fs = get_file_storage();
$fileinfo = array('contextid' => $file->get_contextid(), 'component' => $file->get_component(), 'filearea' => $file->get_filearea(), 'itemid' => $file->get_itemid(), 'filename' => $file->get_filename(), 'filepath' => $file->get_filepath());
$fs->create_file_from_pathname($fileinfo, $tempdst);
// Create replacement file
@unlink($tempsrc);
// Delete the temporary files
@unlink($tempdst);
return true;
}
示例12: import_external_file_contents
/**
* Downloads the file from external repository and saves it in moodle filepool.
* This function is different from {@link repository::sync_reference()} because it has
* bigger request timeout and always downloads the content.
*
* This function is invoked when we try to unlink the file from the source and convert
* a reference into a true copy.
*
* @throws exception when file could not be imported
*
* @param stored_file $file
* @param int $maxbytes throw an exception if file size is bigger than $maxbytes (0 means no limit)
*/
public function import_external_file_contents(stored_file $file, $maxbytes = 0)
{
if (!$file->is_external_file()) {
// nothing to import if the file is not a reference
return;
} else {
if ($file->get_repository_id() != $this->id) {
// error
debugging('Repository instance id does not match');
return;
} else {
if ($this->has_moodle_files()) {
// files that are references to local files are already in moodle filepool
// just validate the size
if ($maxbytes > 0 && $file->get_filesize() > $maxbytes) {
$maxbytesdisplay = display_size($maxbytes);
throw new file_exception('maxbytesfile', (object) array('file' => $file->get_filename(), 'size' => $maxbytesdisplay));
}
return;
} else {
if ($maxbytes > 0 && $file->get_filesize() > $maxbytes) {
// note that stored_file::get_filesize() also calls synchronisation
$maxbytesdisplay = display_size($maxbytes);
throw new file_exception('maxbytesfile', (object) array('file' => $file->get_filename(), 'size' => $maxbytesdisplay));
}
$fs = get_file_storage();
$contentexists = $fs->content_exists($file->get_contenthash());
if ($contentexists && $file->get_filesize() && $file->get_contenthash() === sha1('')) {
// even when 'file_storage::content_exists()' returns true this may be an empty
// content for the file that was not actually downloaded
$contentexists = false;
}
if (!$file->get_status() && $contentexists) {
// we already have the content in moodle filepool and it was synchronised recently.
// Repositories may overwrite it if they want to force synchronisation anyway!
return;
} else {
// attempt to get a file
try {
$fileinfo = $this->get_file($file->get_reference());
if (isset($fileinfo['path'])) {
list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($fileinfo['path']);
// set this file and other similar aliases synchronised
$file->set_synchronized($contenthash, $filesize);
} else {
throw new moodle_exception('errorwhiledownload', 'repository', '', '');
}
} catch (Exception $e) {
if ($contentexists) {
// better something than nothing. We have a copy of file. It's sync time
// has expired but it is still very likely that it is the last version
} else {
throw $e;
}
}
}
}
}
}
}
示例13: send_file
/**
* Repository method to serve file
*
* @param stored_file $storedfile
* @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 = unserialize($storedfile->get_reference());
$cachedfilepath = cache_file::get($reference, array('ttl' => $this->cachedfilettl));
if ($cachedfilepath === false) {
// Cache the file.
$this->set_access_key($reference->access_key);
$this->set_access_secret($reference->access_secret);
$path = $this->get_file($reference->path);
$cachedfilepath = cache_file::create_from_file($reference, $path['path']);
}
send_file($cachedfilepath, $storedfile->get_filename(), 'default', $filter, false, $forcedownload);
}
示例14: send_stored_file
/**
* Handles the sending of file data to the user's browser, including support for
* byteranges etc.
*
* @category files
* @global stdClass $CFG
* @global stdClass $COURSE
* @global moodle_session $SESSION
* @param stored_file $stored_file local file object
* @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 string $filename Override filename
* @param bool $dontdie - return control to caller afterwards. this is not recommended and only used for cleanup tasks.
* if this is passed as true, ignore_user_abort is called. if you don't want your processing to continue on cancel,
* you must detect this case when control is returned using connection_aborted. Please not that session is closed
* and should not be reopened.
* @return null script execution stopped unless $dontdie is true
*/
function send_stored_file($stored_file, $lifetime = 86400, $filter = 0, $forcedownload = false, $filename = null, $dontdie = false)
{
global $CFG, $COURSE, $SESSION;
if (!$stored_file or $stored_file->is_directory()) {
// nothing to serve
if ($dontdie) {
return;
}
die;
}
if ($dontdie) {
ignore_user_abort(true);
}
session_get_instance()->write_close();
// unlock session during fileserving
// Use given MIME type if specified, otherwise guess it using mimeinfo.
// IE, Konqueror and Opera open html file directly in browser from web even when directed to save it to disk :-O
// only Firefox saves all files locally before opening when content-disposition: attachment stated
$filename = is_null($filename) ? $stored_file->get_filename() : $filename;
$isFF = check_browser_version('Firefox', '1.5');
// only FF > 1.5 properly tested
$mimetype = ($forcedownload and !$isFF) ? 'application/x-forcedownload' : ($stored_file->get_mimetype() ? $stored_file->get_mimetype() : mimeinfo('type', $filename));
$lastmodified = $stored_file->get_timemodified();
$filesize = $stored_file->get_filesize();
if ($lifetime > 0 && !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
// get unixtime of request header; clip extra junk off first
$since = strtotime(preg_replace('/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"]));
if ($since && $since >= $lastmodified) {
header('HTTP/1.1 304 Not Modified');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $lifetime) . ' GMT');
header('Cache-Control: max-age=' . $lifetime);
header('Content-Type: ' . $mimetype);
if ($dontdie) {
return;
}
die;
}
}
//do not put '@' before the next header to detect incorrect moodle configurations,
//error should be better than "weird" empty lines for admins/users
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastmodified) . ' GMT');
// if user is using IE, urlencode the filename so that multibyte file name will show up correctly on popup
if (check_browser_version('MSIE')) {
$filename = rawurlencode($filename);
}
if ($forcedownload) {
header('Content-Disposition: attachment; filename="' . $filename . '"');
} else {
header('Content-Disposition: inline; filename="' . $filename . '"');
}
if ($lifetime > 0) {
header('Cache-Control: max-age=' . $lifetime);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $lifetime) . ' GMT');
header('Pragma: ');
if (empty($CFG->disablebyteserving) && $mimetype != 'text/plain' && $mimetype != 'text/html') {
header('Accept-Ranges: bytes');
if (!empty($_SERVER['HTTP_RANGE']) && strpos($_SERVER['HTTP_RANGE'], 'bytes=') !== FALSE) {
// byteserving stuff - for acrobat reader and download accelerators
// see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35
// inspired by: http://www.coneural.org/florian/papers/04_byteserving.php
$ranges = false;
if (preg_match_all('/(\\d*)-(\\d*)/', $_SERVER['HTTP_RANGE'], $ranges, PREG_SET_ORDER)) {
foreach ($ranges as $key => $value) {
if ($ranges[$key][1] == '') {
//suffix case
$ranges[$key][1] = $filesize - $ranges[$key][2];
$ranges[$key][2] = $filesize - 1;
} else {
if ($ranges[$key][2] == '' || $ranges[$key][2] > $filesize - 1) {
//fix range length
$ranges[$key][2] = $filesize - 1;
}
}
if ($ranges[$key][2] != '' && $ranges[$key][2] < $ranges[$key][1]) {
//invalid byte-range ==> ignore header
$ranges = false;
break;
}
//prepare multipart header
$ranges[$key][0] = "\r\n--" . BYTESERVING_BOUNDARY . "\r\nContent-Type: {$mimetype}\r\n";
$ranges[$key][0] .= "Content-Range: bytes {$ranges[$key][1]}-{$ranges[$key][2]}/{$filesize}\r\n\r\n";
//.........这里部分代码省略.........
示例15: xmldb_hotpot_move_file
/**
* xmldb_hotpot_move_file
*
* move a file or folder (within the same context)
* if $file is a directory, then all subfolders and files will also be moved
* if the destination file/folder already exists, then $file will be deleted
*
* @param stored_file $file
* @param string $new_filepath
* @param string $new_filename (optional, default='')
* @return void, but may update filearea
*/
function xmldb_hotpot_move_file($file, $new_filepath, $new_filename = '')
{
$fs = get_file_storage();
$contextid = $file->get_contextid();
$component = $file->get_component();
$filearea = $file->get_filearea();
$itemid = $file->get_itemid();
$old_filepath = $file->get_filepath();
$old_filename = $file->get_filename();
if ($file->is_directory()) {
$children = $fs->get_directory_files($contextid, $component, $filearea, $itemid, $old_filepath);
$old_filepath = '/^' . preg_quote($old_filepath, '/') . '/';
foreach ($children as $child) {
xmldb_hotpot_move_file($child, preg_replace($old_filepath, $new_filepath, $child->get_filepath(), 1));
}
}
if ($new_filename == '') {
$new_filename = $old_filename;
}
if ($fs->file_exists($contextid, $component, $filearea, $itemid, $new_filepath, $new_filename)) {
$file->delete();
// new file already exists
} else {
$file->rename($new_filepath, $new_filename);
}
}