views:

792

answers:

3

I have a system where a user uploads documents (pdf, word) etc. The problem being, foreign users are uploading filenames in arabic, chinese, japanese and the system, being able to handle them, is adding them to the database.

Where the problem arises is trying to download the files using 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);

The system isn't recognising the filename, thus won't download the file. Any suggestions?

+1  A: 

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

Ish Kumar
A: 

If you need to store the filenames in MySQL, make sure you are having proper table and column collations, like utf8_unicode_ci. And don't forget to do mysql_query("SET NAMES utf8"); after connecting. This should be enough to store and retrieve Unicode strings properly.

As for the Content-Disposition header and non-ASCII filenames, there's already a good answer on this: "How to encode the filename parameter of Content-Disposition header in HTTP?"

drdaeman
+2  A: 

I have avoided this issue by generating a unique ID for each file uploaded and renaming the file using that id, then saving the id, original filename and extension in a database table. Then you can easily lookup the desired ID in the table, retrieve the original filename (which you may display for human readability) and extension and then download the {id}.{extension} file.

This also has the added benefit that if two files are uploaded with the exact same name, the latter upload won't overwrite the original.

Dustin Fineout
Used the same principle, works like charm :)
usoban