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]");
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
There is my html code of upload page:
<div id="pageDiv">
<section id="mainSection">
<div id="mainDiv">
<form action="upload.php" method="post" enctype="multipart/form-data">
<table id="mainTable">
<tbody>
<tr>
<td class="headColumn paragraphTitle"><label for="uploadedReport">競賽報告上傳</label></td>
<td><span class="hint"><span id="reportDueText"></span>截止</span></td>
</tr>
<tr>
<td><div id="uploadedReportDiv"><input id="uploadedReport" name="uploadedReport" type="file" required /></div></td>
<td><span class="hint">請以pdf格式上傳</span></td>
</tr>
<tr>
<td class="headColumn paragraphTitle"><label for="uploadedBriefing">競賽簡報上傳</label></td>
<td><span class="hint"><span id="briefingDueText"></span>截止</span></td>
</tr>
<tr>
<td><div id="uploadedBriefingDiv"><input id="uploadedBriefing" name="uploadedBriefing" type="file" required /></div></td>
<td><span class="hint">請以ppt, pptx 或pdf格式上傳</span></td>
</tr>
<tr><td colspan="2"><input id="uploadButton" type="submit" value=""/></td></tr>
<tr><td> </td><td> </td></tr>
<tr>
<td class="headColumn paragraphTitle">檢視已上傳檔案</td>
<td><a id="downloadUploadedFile" href="#">未上傳</a></td><!-- link to download file. If user has uploaded it, href will be changed by the javascript function afterUploaded -->
</tr>
</tbody>
</table>
</form>
</div>
</section>
</div>
The other side, this is my javascript code of upload page:
<script>
$(document).ready(
function()
{
setDateText(reportDue, "reportDueText");
setDateText(briefingDue, "briefingDueText");
afterUploaded();
if(now > reportDue)
{
$("#uploadedReportDiv").hide();
$("#uploadedReport").attr("required", false);
}
if(now > briefingDue || now <= reportDue)
{
$("#uploadedBriefingDiv").hide();
$("#uploadedBriefing").attr("required", false);
}
$("form").submit(
function()
{
var validateReport = false;
var validateBriefing = false;
if($("#uploadedReportDiv").is(":visible") && $("#uploadedReport").val().length > 0)
{
validateReport = validateUploadedFile($("#uploadedReport").val(), "pdf");
}
else if(!$("#uploadedReportDiv").is(":visible"))
validateReport = true;
if($("#uploadedBriefingDiv").is(":visible") && $("#uploadedBriefing").val().length > 0)
validateBriefing = validateUploadedFile($("#uploadedBriefing").val(), "pdf ppt pptx");
else if(!$("#uploadedBriefingDiv").is(":visible"))
validateBriefing = true;
if(!validateReport)
alert("檔案格式錯誤,請上傳pdf格式檔案。");//alert upload wrong file format
if(!validateBriefing)
alert("檔案格式錯誤,請上傳ppt, pptx 或pdf格式檔案。");//alert upload wrong file format
return (validateReport && validateBriefing);
}
);
}
);
function setDateText(date, objectId)
{
var dateText = (date.getFullYear()-1911) + "/" + (date.getMonth()+1) + "/" + (date.getDate());
$("#" + objectId).text(dateText);
}
function validateUploadedFile(filename, validExtensions)
{
var splitedArray = filename.split(".");
var fileExtension = splitedArray[splitedArray.length-1];
if(validExtensions.indexOf(fileExtension) == -1)
return false;
else
return true;
}
function afterUploaded()
{
if($.cookie("isUploaded"))
{
$("#downloadUploadedFile").attr("href", "download.php").text("下載檔案(" + $.cookie("uploadedFileName") + ")");
}
}
</script>
Thank you!