CRUDlex\StreamedFileResponse::getStreamedFileFunction PHP Method

getStreamedFileFunction() public method

Generates a lambda which is streaming the given file to standard output.
public getStreamedFileFunction ( string $file ) : Closure
$file string the filename to stream
return Closure the generated lambda
    public function getStreamedFileFunction($file)
    {
        return function () use($file) {
            set_time_limit(0);
            $handle = fopen($file, 'rb');
            if ($handle !== false) {
                $chunkSize = 1024 * 1024;
                while (!feof($handle)) {
                    $buffer = fread($handle, $chunkSize);
                    echo $buffer;
                    flush();
                }
                fclose($handle);
            }
        };
    }

Usage Example

コード例 #1
0
 /**
  * The controller for serving static files.
  *
  * @param Request $request
  * the current request
  * @param Application $app
  * the Silex application
  *
  * @return Response
  * redirects to the instance details page or 404 on invalid input
  */
 public function staticFile(Request $request, Application $app)
 {
     $fileParam = str_replace('..', '', $request->get('file'));
     $file = __DIR__ . '/../static/' . $fileParam;
     if (!$fileParam || !file_exists($file)) {
         return $this->getNotFoundPage($app, $app['translator']->trans('crudlex.resourceNotFound'));
     }
     $mimeTypes = new MimeTypes();
     $mimeType = $mimeTypes->getMimeType($file);
     $size = filesize($file);
     $streamedFileResponse = new StreamedFileResponse();
     $response = new StreamedResponse($streamedFileResponse->getStreamedFileFunction($file), 200, ['Content-Type' => $mimeType, 'Content-Disposition' => 'attachment; filename="' . basename($file) . '"', 'Content-length' => $size]);
     $response->setETag(filemtime($file))->setPublic()->isNotModified($request);
     $response->send();
     return $response;
 }
All Usage Examples Of CRUDlex\StreamedFileResponse::getStreamedFileFunction
StreamedFileResponse