I have a page that allows users to download files stored in a database. This page simply uses a GET variable to find the corresponding ID in the database table and return the file as an attachment in the header of the file.
header("Content-length: $objFile->size");
header("Content-type: $objFile->type");
header("Content-Disposition: attachment; filename=".($objFile->name));
开发者_Go百科The issue I have is if the file uploaded into the database contains a space in the name, for instance "3 Year Spending Analysis.pdf", then when the file is returned for download the file name comes up as just "3" due to the space in the name.
I've tried urlencode, rawurlencode and others but not getting the expected results of the full file name.
Put it inside quotes:
header('Content-Disposition: attachment; filename="'.($objFile->name).'"');
Here is the relevant part of the HTTP specification.
Update: Technically, this should read
str_replace('"', '\\"', $objFile->name)
because the HTTP spec states that a double quote character inside a quoted-string
must be escaped with a backslash. In practice you don't expect filenames with double quotes in them (I 'm not even sure which filesystems allow it), but for 100% guaranteed compliance the str_replace
is needed.
精彩评论