上载word文档,将其存储在mysql上并显示 [英] Uploading a word document, storing it on mysql and displaying it

查看:319
本文介绍了上载word文档,将其存储在mysql上并显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道php的基本知识.我最近重新发现了它,并自学了新技术.所以请忍受...

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

在此任务中,我要通过php上传MS Word文档,将其存储在mysql上,并在需要时进行检索.

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

这可能吗?应该将诸如.doc之类的文件与名字,姓氏,电子邮件等存储在同一数据库中.还是应该将其作为BLOB存储在单独的数据库中文件上传?我不确定mysql是否能够读取.doc文件中的每一行?谢谢.是否有网上示例供像我这样的初学者使用? 我已经完成了研究.有人说我不应该使用mysql存储文本文件,其他人说可以. 我目前有:

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:

<form action="insert.php" method="post">
<label>First Name:</label><input type="text" name="firstname" id="firstname"/><br>
<label>Last Name:</label><input type="text" name="lastname" id="lastname"/><br>
<label>Email:</label><input type="text" name="email" id="email"/><br>
<div><label id="upload">Select file to upload:
<input type="file" id="upload" name="upload"/></label></div><br>
     <input type="submit" name=submit value="Submit"/>
</form>

推荐答案

这里有一些主题需要讨论.

There are a few topic to be discussed here.

表格

要上传文件,请更改表单enctype属性.

To upload the file, change the form enctype attribute.

<form action="insert.php" method="post" enctype="multipart/form-data">
    :
</form>

存储文件

您可以将文件存储在数据库中,也可以将其存储为服务器磁盘系统中的文件.无论您选择什么,都无需将文件分成几行.将文件存储为一个单元.

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.

文件上传处理

您可以将文件somwehere保存在磁盘上.这不是执行此操作的最佳方法,而是最简单的演示方法.在SO上有足够的示例,例如如何上传&保存具有所需名称的文件

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();

这将显示文件,并且可能会使浏览器混乱.要告诉浏览器将该文件作为Word文档进行处理,必须在发送文件之前将适当的标头发送给浏览器.

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();

这篇关于上载word文档,将其存储在mysql上并显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆