0

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>&nbsp;</td><td>&nbsp;</td></tr>
                        <tr>
                            <td class="headColumn paragraphTitle">檢視已上傳檔案&lt;/td>
                            <td><a id="downloadUploadedFile" href="#">未上傳&lt;/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!

4

2 回答 2

0

可能,将二进制文件数据存储在数据库中并不是一个好主意。如果您在 move_uploaded_file() PHP 函数的帮助下将上传的文件移动到某个目录,并且只将文件名放入数据库中会更好。

于 2013-09-13T03:38:41.123 回答
0

从您的回答中可以清楚地看出,在您从数据库中读取文件后,该文件并不相同,因此很明显那里出了点问题。

MySQL 确实支持二进制数据类型。但是,仅仅因为您可以,并不意味着您应该这样做。MySQL 一次将结果发送到客户端。因此,任何使用二进制数据解析结果集的应用程序都需要等待每一行到达,然后才能对其进行处理。此外,将二进制数据存储在 MySQL 中并没有真正的好处。

更好的二进制数据方法是将数据存储在文件系统上,并将指向这些文件的指针存储在 MySQL 中。使用这种方法,您实际上可以在处理结果集时在后台线程中流式传输二进制数据。

这个技巧不仅仅适用于二进制数据;它适用于任何类型的大数据对象。困扰二进制数据的性能问题也困扰着字符数据。换句话说,结果集的任何部分的读取都是串行完成的。您会更加注意二进制数据,因为它通常很大。您会注意到大字符数据同样存在的问题。您确实需要权衡在文件系统上存储大字符数据的性能优势与在数据库中搜索该数据的能力。

但是,如果您坚持将文件存储到数据库中,那么请务必使用BLOB类型字段。请参阅此处的相关手册页。

于 2013-09-16T08:44:09.867 回答