Send file download response

@param string $file The path of file
@param array $downloadOptions The download options
@return $this
@throws \RuntimeException When file not found
184031
public function download($file = null, array $downloadOptions = array())
    {
        $o = $downloadOptions + $this->downloadOption;

        if (!is_file($file)) {
            throw new \RuntimeException('File not found', 404);
        }

        $name = $o['filename'] ? : basename($file);
        $name = rawurlencode($name);

        // For IE
        $userAgent = $this->request->getServer('HTTP_USER_AGENT');
        if (preg_match('/MSIE ([\w.]+)/', $userAgent)) {
            $filename = '=' . $name;
        } else {
            $filename = "*=UTF-8''" . $name;
        }

        $this->setHeader(array(
            'Content-Description' => 'File Transfer',
            'Content-Type' => $o['type'],
            'Content-Disposition' => $o['disposition'] . ';filename' . $filename,
            'Content-Transfer-Encoding' => 'binary',
            'Expires' => '0',
            'Cache-Control' => 'must-revalidate',
            'Pragma' => 'public',
            'Content-Length' => filesize($file),
        ));

        // Send headers
        $this->send();

        // Send file content
        readfile($file);

        return $this;
    }
558924
public function setFiles($file, $name = null)
	{
		// Verifying if is array
		if (is_array($file)) {
			foreach ($file as $k => $v) {
				$this->setFiles($v,$k);
			}
		} else {
			$parray = &$this->files;
			
			if (file_exists($file)) {
				if (is_null($file)) {
					if (isset($parray[$name])) unset($parray[$name]);
				} else {
					if (is_null($name) || is_integer($name)) {
						$count = count($parray);
						$name = 'file_contents_' . $count;
					}
						
					$parray[$name] = $file;
				}
			} else {
				throw new RuntimeException('Invalid file, file not exists.');
			}	
		}
		
		return $this;
	}
113016
private function download()
    {
        $file = (string) gps('file');

        try {
            $backups = $this->getBackups();
        } catch (Exception $e) {
        }

        if (empty($backups) || !isset($backups[$file])) {
            $this->browser(array(gTxt('rah_backup_can_not_download'), E_ERROR));
            return;
        }

        extract($backups[$file]);

        @ini_set('zlib.output_compression', 'Off');
        @set_time_limit(0);
        @ignore_user_abort(true);

        ob_clean();
        header('Content-Description: File Download');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.$name.'"; size="'.$size.'"');
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: private');
        header('Content-Length: '.$size);
        ob_flush();
        flush();

        if ($f = fopen($path, 'rb')) {
            while(!feof($f) && connection_status() == 0) {
                echo fread($f, 1024*64);
                ob_flush();
                flush();
            }

            fclose($f);
        }

        exit;
    }
201137
protected function downloadResponse($file, $name = null, array $headers = array())
	{
		$response = new BinaryFileResponse($file, 200, $headers, true, 'attachment');

		if ($name !== null) {
			return $response->setContentDisposition('attachment', $name, Str::ascii($name));
		}

		return $response;
	}
821031
public function fileDownload($file)
    {
        $response = new SymfonyResponse();

        $response->headers->set('Content-Disposition', 
            $response->headers->makeDisposition(
                ResponseHeaderBag::DISPOSITION_ATTACHMENT,
                $file
            )
        );

        return $response;
    }
1065410
public function uploadAvatar(string $file): Response
    {
        $this->requiresAccessToken();

        return $this->stream(
            'POST', 'profile/avatar', $this->getApiHeaders(), $this->getApiBody(), compact('file')
        );
    }
15265
public static function write($options)
    {
        $defaults = [
            'file' => null
        ];
        $options += $defaults;

        if (!$options['file']) {
            throw new RuntimeException("Missing file name");
        }

        return file_put_contents($options['file'], static::export($options));
    }
848255
public static function send($file, $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT, $fileName = null)
    {
        return MimeType::response($file, $disposition, $fileName)->send();
    }
395466
public function loadFile($file)
    {
        if (!is_file($file)) {
            throw new \RuntimeException(sprintf('File: %s not found.', $file));
        }

        $this->body = function () use ($file) {
            return file_get_contents($file);
        };

        $this->setFilename(pathinfo($file, PATHINFO_BASENAME));

        return $this;
    }
277821
public function toFile($file)
	{
		$minified = $this->minify();

		// Create dir
		$dir = dirname($file);

		if (!is_dir($dir))
		{
			if (is_file($dir))
			{
				throw new \RuntimeException(sprintf('Path: %s is already a file.', $dir));
			}

			if (!@mkdir($dir, 0755, true) && !is_dir($dir))
			{
				throw new \RuntimeException(sprintf('Auto create dir: %s fail.', $dir));
			}
		}

		file_put_contents($file, $minified);

		return $this;
	}
1054521
public function fork($file, $options = array(), $start = true)
    {
        return $this->parallel(function ($process) use ($file) {
            if (is_string($file) && is_file($file)) {
                include($file);
            } else {
                throw new \RuntimeException('Bad file');
            }
        }, $options, $start);
    }
819976
public function download(File $file, Download $command)
    {
        try {
            $response = $this->api->get($file->url);
        } catch (\Exception $e) {
            throw new InvalidDownloadException($e->getMessage());
        }

        if ($response->getStatusCode() == 200) {
            $response = $this->mutateHeaders($response, $file);

            return $response;
        }
    }
981892
public function download($url, $target, $callback = null)
    {
        if (!$this->filesystem->fileExists($target)) {
            $this->filesystem->dirCreate(dir($target));

            $request = new Request($url, $callback ?: function ($response) {
            });

            $request->options['file'] = $target;

            $this->add($request);
        }

        return $this;
    }
1260564
public function xSendFile($file, $options = array())
    {
        Yii::app()->request->xSendFile($file->resolvePath(), $options);
    }
1174244
public function download($file, $name = null, array $headers = array(), $disposition = 'attachment')
	{
		$response = new BinaryFileResponse($file, 200, $headers, true, $disposition);

		if ( ! is_null($name))
		{
			return $response->setContentDisposition($disposition, $name, str_replace('%', '', Str::ascii($name)));
		}

		return $response;
	}
354178
public function download($fileName, $url = null, $requestBody = null, $method = 'GET')
    {
        $result = $this->saveFile($fileName)->send($url, $requestBody, $method);
        $this->saveFileOption = array();
        return $result;
    }
855097
public function setOption($option, $value) {
        //Silence it because errors handled differently
        $res = @curl_setopt($this->handler, $option, $value);

        if($res === TRUE) {
            return $this;
        }

        throw new RuntimeException('Failed to set the following option: ' . $option);
    }