Skip to content

Instantly share code, notes, and snippets.

@hayleyxyz
Created November 4, 2014 21:04
Show Gist options
  • Save hayleyxyz/9465a20c4e09d03c122a to your computer and use it in GitHub Desktop.
Save hayleyxyz/9465a20c4e09d03c122a to your computer and use it in GitHub Desktop.
Laravel workaround for Symphony Download Response Error: "the filename fallback must only contain ASCII characters". Extend the Symfony\Component\HttpFoundation\File\File object to return an ASCII-safe value for getFilename()
<?php
use Symfony\Component\HttpFoundation\File\File;
/*
This class is a hack/workaround for the InvalidArgumentException "The filename fallback must only contain ASCII characters."
for file downloads with non-ASCII filenames. Since the Response::download() method doesn't allow specifying a fallback name,
this is the only realistic option, aside from extending, rewriting, and maintaining a bunch of framework classes, or
modifying the framework itself.
*/
class AsciiSafeDownloadFile extends File {
/*
Take the original filename and ASCII-fy it
*/
public function getFilename() {
$orig = parent::getFilename();
return Str::ascii($orig);
}
}
<?php
class IndexController extends ExampleController {
protected function download(SplFileInfo $path) {
$file = new AsciiSafeDownloadFile($path->getPathname());
return Response::download($file, $path->getBasename());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment