本文整理汇总了PHP中repository::get_unused_filename方法的典型用法代码示例。如果您正苦于以下问题:PHP repository::get_unused_filename方法的具体用法?PHP repository::get_unused_filename怎么用?PHP repository::get_unused_filename使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类repository
的用法示例。
在下文中一共展示了repository::get_unused_filename方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_get_unused_filename
public function test_get_unused_filename()
{
global $USER;
$this->resetAfterTest(true);
$this->setAdminUser();
$fs = get_file_storage();
$draftitemid = null;
$context = context_user::instance($USER->id);
file_prepare_draft_area($draftitemid, $context->id, 'phpunit', 'test_get_unused_filename', 1);
$dummy = array('contextid' => $context->id, 'component' => 'user', 'filearea' => 'draft', 'itemid' => $draftitemid, 'filepath' => '/', 'filename' => '');
// Create some files.
$existingfiles = array('test', 'test.txt', 'test (1).txt', 'test1.txt', 'test1 (1).txt', 'test1 (2).txt', 'test1 (3).txt', 'test1 (My name is Bob).txt', 'test2 (555).txt', 'test3 (1000).txt', 'test3 (1000MB).txt');
foreach ($existingfiles as $filename) {
$dummy['filename'] = $filename;
$file = $fs->create_file_from_string($dummy, 'blah! ' . $filename);
$this->assertTrue(repository::draftfile_exists($draftitemid, '/', $filename));
}
// Actual testing.
$this->assertEquals('free.txt', repository::get_unused_filename($draftitemid, '/', 'free.txt'));
$this->assertEquals('test (1)', repository::get_unused_filename($draftitemid, '/', 'test'));
$this->assertEquals('test (2).txt', repository::get_unused_filename($draftitemid, '/', 'test.txt'));
$this->assertEquals('test1 (4).txt', repository::get_unused_filename($draftitemid, '/', 'test1.txt'));
$this->assertEquals('test1 (8).txt', repository::get_unused_filename($draftitemid, '/', 'test1 (8).txt'));
$this->assertEquals('test1 ().txt', repository::get_unused_filename($draftitemid, '/', 'test1 ().txt'));
$this->assertEquals('test2 (556).txt', repository::get_unused_filename($draftitemid, '/', 'test2 (555).txt'));
$this->assertEquals('test3 (1001).txt', repository::get_unused_filename($draftitemid, '/', 'test3 (1000).txt'));
$this->assertEquals('test3 (1000MB) (1).txt', repository::get_unused_filename($draftitemid, '/', 'test3 (1000MB).txt'));
$this->assertEquals('test4 (1).txt', repository::get_unused_filename($draftitemid, '/', 'test4 (1).txt'));
}
示例2: process_upload
//.........这里部分代码省略.........
case UPLOAD_ERR_FORM_SIZE:
throw new moodle_exception('upload_error_form_size', 'repository_upload');
break;
case UPLOAD_ERR_PARTIAL:
throw new moodle_exception('upload_error_partial', 'repository_upload');
break;
case UPLOAD_ERR_NO_FILE:
throw new moodle_exception('upload_error_no_file', 'repository_upload');
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
break;
case UPLOAD_ERR_CANT_WRITE:
throw new moodle_exception('upload_error_cant_write', 'repository_upload');
break;
case UPLOAD_ERR_EXTENSION:
throw new moodle_exception('upload_error_extension', 'repository_upload');
break;
default:
throw new moodle_exception('nofile');
}
}
\core\antivirus\manager::scan_file($_FILES[$elname]['tmp_name'], $_FILES[$elname]['name'], true);
// {@link repository::build_source_field()}
$sourcefield = $this->get_file_source_info($_FILES[$elname]['name']);
$record->source = self::build_source_field($sourcefield);
if (empty($saveas_filename)) {
$record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
} else {
$ext = '';
$match = array();
$filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
if (strpos($filename, '.') === false) {
// File has no extension at all - do not add a dot.
$record->filename = $saveas_filename;
} else {
if (preg_match('/\\.([a-z0-9]+)$/i', $filename, $match)) {
if (isset($match[1])) {
$ext = $match[1];
}
}
$ext = !empty($ext) ? $ext : '';
if (preg_match('#\\.(' . $ext . ')$#i', $saveas_filename)) {
// saveas filename contains file extension already
$record->filename = $saveas_filename;
} else {
$record->filename = $saveas_filename . '.' . $ext;
}
}
}
// Check the file has some non-null contents - usually an indication that a user has
// tried to upload a folder by mistake
if (!$this->check_valid_contents($_FILES[$elname]['tmp_name'])) {
throw new moodle_exception('upload_error_invalid_file', 'repository_upload', '', $record->filename);
}
if ($this->mimetypes != '*') {
// check filetype
$filemimetype = file_storage::mimetype($_FILES[$elname]['tmp_name'], $record->filename);
if (!in_array($filemimetype, $this->mimetypes)) {
throw new moodle_exception('invalidfiletype', 'repository', '', get_mimetype_description(array('filename' => $_FILES[$elname]['name'])));
}
}
if (empty($record->itemid)) {
$record->itemid = 0;
}
if ($maxbytes !== -1 && filesize($_FILES[$elname]['tmp_name']) > $maxbytes) {
$maxbytesdisplay = display_size($maxbytes);
throw new file_exception('maxbytesfile', (object) array('file' => $record->filename, 'size' => $maxbytesdisplay));
}
if (file_is_draft_area_limit_reached($record->itemid, $areamaxbytes, filesize($_FILES[$elname]['tmp_name']))) {
throw new file_exception('maxareabytes');
}
$record->contextid = $context->id;
$record->userid = $USER->id;
if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
$existingfilename = $record->filename;
$unused_filename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename);
$record->filename = $unused_filename;
$stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
if ($overwriteexisting) {
repository::overwrite_existing_draftfile($record->itemid, $record->filepath, $existingfilename, $record->filepath, $record->filename);
$record->filename = $existingfilename;
} else {
$event = array();
$event['event'] = 'fileexists';
$event['newfile'] = new stdClass();
$event['newfile']->filepath = $record->filepath;
$event['newfile']->filename = $unused_filename;
$event['newfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $unused_filename)->out(false);
$event['existingfile'] = new stdClass();
$event['existingfile']->filepath = $record->filepath;
$event['existingfile']->filename = $existingfilename;
$event['existingfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $existingfilename)->out(false);
return $event;
}
} else {
$stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
}
return array('url' => moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(false), 'id' => $record->itemid, 'file' => $record->filename);
}
示例3: move_to_filepool
/**
* Move file from download folder to file pool using FILE API
*
* @todo MDL-28637
* @static
* @param string $thefile file path in download folder
* @param stdClass $record
* @return array containing the following keys:
* icon
* file
* id
* url
*/
public static function move_to_filepool($thefile, $record)
{
global $DB, $CFG, $USER, $OUTPUT;
// scan for viruses if possible, throws exception if problem found
self::antivir_scan_file($thefile, $record->filename, empty($CFG->repository_no_delete));
//TODO: MDL-28637 this repository_no_delete is a bloody hack!
$fs = get_file_storage();
// If file name being used.
if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
$draftitemid = $record->itemid;
$new_filename = repository::get_unused_filename($draftitemid, $record->filepath, $record->filename);
$old_filename = $record->filename;
// Create a tmp file.
$record->filename = $new_filename;
$newfile = $fs->create_file_from_pathname($record, $thefile);
$event = array();
$event['event'] = 'fileexists';
$event['newfile'] = new stdClass();
$event['newfile']->filepath = $record->filepath;
$event['newfile']->filename = $new_filename;
$event['newfile']->url = moodle_url::make_draftfile_url($draftitemid, $record->filepath, $new_filename)->out();
$event['existingfile'] = new stdClass();
$event['existingfile']->filepath = $record->filepath;
$event['existingfile']->filename = $old_filename;
$event['existingfile']->url = moodle_url::make_draftfile_url($draftitemid, $record->filepath, $old_filename)->out();
return $event;
}
if ($file = $fs->create_file_from_pathname($record, $thefile)) {
if (empty($CFG->repository_no_delete)) {
$delete = unlink($thefile);
unset($CFG->repository_no_delete);
}
return array('url' => moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename())->out(), 'id' => $file->get_itemid(), 'file' => $file->get_filename(), 'icon' => $OUTPUT->pix_url(file_extension_icon($thefile, 32))->out());
} else {
return null;
}
}
示例4: zip_packer
}
break;
case 'zip':
$zipper = new zip_packer();
$file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
if (!$file->get_parent_directory()) {
$parent_path = '/';
$filepath = '/';
$filename = get_string('files') . '.zip';
} else {
$parent_path = $file->get_parent_directory()->get_filepath();
$filepath = explode('/', trim($file->get_filepath(), '/'));
$filepath = array_pop($filepath);
$filename = $filepath . '.zip';
}
$filename = repository::get_unused_filename($itemid, $parent_path, $filename);
$newfile = $zipper->archive_to_storage(array($filepath => $file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id);
$home_url->param('action', 'browse');
$home_url->param('draftpath', $parent_path);
redirect($home_url, get_string('ziped', 'repository'));
break;
case 'unzip':
$zipper = new zip_packer();
$file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename);
if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $itemid, $draftpath, $USER->id)) {
$str = get_string('unzipped', 'repository');
} else {
$str = get_string('cannotunzip', 'error');
}
$home_url->param('action', 'browse');
$home_url->param('draftpath', $draftpath);
示例5: array
$record->filesize = $file->get_filesize();
$reference = $file->get_reference();
$repo_id = $file->get_repository_id();
$repo = repository::get_repository_by_id($repo_id, $contextid, $repooptions);
}
}
if ($usefilereference) {
if ($repo->has_moodle_files()) {
$sourcefile = repository::get_moodle_file($reference);
$record->contenthash = $sourcefile->get_contenthash();
$record->filesize = $sourcefile->get_filesize();
}
// Check if file exists.
if (repository::draftfile_exists($itemid, $saveas_path, $saveas_filename)) {
// File name being used, rename it.
$unused_filename = repository::get_unused_filename($itemid, $saveas_path, $saveas_filename);
$record->filename = $unused_filename;
// Create a file copy using unused filename.
$storedfile = $fs->create_file_from_reference($record, $repo_id, $reference);
$event = array();
$event['event'] = 'fileexists';
$event['newfile'] = new stdClass();
$event['newfile']->filepath = $saveas_path;
$event['newfile']->filename = $unused_filename;
$event['newfile']->url = moodle_url::make_draftfile_url($itemid, $saveas_path, $unused_filename)->out();
$event['existingfile'] = new stdClass();
$event['existingfile']->filepath = $saveas_path;
$event['existingfile']->filename = $saveas_filename;
$event['existingfile']->url = moodle_url::make_draftfile_url($itemid, $saveas_path, $saveas_filename)->out();
} else {
$storedfile = $fs->create_file_from_reference($record, $repo_id, $reference);
示例6: copy_to_area
/**
* This function overwrite the default implement to copying file using file_storage
*
* @global object $USER
* @global object $DB
* @param string $encoded The information of file, it is base64 encoded php serialized data
* @param string $draftitemid itemid
* @param string $new_filename The intended name of file
* @param string $new_filepath the new path in draft area
* @return array The information of file
*/
public function copy_to_area($encoded, $draftitemid, $new_filepath, $new_filename) {
global $USER, $DB;
$user_context = get_context_instance(CONTEXT_USER, $USER->id);
$fs = get_file_storage();
$params = unserialize(base64_decode($encoded));
$contextid = clean_param($params['contextid'], PARAM_INT);
$fileitemid = clean_param($params['itemid'], PARAM_INT);
$filename = clean_param($params['filename'], PARAM_FILE);
$filepath = clean_param($params['filepath'], PARAM_PATH);;
$filearea = clean_param($params['filearea'], PARAM_AREA);
$component = clean_param($params['component'], PARAM_COMPONENT);
// XXX:
// When user try to pick a file from other filearea, normally file api will use file browse to
// operate the files with capability check, but in some areas, users don't have permission to
// browse the files (for example, forum_attachment area).
//
// To get 'recent' plugin working, we need to use lower level file_stoarge class to bypass the
// capability check, we will use a better workaround to improve it.
if ($stored_file = $fs->get_file($contextid, $component, $filearea, $fileitemid, $filepath, $filename)) {
// verify user id
if ($USER->id != $stored_file->get_userid()) {
throw new moodle_exception('errornotyourfile', 'repository');
}
$file_record = array('contextid'=>$user_context->id, 'component'=>'user', 'filearea'=>'draft',
'itemid'=>$draftitemid, 'filepath'=>$new_filepath, 'filename'=>$new_filename, 'sortorder'=>0);
// test if file already exists
if (repository::draftfile_exists($draftitemid, $new_filepath, $new_filename)) {
// create new file
$unused_filename = repository::get_unused_filename($draftitemid, $new_filepath, $new_filename);
$file_record['filename'] = $unused_filename;
// create a tmp file
$fs->create_file_from_storedfile($file_record, $stored_file);
$event = array();
$event['event'] = 'fileexists';
$event['newfile'] = new stdClass;
$event['newfile']->filepath = $new_filepath;
$event['newfile']->filename = $unused_filename;
$event['newfile']->url = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $unused_filename)->out();
$event['existingfile'] = new stdClass;
$event['existingfile']->filepath = $new_filepath;
$event['existingfile']->filename = $new_filename;
$event['existingfile']->url = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $new_filename)->out();;
return $event;
} else {
$fs->create_file_from_storedfile($file_record, $stored_file);
$info = array();
$info['title'] = $new_filename;
$info['itemid'] = $draftitemid;
$info['filesize'] = $stored_file->get_filesize();
$info['url'] = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $new_filename)->out();;
$info['contextid'] = $user_context->id;
return $info;
}
}
return false;
}
示例7: process_upload
//.........这里部分代码省略.........
if ($record->filepath !== '/') {
$record->filepath = file_correct_filepath($record->filepath);
}
if (!isset($_FILES[$elname])) {
throw new moodle_exception('nofile');
}
if (!empty($_FILES[$elname]['error'])) {
switch ($_FILES[$elname]['error']) {
case UPLOAD_ERR_INI_SIZE:
throw new moodle_exception('upload_error_ini_size', 'repository_upload');
break;
case UPLOAD_ERR_FORM_SIZE:
throw new moodle_exception('upload_error_form_size', 'repository_upload');
break;
case UPLOAD_ERR_PARTIAL:
throw new moodle_exception('upload_error_partial', 'repository_upload');
break;
case UPLOAD_ERR_NO_FILE:
throw new moodle_exception('upload_error_no_file', 'repository_upload');
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
break;
case UPLOAD_ERR_CANT_WRITE:
throw new moodle_exception('upload_error_cant_write', 'repository_upload');
break;
case UPLOAD_ERR_EXTENSION:
throw new moodle_exception('upload_error_extension', 'repository_upload');
break;
default:
throw new moodle_exception('nofile');
}
}
// scan the files, throws exception and deletes if virus found
// this is tricky because clamdscan daemon might not be able to access the files
$permissions = fileperms($_FILES[$elname]['tmp_name']);
@chmod($_FILES[$elname]['tmp_name'], $CFG->filepermissions);
self::antivir_scan_file($_FILES[$elname]['tmp_name'], $_FILES[$elname]['name'], true);
@chmod($_FILES[$elname]['tmp_name'], $permissions);
if (empty($saveas_filename)) {
$record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
} else {
$ext = '';
$match = array();
$filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
if (preg_match('/\\.([a-z0-9]+)$/i', $filename, $match)) {
if (isset($match[1])) {
$ext = $match[1];
}
}
$ext = !empty($ext) ? $ext : '';
if (preg_match('#\\.(' . $ext . ')$#i', $saveas_filename)) {
// saveas filename contains file extension already
$record->filename = $saveas_filename;
} else {
$record->filename = $saveas_filename . '.' . $ext;
}
}
// Check the file has some non-null contents - usually an indication that a user has
// tried to upload a folder by mistake
if (!$this->check_valid_contents($_FILES[$elname]['tmp_name'])) {
throw new moodle_exception('upload_error_invalid_file', 'repository_upload', '', $record->filename);
}
if ($this->mimetypes != '*') {
// check filetype
$filemimetype = mimeinfo('type', $_FILES[$elname]['name']);
if (!in_array($filemimetype, $this->mimetypes)) {
throw new moodle_exception('invalidfiletype', 'repository', '', get_mimetype_description(array('filename' => $_FILES[$elname]['name'])));
}
}
if (empty($record->itemid)) {
$record->itemid = 0;
}
if ($maxbytes !== -1 && filesize($_FILES[$elname]['tmp_name']) > $maxbytes) {
throw new file_exception('maxbytes');
}
$record->contextid = $context->id;
$record->userid = $USER->id;
$record->source = '';
if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
$existingfilename = $record->filename;
$unused_filename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename);
$record->filename = $unused_filename;
$stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
$event = array();
$event['event'] = 'fileexists';
$event['newfile'] = new stdClass();
$event['newfile']->filepath = $record->filepath;
$event['newfile']->filename = $unused_filename;
$event['newfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $unused_filename)->out(false);
$event['existingfile'] = new stdClass();
$event['existingfile']->filepath = $record->filepath;
$event['existingfile']->filename = $existingfilename;
$event['existingfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $existingfilename)->out(false);
return $event;
} else {
$stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
return array('url' => moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(false), 'id' => $record->itemid, 'file' => $record->filename);
}
}
示例8: stdClass
}
$file->rename($path, $file->get_filename());
}
$return = new stdClass();
$return->filepath = $parent;
echo json_encode($return);
die;
case 'zip':
$filepath = required_param('filepath', PARAM_PATH);
$zipper = get_file_packer('application/zip');
$fs = get_file_storage();
$file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
$parent_path = $file->get_parent_directory()->get_filepath();
$filepath = explode('/', trim($file->get_filepath(), '/'));
$filepath = array_pop($filepath);
$zipfile = repository::get_unused_filename($draftid, $parent_path, $filepath . '.zip');
if ($newfile = $zipper->archive_to_storage(array($filepath => $file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $zipfile, $USER->id)) {
$return = new stdClass();
$return->filepath = $parent_path;
echo json_encode($return);
} else {
echo json_encode(false);
}
die;
case 'downloaddir':
$filepath = required_param('filepath', PARAM_PATH);
$zipper = get_file_packer('application/zip');
$fs = get_file_storage();
$area = file_get_draft_area_info($draftid);
if ($area['filecount'] == 0) {
echo json_encode(false);
示例9: upload
/**
* Process uploaded file
* @return array|bool
*/
public function upload($saveas_filename, $maxbytes)
{
global $USER, $CFG;
$types = optional_param('accepted_types', '*', PARAM_RAW);
if (is_array($types) and in_array('*', $types) or $types == '*') {
$this->mimetypes = '*';
} else {
foreach ($types as $type) {
$this->mimetypes[] = mimeinfo('type', $type);
}
}
$record = new stdClass();
$record->filearea = 'draft';
$record->component = 'user';
$record->filepath = optional_param('savepath', '/', PARAM_PATH);
$record->itemid = optional_param('itemid', 0, PARAM_INT);
$record->license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
$record->author = optional_param('author', '', PARAM_TEXT);
$context = get_context_instance(CONTEXT_USER, $USER->id);
$elname = 'repo_upload_file';
$fs = get_file_storage();
$sm = get_string_manager();
if ($record->filepath !== '/') {
$record->filepath = file_correct_filepath($record->filepath);
}
if (!isset($_FILES[$elname])) {
throw new moodle_exception('nofile');
}
if (!empty($_FILES[$elname]['error'])) {
switch ($_FILES[$elname]['error']) {
case UPLOAD_ERR_INI_SIZE:
throw new moodle_exception('upload_error_ini_size', 'repository_upload');
break;
case UPLOAD_ERR_FORM_SIZE:
throw new moodle_exception('upload_error_form_size', 'repository_upload');
break;
case UPLOAD_ERR_PARTIAL:
throw new moodle_exception('upload_error_partial', 'repository_upload');
break;
case UPLOAD_ERR_NO_FILE:
throw new moodle_exception('upload_error_no_file', 'repository_upload');
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
break;
case UPLOAD_ERR_CANT_WRITE:
throw new moodle_exception('upload_error_cant_write', 'repository_upload');
break;
case UPLOAD_ERR_EXTENSION:
throw new moodle_exception('upload_error_extension', 'repository_upload');
break;
default:
throw new moodle_exception('nofile');
}
}
if (empty($saveas_filename)) {
$record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
} else {
$ext = '';
$match = array();
$filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
if (preg_match('/\\.([a-z0-9]+)$/i', $filename, $match)) {
if (isset($match[1])) {
$ext = $match[1];
}
}
$ext = !empty($ext) ? $ext : '';
if (preg_match('#\\.(' . $ext . ')$#i', $saveas_filename)) {
// saveas filename contains file extension already
$record->filename = $saveas_filename;
} else {
$record->filename = $saveas_filename . '.' . $ext;
}
}
if ($this->mimetypes != '*') {
// check filetype
$filemimetype = mimeinfo('type', $_FILES[$elname]['name']);
if (!in_array($filemimetype, $this->mimetypes)) {
if ($sm->string_exists($filemimetype, 'mimetypes')) {
$filemimetype = get_string($filemimetype, 'mimetypes');
}
throw new moodle_exception('invalidfiletype', 'repository', '', $filemimetype);
}
}
if (empty($record->itemid)) {
$record->itemid = 0;
}
if ($maxbytes !== -1 && filesize($_FILES[$elname]['tmp_name']) > $maxbytes) {
throw new file_exception('maxbytes');
}
$record->contextid = $context->id;
$record->userid = $USER->id;
$record->source = '';
if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
$existingfilename = $record->filename;
$unused_filename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename);
//.........这里部分代码省略.........
示例10: upload
/**
* Process uploaded file
* @return array|bool
*/
public function upload($search_text)
{
global $USER, $CFG;
$record = new stdClass();
$record->filearea = 'draft';
$record->component = 'user';
$record->filepath = optional_param('savepath', '/', PARAM_PATH);
$record->itemid = optional_param('itemid', 0, PARAM_INT);
$record->license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
$record->author = optional_param('author', '', PARAM_TEXT);
$context = get_context_instance(CONTEXT_USER, $USER->id);
$filename = required_param('recordaudio_filename', PARAM_FILE);
$filedata = required_param('recordaudio_filedata', PARAM_RAW);
$filedata = base64_decode($filedata);
$fs = get_file_storage();
$sm = get_string_manager();
if ($record->filepath !== '/') {
$record->filepath = file_correct_filepath($record->filepath);
}
$record->filename = $filename;
if (empty($record->itemid)) {
$record->itemid = 0;
}
$record->contextid = $context->id;
$record->userid = $USER->id;
$record->source = '';
if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
$existingfilename = $record->filename;
$unused_filename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename);
$record->filename = $unused_filename;
$stored_file = $fs->create_file_from_string($record, $filedata);
$event = array();
$event['event'] = 'fileexists';
$event['newfile'] = new stdClass();
$event['newfile']->filepath = $record->filepath;
$event['newfile']->filename = $unused_filename;
$event['newfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $unused_filename)->out();
$event['existingfile'] = new stdClass();
$event['existingfile']->filepath = $record->filepath;
$event['existingfile']->filename = $existingfilename;
$event['existingfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $existingfilename)->out();
return $event;
} else {
$stored_file = $fs->create_file_from_string($record, $filedata);
return array('url' => moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(), 'id' => $record->itemid, 'file' => $record->filename);
}
}