3

我有一个用户上传文档(pdf、word)等的系统。问题是,外国用户正在上传阿拉伯文、中文、日文的文件名,系统能够处理它们,将它们添加到数据库中。

出现问题的地方是尝试使用 php 下载文件:

$result = mysql_query($query) or die('Error, query failed');

list($filename, $type, $filesize, $filepath) = mysql_fetch_array($result);

header("Content-Disposition: attachment; filename=$filename");

header("Content-length: $filesize");

header("Content-type: $type");

readfile($filepath);

系统无法识别文件名,因此不会下载文件。有什么建议么?

4

3 回答 3

3

我通过为每个上传的文件生成一个唯一的 ID 并使用该 ID 重命名文件,然后将 ID、原始文件名和扩展名保存在数据库表中来避免这个问题。然后,您可以轻松地在表中查找所需的 ID,检索原始文件名(您可以显示以供人类阅读)和扩展名,然后下载 {id}.{extension} 文件。

这还有一个额外的好处,即如果以完全相同的名称上传两个文件,则后者的上传不会覆盖原始文件。

于 2009-07-09T15:31:23.167 回答
1

It's complicated for uploading Unicode names like ( e.g. 我是神.doc ) upto php 5 and linux, I suspect various OS does not support such file names

One alternative for you is to uploads them with some custom names may be {file-id}.doc and store their information (like original filename) in database table, And on download page you can modify the headers with information stored in table containing information for that file

于 2009-07-08T10:17:23.323 回答
1

如果您需要将文件名存储在 MySQL 中,请确保您具有正确的表和列排序规则,例如utf8_unicode_ci. 并且不要忘记mysql_query("SET NAMES utf8");在连接后做。这应该足以正确存储和检索 Unicode 字符串。

至于Content-Dispositionheader和non-ASCII filenames,已经有很好的答案了:“ How to encode the filename parameter of Content-Disposition header in HTTP?

于 2009-07-08T18:23:20.107 回答