Elgg\FileService\File::getURL PHP Method

getURL() public method

Returns publicly accessible URL
public getURL ( ) : string | false
return string | false
    public function getURL()
    {
        if (!$this->file instanceof \ElggFile || !$this->file->exists()) {
            elgg_log("Unable to resolve resource URL for a file that does not exist on filestore");
            return false;
        }
        $relative_path = '';
        $root_prefix = _elgg_services()->config->getDataPath();
        $path = $this->file->getFilenameOnFilestore();
        if (substr($path, 0, strlen($root_prefix)) == $root_prefix) {
            $relative_path = substr($path, strlen($root_prefix));
        }
        if (!$relative_path) {
            elgg_log("Unable to resolve relative path of the file on the filestore");
            return false;
        }
        $data = array('expires' => isset($this->expires) ? $this->expires : 0, 'last_updated' => filemtime($this->file->getFilenameOnFilestore()), 'disposition' => $this->disposition == self::INLINE ? 'i' : 'a', 'path' => $relative_path);
        if ($this->use_cookie) {
            $data['cookie'] = _elgg_services()->session->getId();
            if (empty($data['cookie'])) {
                return false;
            }
            $data['use_cookie'] = 1;
        } else {
            $data['use_cookie'] = 0;
        }
        ksort($data);
        $mac = _elgg_services()->crypto->getHmac($data)->getToken();
        $url_segments = array('serve-file', "e{$data['expires']}", "l{$data['last_updated']}", "d{$data['disposition']}", "c{$data['use_cookie']}", $mac, $relative_path);
        return elgg_normalize_url(implode('/', $url_segments));
    }

Usage Example

Example #1
0
 function createRequest(\Elgg\FileService\File $file)
 {
     $site_url = elgg_get_site_url();
     $url = $file->getURL();
     $path = substr($url, strlen($site_url));
     $path_key = \Elgg\Application::GET_PATH_KEY;
     $request = \Elgg\Http\Request::create("?{$path_key}={$path}");
     $cookie_name = _elgg_services()->config->getCookieConfig()['session']['name'];
     $session_id = _elgg_services()->session->getId();
     $request->cookies->set($cookie_name, $session_id);
     return $request;
 }