无法从PHP中的mongoDB GridFS下载文件

时间:2014-09-16 09:08:16

标签: php html mongodb gridfs

我正在尝试制作一个用于上传和显示mongoDB和PHP文件的小程序 正在正常上传文件。

我已经将2-3MB的简单图像文件上传到mongoDB中的gridFS,但是我的下载代码提供了长字节数以及html代码作为输出

我想下载临时文件以在浏览器中显示它。我应该在这段代码中做些什么改变

上传文件代码:

<?php
if(isset($_POST['formsubmit']))
{
    $m = new MongoClient();
    $coll = $m->test;
    $gridFS = $coll->getGridFS();
    $tag = $_POST['username'];
    $gridFS->storeUpload('pic',array("tag"=>$tag));
    echo 'File Uploaded Successfully';
    $m->close();
}
?>

<html>
<head><title>Upload Image</title></head>
<body>
    <form method="POST" enctype="multipart/form-data">
        <label for="username">Username:</label>
        <input type="text" name="username" id="username" />
        <label for="pic">Please upload a profile picture:</label>
        <input type="file" name="pic" id="pic" />
        <input type="submit" value="Register" name="formsubmit"/>
    </form>
</body>
</html>

下载文件的代码:

<html>
    <head> Download Image</head>
    <body>
        <form method="POST" enctype="multipart/form-data">
            <label for="username">Username:</label>
            <input type="text" name="username" id="username" />
            <input type="submit" value="Register" name="formsubmit"/>
        </form>
    </body>
</html>

<?php
if (isset($_POST['formsubmit'])) {
    $m = new MongoClient();
    $coll = $m->test;
    $images = $coll->getGridFS();

    $image = $images->findOne(array(
        'tag' => $_POST['username'],
    ));

    header('Content-type: image/png;');
    echo $image->getBytes();
}
?>

如果在浏览器中打开,则显示以下错误

  

图片&#34; localhost / PHP / Project_working / fileretrive.php&#34;   无法显示,因为它包含错误。

0 个答案:

没有答案