Sends downloadable headers for a file

@param string $filename A path to the target file
@param string $alias Basename name can be optionally changed
@throws \RuntimeException If can't access the target file
@return void
499064
public function download($target, $alias = null)
    {
        $this->preload($target, $alias);

        // Count file size in bytes
        $size = filesize($target);

        // multipart-download and download resuming support
        if (isset($_SERVER['HTTP_RANGE'])) {

            list($a, $range) = explode("=", $_SERVER['HTTP_RANGE'], 2);
            list($range) = explode(",", $range, 2);
            list($range, $range_end) = explode("-", $range);

            $range = intval($range);

            if (!$range_end) {
                $range_end = $size - 1;
            } else {
                $range_end = intval($range_end);
            }

            $new_length = $range_end - $range + 1;

            $headers = array(
                'Content-Length' => $new_length,
                'Content-Range' => sprintf('bytes %s', $range - $range_end / $size)
            );

            $this->headerBag->setStatusCode(206)
                            ->setPairs($headers)
                            ->send();
        } else {

            $new_length = $size;
            $this->headerBag->appendPair('Content-Length', $size)
                            ->send()
                            ->clear();
        }

        $chunksize = 1024 * 1024;
        $bytes_send = 0;

        $target = fopen($target, 'r');

        if (isset($_SERVER['HTTP_RANGE'])) {
            fseek($target, $range);
        }

        while (!feof($target) && (!connection_aborted()) && ($bytes_send < $new_length)) {

            $buffer = fread($target, $chunksize);
            print($buffer);
            flush();
            $bytes_send += strlen($buffer);
        }

        fclose($target);
    }
1062822
protected static function downloadExportFile( $filename, $inline ) {
		$filepath = static::getExportFolder() . $filename;
		\Pressbooks\Redirect\force_download( $filepath, $inline );
	}
141383
public function addConfigFile($file, $alias = null) {

        $filePath = $this->findConfigFile($file);
        if (!is_string($alias)) {
            $this->configFiles[] = $filePath;
        } else {
            $this->configFiles[$alias] = $filePath;
        }
    }
177888
public function handleRename(string $fileName): void
	{
		if ($this->presenter->isAjax() && $this->editable) {
			$this->template->files = [$this->rename($fileName)];
			$this->redrawControl('itemsContainer');
		} else {
			exit;
		}
	}
283092
public function updateFile($filename, $prefix)
    {
        $this->fvFilename = $filename;
        $this->fvPrefix = $prefix;
        $this->save();
        $this->logVersionUpdate(self::UT_REPLACE_FILE);
    }
611438
public function as_download( $filename = null ) 
	{
		if ( is_null( $filename ) ) 
		{
			$filename = 'file.'.CCStr::suffix( $this->header( 'Content-Type' ), '/' );
		}

		$this->header( 'Content-Description', 'File Transfer' );
		$this->header( 'Content-Disposition', 'attachment; filename='.$filename );
		$this->header( 'Content-Transfer-Encoding', 'binary' );
		$this->header( 'Expires', '0' );
		$this->header( 'Cache-Control', 'must-revalidate' );
		$this->header( 'Pragma', 'public' );
		$this->header( 'Content-Length', strlen( $this->body() ) );

		return $this;
	}
251340
public function setFileName( $fileName ){
		$this->fileName	= $fileName;
		$this->sheet	= self::load( $fileName );
	}
183316
public function sendToBrowserDownload($filename)
    {
        ReportRenderer::sendToBrowser(
            $filename,
            ReportRenderer::CSV_FORMAT,
            "text/" . ReportRenderer::CSV_FORMAT,
            $this->getRenderedReport()
        );
    }
1031605
public function setDownload($filename = false) {
        $h = "attachment;";
        if ($filename) {
            $h .= "filename='" . $filename . "'";
        }
        $this['response']->setHeader("Content-Disposition", $h);
    }
853217
private function _openFile($filename)
    {
        // Check local tmp directory
        if (!is_dir($this->config['tmp']) || !is_writeable($this->config['tmp'])) {
            $this->fail('tmp directory not found or is not writable');
        }

        // Download file to local tmp directory
        $tmp_file = $this->config['tmp'] . "/ftp_data_file.tmp";

        if ($this->isSFTP()) {
            $downloaded = @$this->ftp->get($filename, $tmp_file);
        } else {
            $downloaded = @ftp_get($this->ftp, $tmp_file, $filename, FTP_BINARY);
        }
        if (!$downloaded) {
            $this->fail('failed to download file to tmp directory');
        }

        // Open file content to variable
        if ($this->file = file_get_contents($tmp_file)) {
            $this->filepath = $filename;
        } else {
            $this->fail('failed to open tmp file');
        }
    }
192437
public function saveToFile($FileName = null)
    {
        if (! is_null($FileName)) {
            $this->FileName = $FileName;
        }

        if (($this->_openSHPFile(true)) && ($this->_openSHXFile(true)) && ($this->_createDBFFile())) {
            $this->_saveHeaders();
            $this->_saveRecords();
            $this->_closeSHPFile();
            $this->_closeSHXFile();
            $this->_closeDBFFile();
        } else {
            return false;
        }
    }
843705
public function setFileName($fileName)
    {
        $this->fileName = $fileName;
        $this->setContentType(mime_content_type($this->fileName));
    }
484901
protected function loadFile($filename)
    {
        if (isset($this->aliases[$filename])) {
            $filename = $this->aliases[$filename];
        }

        switch (pathinfo($filename, PATHINFO_EXTENSION)) {
            case 'js':
            case 'json':
                $level = error_reporting(0);
                $json = file_get_contents($filename);
                error_reporting($level);

                if ($json === false) {
                    $err = error_get_last();
                    throw new InvalidArgumentException("Unable to open {$filename}: " . $err['message']);
                }

                $config = json_decode($json, true);
                // Throw an exception if there was an error loading the file
                if ($error = json_last_error()) {
                    $message = isset(self::$jsonErrors[$error]) ? self::$jsonErrors[$error] : 'Unknown error';
                    throw new RuntimeException("Error loading JSON data from {$filename}: ({$error}) - {$message}");
                }
                break;
            case 'php':
                if (!is_readable($filename)) {
                    throw new InvalidArgumentException("Unable to open {$filename} for reading");
                }
                $config = require $filename;
                if (!is_array($config)) {
                    throw new InvalidArgumentException('PHP files must return an array of configuration data');
                }
                break;
            default:
                throw new InvalidArgumentException('Unknown file extension: ' . $filename);
        }

        // Keep track of this file being loaded to prevent infinite recursion
        $this->loadedFiles[$filename] = true;

        // Merge include files into the configuration array
        $this->mergeIncludes($config, dirname($filename));

        return $config;
    }
691534
public function clientWrite($filename, $source)
    {
        $path = $this->clientPath($filename);
        file_put_contents($path, $source);
    }
1265036
protected static function _downloadCoverFile( $filename ) {
		$filepath = static::getCoversFolder() . $filename;
		\Pressbooks\Redirect\force_download( $filepath );
		exit;
	}
824531
public function downloadExistsFile($fileName, $aliasOfFileName = '')
    {
        $baseFileName = $aliasOfFileName === '' ? basename($fileName) : $aliasOfFileName;

        header("Content-Type: text/csv; charset={$this->getHeaderCharset()}");
        header("Content-Disposition: attachment; filename={$baseFileName}");

        readfile($fileName);
    }
287409
public function handleDownload($filename, $filehash)
	{
		$message = $this->cache->load($filename);
		$file = $message->attachments[$filehash];
		header("Content-Type: $file->type; charset='UTF-8'");
		header("Content-Transfer-Encoding: base64");
		header("Content-Disposition: attachment; filename=\"$file->filename\"");
		header('Content-Length: ' . strlen($file->data));
		echo base64_decode($file->data);
		exit;
	}