繁体   English   中英

在PHP中将BLOB映像从数据库保存到磁盘

[英]save BLOB image to disk from database in PHP

我已将图像存储在我的数据库中作为BLOB文件。 这可能不是最好的做法(我知道这一点)。 我想在点击时在我的页面上创建一个链接,并提示用户使用典型的保存文件提示,这样他们就可以将文件保存在他们的个人磁盘上。

我在网上进行了研究,但我正在尝试的东西似乎并不起作用,只是将字节发布到我的页面而不是文件中。 我保存文件的代码如下。 我正在使用一个框架,因此DB事务和retrevial​​代码可能看起来很奇怪但值正确设置。 这些文件在存储到DB之前也在base64中编码,因此我调用decode函数将其恢复到正常状态。

function save_file($file_id) {
    $result = $DB->get_file($file_id);   
    $size = $result->fields['size'];  //1024
    $mime = $result->fields['mime'];  //image/jpg
    $name = $result->fields['name'];  //test.jpg

    header("Content-Disposition:attachment;filename=".$name);
    header("Content-Type=: ".$mime);
    header("Content-Length: ".$size);

    base64_decode(file);
    echo($file);
    exit();
}

我认为你的Content-Type标题是错误的。 =似乎是一个错字。

function save_file($file_id, $target_folder) {
    $result       = $DB->get_file($file_id);   
    $file_content = $result->fields['your_file_content_field'];
    $name         = $result->fields['name'];

    /* Decode only if the file contents base64 encoded 
     * before inserting to database.
     */
    $file_content = base64_decode($file_content);

    return file_put_contents($target_folder.'/'.$name, $file_content);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM