PHP上传/下载移动上传的文件



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

编辑:当前上传.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);
}
                ?>

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

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

再写一行

exit;

也许这会对你有所帮助。

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`)
)

最新更新