Created
November 4, 2014 21:04
-
-
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()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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