douyu1656
2013-10-24 13:22
浏览 62
已采纳

如何在php中将文件从数据库保存到服务器

I have a database table to store my templates(.docx file). I am using openTbs to work on that templates.My need is that to take out the template and store it in thee server itself, and edit using openTbs.

My question is that how I can retrieve .docx file from database and store that in the server side.? (The storing may be temporary but i want to edit it using openTbs.)

Thanks in advance.......

图片转代码服务由CSDN问答提供 功能建议

我有一个数据库表来存储我的模板(.docx文件)。 我使用openTbs来处理模板。我需要的是取出模板并将其存储在服务器本身,并使用openTbs进行编辑。

我的问题是我如何从数据库中检索.docx文件并将其存储在服务器端。 (存储可能是暂时的,但我想使用openTbs进行编辑。)

提前致谢.......

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dongtao5055 2013-10-24 15:55
    最佳回答

    It is bad practice to save binary data in the database, mostly when this data are intend to be processed by a file server, such as pictures or your DOCX.

    Anyway, since OpenTBS 1.8.1, you can open an DOCX (or any Ms Office, LibreOffie or zip archive) from a PHP file handle.

    You can use this feature to easily merge your template from the database :

    // retrieve binary data of the file
    $rs = mysql_query($dbc, "SELECT data FROM files WHERE id=$id");
    $rec= mysql_fetch_array($rs, MYSQLI_ASSOC);
    mysql_free($rs);
    
    // create a temporary file
    $temp = tmpfile();
    fwrite($temp, $rec['file']);
    unset($rec); // free PHP memory
    
    // Open the file with OpenTBS
    $TBS = new clsTinyButStrong;
    $TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);
    $TBS->LoadTemplate($temp);
    ...
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题