Thursday, September 12, 2013

PHP failed to download uploaded file from database since uploaded with chinese filename

PHP failed to download uploaded file from database since uploaded with
chinese filename

Excuse me, I am building a web site that can let user upload their
document(pdf) to the mysql database.
Since I have wrote the upload and download php page, I could upload
files(pdf) and download english named files successfully, but fail to
download the files whose name contain chinese words.
More specifically, I can download the chinese named file, but couldn't
open it. It shows that the file is damaged.
Here is a part of code of my upload php page:
$connect = mysqli_connect("localhost", "root", "password", "table");
if(mysqli_connect_errno($connect))//check connection
{
/*show error message and die*/
}
//set client character set to 'utf8'
mysqli_set_charset($connect, "utf8");
$fileName = mysqli_real_escape_string($connect, $name);
$filePath = mysqli_real_escape_string($connect, $tmp_name);
$fileSize = mysqli_real_escape_string($connect, $size);
$fileType = mysqli_real_escape_string($connect, $type);
$content = mysqli_real_escape_string($connect,
file_get_contents($tmp_name));
$filePath = addslashes($filePath);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$teamName = $_COOKIE['teamName'];
$reportQuery = "UPDATE uploadedreport SET name='$fileName',
type='$fileType', size='$fileSize', content='$content' WHERE
team='$teamName'";
uploadFileQuery($connect, $reportQuery, $fileName);
mysqli_close($connect);
Besides, another part of code of download php page is:
$teamName = $_COOKIE['teamName'];
$downloadReportQuery = "SELECT name, type, size, content FROM
uploadedreport WHERE team='$teamName'";
$result = mysqli_query($connect, $downloadReportQuery);
if($result == false)
{
/*alert error message and die*/
}
$row = mysqli_fetch_array($result);
header("Content-length:$row[size]");
header("Content-type:$row[type];charset:UTF-8");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=$row[name]");//Tells
the browser to save this downloaded file under the specified name
echo $row["content"];
mysqli_free_result($result);
mysqli_close($connect);
It's there anything wrong? I am disturbed by this problem for several days.
Thanks for your help!
Using Notepad++ to open the damaged downloaded pdf file: little part of
downloaded pdf

No comments:

Post a Comment