Skip to main content

Laravel File Handling

The thing I like best about Laravel is its elegance.  As far as I'm concerned, it is the cleanest framework out there, even with its limitations.  So I just had to write this post about how I handle files in Laravel.  Note that the storage path can be configured to local storage or to an S3 bucket.

First, we need a route for opening the file in our routes.php file:

// Open a file
Route::get('file/{id}', 'HomeController@getFile');

Then, in our home controller, the related function:

/** * Open file
*/
public function getFile($filename)
{
// Full path
$path = storage_path('app/' . $filename);

// Determine the mime type
$mimetype = mime_content_type($path);

// Return the file
return response()->file($path, ['Content-Type: ' . $mimetype . '; charset=utf-8']);
}



That's it!  When you hit that URL route, the file will automatically open.

If you aren't sure whether your environment supports the mime_content_type() function, you can add a little bit of extra code (this part comes from php.net):


/**
* Open a file
*/
public function getFile($filename)
{
// Full path
$path = storage_path('app/' . $filename);

// Determine the mime type
if (function_exists('mime_content_type')) {
$mimetype = mime_content_type($path);
} else {
$mime_types = [
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'docx' => 'application/msword',
'xls' => 'application/vnd.ms-excel',
'xlsx' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'pptx' => 'application/vnd.ms-powerpoint',
'rtf' => 'application/rtf',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
];

// Get extension
$ext = strtolower(array_pop(explode('.', $filename)));

// Match to mime type
if (array_key_exists($ext, $mime_types)) {
$mimetype = $mime_types[$ext];
} else {
$mimetype = 'application/octet-stream';
}
}

// Return the file
return response()->file($path, ['Content-Type: ' . $mimetype . '; charset=utf-8']);
}

Comments