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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With