如果我想保存来自使用 PHP 和 MySQL 的用户的文件,我将使用什么数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2342950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:06:35  来源:igfitidea点击:

What data type would I use if I want to save a file from a user using PHP and MySQL?

phpmysqlfiletypes

提问by Sam Dark

I want users to be able to upload their CV in PDF, .txt, .doc, .docx; and allow potential employers to download the file.

我希望用户能够以 PDF、.txt、.doc、.docx 格式上传他们的简历;并允许潜在雇主下载该文件。

What data type should I use? In MS MSQL, I would use varbinary(max), right? But since I'm new to MySQL I'm a bit confused. :)

我应该使用什么数据类型?在 MS MSQL 中,我会使用 varbinary(max),对吗?但是由于我是 MySQL 的新手,所以我有点困惑。:)

回答by Svetlozar Angelov

You have to use the BLOBtype

你必须使用BLOB类型

回答by Sam Dark

You should use varchar holding a path to the file. File itself should not be stored in database.

您应该使用 varchar 保存文件的路径。文件本身不应存储在数据库中。

回答by ruzz

no one should mention the whole don't store files in a db to flickr. :)

没有人应该提到整个不要将文件存储在 db 中到 flickr。:)

Storing files, even large ones, in the DB is a question about your hardware more than a programming practice. if your server (or server farm) can handle the business there's no real disadvantage to it, and there are advantages. the principal one being that files in a db aren't "stuck" to the server. files go wherever the database goes, and are replicated as required.

在 DB 中存储文件,甚至是大文件,与其说是编程实践,不如说是关于您的硬件的问题。如果您的服务器(或服务器群)可以处理业务,那么它就没有真正的劣势,并且有优势。主要是数据库中的文件没有“卡住”到服务器。文件放在数据库去的任何地方,并根据需要进行复制。

as with everything, no one answer is correct.. you have to make your best decision based on your actual project requirements and future plans.

与所有事情一样,没有一个答案是正确的……您必须根据您的实际项目要求和未来计划做出最佳决定。

回答by BOB

At this stage both options above has its pros and cons. the questions is,

在这个阶段,上述两种选择各有利弊。问题是,

  • does the size of the file increase when stored as a blob and
  • does the performance suffer,
  • 存储为 blob 时文件的大小是否会增加?
  • 性能是否受到影响,

relative to storing and downloading it from a server.

相对于从服务器存储和下载它。

回答by 786

filename.phpusing form post method in this file

filename.php在这个文件中使用 form post 方法

Upload File<a href="download.php?f=<?php echo $upload_file ?>"><?php echo $upload_file ?></a>&nbsp;<input type="file" name="uploaded_file" id="uploaded_file" value="">

Get the posted file in php

在php中获取发布的文件

$file = $_FILES['uploaded_file'];    //receive the posted file
    $name = $file['name'];
    $path = "C:/wamp/www/mantis/uploads/" . basename($name); // 5
    if (move_uploaded_file($file['tmp_name'], $path)) {

    } else {

    }
**mysql query**
insert into tablename(upload_file)values('".$name."');

回答by Andrew Kolesnikov

Its usually a bad idea to store binary documents like DOC in MySQL. I would just upload them to the filesystem or convert to plaintext before saving to MySQL as BLOB or varchar.

在 MySQL 中存储像 DOC 这样的二进制文档通常是一个坏主意。在将它们保存为 BLOB 或 varchar 之前,我只会将它们上传到文件系统或转换为纯文本。

回答by JPro

I agree with most of them above. Is is generally not a good idea to store files in the database unlike Facebook which stores thumbnails in the database.

我同意上面的大多数。与将缩略图存储在数据库中的 Facebook 不同,将文件存储在数据库中通常不是一个好主意。