当前位置: 首页>>代码示例>>PHP>>正文


PHP stored_file::get_filesize方法代码示例

本文整理汇总了PHP中stored_file::get_filesize方法的典型用法代码示例。如果您正苦于以下问题:PHP stored_file::get_filesize方法的具体用法?PHP stored_file::get_filesize怎么用?PHP stored_file::get_filesize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stored_file的用法示例。


在下文中一共展示了stored_file::get_filesize方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: 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) {
                     throw new file_exception('maxbytes');
                 }
                 return;
             } else {
                 if ($maxbytes > 0 && $file->get_filesize() > $maxbytes) {
                     // note that stored_file::get_filesize() also calls synchronisation
                     throw new file_exception('maxbytes');
                 }
                 $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;
                         }
                     }
                 }
             }
         }
     }
 }
开发者ID:isuruAb,项目名称:moodle,代码行数:71,代码来源:lib.php

示例2: send_file

 /**
  * Repository method to serve the referenced file
  *
  * This method is ivoked from {@link send_stored_file()}.
  * Dropbox repository first caches the file by reading it into temporary folder and then
  * serves from there.
  *
  * @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($storedfile->get_reference());
     if ($storedfile->get_filesize() > $this->max_cache_bytes()) {
         header('Location: ' . $this->get_file_download_link($ref->url));
         die;
     }
     try {
         $this->import_external_file_contents($storedfile, $this->max_cache_bytes());
         if (!is_array($options)) {
             $options = array();
         }
         $options['sendcachedexternalfile'] = true;
         send_stored_file($storedfile, $lifetime, $filter, $forcedownload, $options);
     } catch (moodle_exception $e) {
         // redirect to Dropbox, it will show the error.
         // We redirect to Dropbox shared link, not to download link here!
         header('Location: ' . $ref->url);
         die;
     }
 }
开发者ID:krysnuvadga,项目名称:moodle,代码行数:34,代码来源:lib.php

示例3: update_references_to_storedfile

 /**
  * Updates all files that are referencing this file with the new contenthash
  * and filesize
  *
  * @param stored_file $storedfile
  */
 public function update_references_to_storedfile(stored_file $storedfile)
 {
     global $CFG, $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();
     $reference = self::pack_reference($params);
     $referencehash = sha1($reference);
     $sql = "SELECT repositoryid, id FROM {files_reference}\n                 WHERE referencehash = ?";
     $rs = $DB->get_recordset_sql($sql, array($referencehash));
     $now = time();
     foreach ($rs as $record) {
         $this->update_references($record->id, $now, null, $storedfile->get_contenthash(), $storedfile->get_filesize(), 0, $storedfile->get_timemodified());
     }
     $rs->close();
 }
开发者ID:IFPBMoodle,项目名称:moodle,代码行数:26,代码来源:file_storage.php

示例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());
            }
            return true;
        }

        // Backward compatibility (Moodle 2.3-2.5) implementation that calls
        // methods repository::get_reference_file_lifetime(), repository::sync_individual_file()
        // and repository::get_file_by_reference(). These methods are removed from the
        // base repository class but may still be implemented by the child classes.

        // THIS IS NOT A GOOD EXAMPLE of implementation. For good examples see the overwriting methods.

        if (!method_exists($this, 'get_file_by_reference')) {
            // Function get_file_by_reference() is not implemented. No synchronisation.
            return false;
        }

        // Check if the previous sync result is still valid.
        if (method_exists($this, 'get_reference_file_lifetime')) {
            $lifetime = $this->get_reference_file_lifetime($file->get_reference());
        } else {
            // Default value that was hardcoded in Moodle 2.3 - 2.5.
            $lifetime =  60 * 60 * 24;
        }
        if (($lastsynced = $file->get_referencelastsync()) && $lastsynced + $lifetime >= time()) {
            return false;
        }

        $cache = cache::make('core', 'repositories');
        if (($lastsyncresult = $cache->get('sync:'.$file->get_referencefileid())) !== false) {
            if ($lastsyncresult === true) {
                // We are in the process of synchronizing this reference.
                // Avoid recursion when calling $file->get_filesize() and $file->get_contenthash().
                return false;
            } else {
                // We have synchronised the same reference inside this request already.
                // It looks like the object $file was created before the synchronisation and contains old data.
                if (!empty($lastsyncresult['missing'])) {
                    $file->set_missingsource();
                } else {
                    $cache->set('sync:'.$file->get_referencefileid(), true);
                    if ($file->get_contenthash() != $lastsyncresult['contenthash'] ||
                            $file->get_filesize() != $lastsyncresult['filesize']) {
                        $file->set_synchronized($lastsyncresult['contenthash'], $lastsyncresult['filesize']);
                    }
                    $cache->set('sync:'.$file->get_referencefileid(), $lastsyncresult);
                }
                return true;
            }
        }
//.........这里部分代码省略.........
开发者ID:educacionbe,项目名称:cursos,代码行数:101,代码来源:lib.php

示例5: 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);
 }
开发者ID:qanuj,项目名称:moodle,代码行数:32,代码来源:stored_file.php

示例6: file_is_indexable

 /**
  * Checks to see if a passed file is indexable.
  *
  * @param \stored_file $file The file to check
  * @return bool True if the file can be indexed
  */
 protected function file_is_indexable($file)
 {
     if (!empty($this->config->maxindexfilekb) && $file->get_filesize() > $this->config->maxindexfilekb * 1024) {
         // The file is too big to index.
         return false;
     }
     $mime = $file->get_mimetype();
     if ($mime == 'application/vnd.moodle.backup') {
         // We don't index Moodle backup files. There is nothing usefully indexable in them.
         return false;
     }
     return true;
 }
开发者ID:janeklb,项目名称:moodle,代码行数:19,代码来源:engine.php

示例7: try_content_recovery

 /**
  * Tries to recover missing content of file from trash.
  *
  * @param stored_file $file stored_file instance
  * @return bool success
  */
 public function try_content_recovery($file)
 {
     $contenthash = $file->get_contenthash();
     $trashfile = $this->trash_path_from_hash($contenthash) . '/' . $contenthash;
     if (!is_readable($trashfile)) {
         if (!is_readable($this->trashdir . '/' . $contenthash)) {
             return false;
         }
         // nice, at least alternative trash file in trash root exists
         $trashfile = $this->trashdir . '/' . $contenthash;
     }
     if (filesize($trashfile) != $file->get_filesize() or sha1_file($trashfile) != $contenthash) {
         //weird, better fail early
         return false;
     }
     $contentdir = $this->path_from_hash($contenthash);
     $contentfile = $contentdir . '/' . $contenthash;
     if (file_exists($contentfile)) {
         //strange, no need to recover anything
         return true;
     }
     if (!is_dir($contentdir)) {
         if (!mkdir($contentdir, $this->dirpermissions, true)) {
             return false;
         }
     }
     return rename($trashfile, $contentfile);
 }
开发者ID:nigeli,项目名称:moodle,代码行数:34,代码来源:file_storage.php

示例8: create_from_archive

 public static function create_from_archive(gallery $gallery, \stored_file $storedfile, $formdata = array())
 {
     global $DB;
     $context = $gallery->get_collection()->context;
     $maxitems = $gallery->get_collection()->maxitems;
     $count = $DB->count_records('mediagallery_item', array('galleryid' => $gallery->id));
     if ($maxitems != 0 && $count >= $maxitems) {
         return;
     }
     $fs = get_file_storage();
     $packer = get_file_packer('application/zip');
     $fs->delete_area_files($context->id, 'mod_mediagallery', 'unpacktemp', 0);
     $storedfile->extract_to_storage($packer, $context->id, 'mod_mediagallery', 'unpacktemp', 0, '/');
     $itemfiles = $fs->get_area_files($context->id, 'mod_mediagallery', 'unpacktemp', 0);
     $storedfile->delete();
     foreach ($itemfiles as $storedfile) {
         if ($storedfile->get_filesize() == 0 || preg_match('#^/.DS_Store|__MACOSX/#', $storedfile->get_filepath())) {
             continue;
         }
         if ($maxitems != 0 && $count >= $maxitems) {
             break;
         }
         $filename = $storedfile->get_filename();
         // Create an item.
         $data = new \stdClass();
         $data->caption = $filename;
         $data->description = '';
         $data->display = 1;
         $metafields = array('moralrights' => 1, 'originalauthor' => '', 'productiondate' => 0, 'medium' => '', 'publisher' => '', 'broadcaster' => '', 'reference' => '');
         foreach ($metafields as $field => $default) {
             $data->{$field} = isset($formdata->{$field}) ? $formdata->{$field} : $default;
         }
         $data->galleryid = $gallery->id;
         if (!$count) {
             $data->thumbnail = 1;
             $count = 0;
         }
         $item = self::create($data);
         // Copy the file into the correct area.
         $fileinfo = array('contextid' => $context->id, 'component' => 'mod_mediagallery', 'filearea' => 'item', 'itemid' => $item->id, 'filepath' => '/', 'filename' => $filename);
         if (!$fs->get_file($context->id, 'mod_mediagallery', 'item', $item->id, '/', $filename)) {
             $storedfile = $fs->create_file_from_storedfile($fileinfo, $storedfile);
         }
         $item->generate_image_by_type('lowres');
         $item->generate_image_by_type('thumbnail');
         $count++;
     }
     $fs->delete_area_files($context->id, 'mod_mediagallery', 'unpacktemp', 0);
 }
开发者ID:eSrem,项目名称:moodle-mod_mediagallery,代码行数:49,代码来源:item.php

示例9: update_references_to_storedfile

 /**
  * Updates all files that are referencing this file with the new contenthash
  * and filesize
  *
  * @param stored_file $storedfile
  */
 public function update_references_to_storedfile(stored_file $storedfile)
 {
     global $CFG, $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();
     $reference = self::pack_reference($params);
     $referencehash = sha1($reference);
     $sql = "SELECT repositoryid, id FROM {files_reference}\n                 WHERE referencehash = ? and reference = ?";
     $rs = $DB->get_recordset_sql($sql, array($referencehash, $reference));
     $now = time();
     foreach ($rs as $record) {
         require_once $CFG->dirroot . '/repository/lib.php';
         $repo = repository::get_instance($record->repositoryid);
         $lifetime = $repo->get_reference_file_lifetime($reference);
         $this->update_references($record->id, $now, $lifetime, $storedfile->get_contenthash(), $storedfile->get_filesize(), 0);
     }
     $rs->close();
 }
开发者ID:saurabh947,项目名称:MoodleLearning,代码行数:29,代码来源:file_storage.php

示例10: replace_content_with

 /**
  * Replace the content by providing another stored_file instance
  *
  * @param stored_file $storedfile
  */
 public function replace_content_with(stored_file $storedfile)
 {
     $contenthash = $storedfile->get_contenthash();
     $this->set_contenthash($contenthash);
     $this->set_filesize($storedfile->get_filesize());
 }
开发者ID:Jtgadbois,项目名称:Pedadida,代码行数:11,代码来源:stored_file.php

示例11: 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";
//.........这里部分代码省略.........
开发者ID:raymondAntonio,项目名称:moodle,代码行数:101,代码来源:filelib.php


注:本文中的stored_file::get_filesize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。