Skip to content

Instantly share code, notes, and snippets.

@Sentences
Forked from sparksp/libraries-file.php
Created October 23, 2012 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sentences/3938976 to your computer and use it in GitHub Desktop.
Save Sentences/3938976 to your computer and use it in GitHub Desktop.
Extended File for Laravel
<?php
/**
* File
*
* Note: Remember to remove the "File" alias in APP_DIR/config/application.php
*
* @author Phill Sparks <me@phills.me.uk>
*/
class File extends Laravel\File {
/**
* Create a response that will force a file to be downloaded.
*
* @param string $path
* @param string $name
* @return Response
*/
public static function inline($path, $name = null)
{
if (is_null($name))
{
$name = basename($path);
}
$response = Response::make(static::get($path));
$response->header('Content-Type', static::mime(static::extension($path)));
$response->header('Content-Disposition', 'inline; filename="'.$name.'"');
$response->header('Content-Transfer-Encoding', 'binary');
$response->header('Expires', 0);
$response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$response->header('Pragma', 'public');
$response->header('Content-Length', filesize($path));
return $response;
}
}
<?php
return array(
'GET /files/(:any)' => array('name' => 'file', 'do' => function($name) {
$path = STORAGE_PATH.'files/'.$name;
if (file_exists($path))
{
return File::inline($path);
}
return Response::error(404);
}),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment