php上传/下载移动上传文件

时间:2012-05-17 12:53:15

标签: php mysql upload download

我的上传/下载过程出现问题。我的文件作为BLOB插入到mysql中,但是在下载时它会抓取调用php页面的html标记。在插入数据库之前,是否需要将上传的文件移动到临时目录?

编辑:当前的upload.php文件。仍然获得html内容而不是实际文件本身,它仍然正确命名。

           <?php
                    // Make sure an ID was passed DOWNLOAD HANDLER *******
if(isset($_GET['id'])) {
// Get the ID
    $id = intval($_GET['id']); var_dump($id);


    require_once ('../mysqli_connect.php'); //Connect to the db



        // Fetch the file information
        $downloadq = "
            SELECT `file_type`, `size`, `title`, 'content', 'upload_id'
            FROM `upload`
            WHERE `upload_id` =".$id;
        $result = mysqli_query ($dbc, $downloadq); // Run the query


        if($result) {
            // Make sure the result is valid
            if (mysqli_num_rows($result) > 0) {
            // Get the row
                $row = mysqli_fetch_assoc($result);
                //var_dump($row);

                // Print headers
                header("Content-Type: application/msword");
                header("Content-Length: ". $row['size']);
               header("Content-Disposition: attachment; filename=". $row['title']);
               header("Content-Transfer-Encoding: binary");


                // Print data
                echo (stripslashes($row['content']));


            }
            else {
                echo 'Error! No such ID.';
            }

            // Free the mysqli resources
            mysqli_free_result($result);
        }
        else {
            echo "Error! Query failed: <pre>{$dbc->error}</pre>";
        }
        mysqli_close($dbc);
}
                ?>

2 个答案:

答案 0 :(得分:0)

你可以试试这个。 在以下行之后

echo (stripslashes($row['content']));

再写一行

exit;

也许这会对你有帮助。

答案 1 :(得分:0)

CREATE TABLE `file` (
    `id`        Int Unsigned Not Null Auto_Increment,
    `name`      VarChar(255) Not Null Default 'Untitled.txt',
    `mime`      VarChar(50) Not Null Default 'text/plain',
    `size`      BigInt Unsigned Not Null Default 0,
    `data`      MediumBlob Not Null,
    `created`   DateTime Not Null,
    PRIMARY KEY (`id`)
)