mysql存储word文档_上载Word文档,将其存储在mysql上并显示

bd96500e110b49cbb3cd949968f18be7.png

Hi I know php basics and that. I've recently rediscovered it and teaching myself new techniques. So please bear with me...

In this task, I'm to upload a MS Word document via php, store it on mysql and retrieve it when needed.

Is this possible? Should a file such as a .doc be stored on the same database as first name, last name, email etc. Or should I store it as a BLOB on a separate database for file uploads? I'm not sure if mysql is able to read every line on a .doc file? Thanks. Are there any examples online for beginners like myself?

I've already done the research. Some people say I shouldn't use mysql to store text files, other say I can.

I currently have:

First Name:

Last Name:

Email:

Select file to upload:

解决方案

There are a few topic to be discussed here.

The form

To upload the file, change the form enctype attribute.

:

Storing the file

You could store the file in a database, or just as file in the server disk system. Whatever you choose, it is not necessary to split the file into its lines. Store the file as a single unit.

It should suffice to say here that your database field should be an appropriate type and size to hold the file.

Personally, I am a fan on storing the file on disk, and the file name on the database.

File upload handling

You can save the file somwehere on disk. This is not the best way to do it, but the simplest to demostrate. There are enough example on SO, for example How to upload & Save Files with Desired name

$info = pathinfo($_FILES['upload']['name']);

$ext = $info['extension']; // get the extension of the file

$newname = "newname.".$ext;

$target = 'mydocs/'.$newname;

move_uploaded_file( $_FILES['upload']['tmp_name'], $target);

Downloading the file

To have the file displayed and download, merely print the contents out to the browser.

ob_start();

// do things. See below

ob_clean();

flush();

readfile($file);

ob_flush();

This will display the file and probably confuse the browser. To tell the browser to handle the file as a Word document, you must send the appropriate headers to the browser before sending the file.

ob_start();

if(isset($_REQUEST['dlink']))

{

$file = $_REQUEST['dlink'];

header('Content-Description: File Transfer');

header('Content-Type: application/octet-stream');

header('Content-Disposition: attachment; filename='.basename($file));

header('Content-Transfer-Encoding: binary');

header('Expires: 0');

header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

header('Pragma: public');

header('Content-Length: ' . filesize($file));

ob_clean();

flush();

readfile($file);

exit;

}

ob_flush();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值