0

I'm working on a project that the user will be Upload several files with different MIME Types and I want to save the files to a DataBase(Mysql with InnoDB engeen). Now These are my Questions:
1/ Should I Create several Tables for every MIME type or may be different rows for every type?
2/ I tested BLOB in rows type in mysql for field's type, but it seems there is a problem with DB!!!
--either I tried MEDIUM BLOB and LONG BLOB--
3/ If I have to save every MIME type in a different row or table, Which type is OK for this iletypes:
a/pdf b/jpeg c/png d/gif e/video/mp4 f/application/word

4

1 回答 1

0

这是我刚刚测试的一个小例子

表:文件

id(主键,自动递增)

文件名(varchar 255)

类型(varchar 255)

文件(大块)

编码:utf8_unicode_ci

<?php


$hostname = 'localhost';
$username = 'root';
$password = '';

try
{
    $dbh = new PDO("mysql:host=$hostname;dbname=test", $username, $password);
    echo 'Connected to database';
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = $dbh->prepare("INSERT INTO `files` (filename, type, file) VALUES('img.png', 'image/png', :bin)");
    $sql->bindParam("bin", file_get_contents("img.png"));
    $sql->execute();

    $sql = $dbh->prepare("SELECT * FROM `files` WHERE `filename`='img.png'");
    $sql->execute();
    $result = $sql->fetch();

    $file = fopen("new-".$result['filename'], "w+");
    $w = fwrite($file, $result['file']);
    fclose($file);

}
catch(PDOException $e)
{
    echo $e->getMessage();
}
?>

对于您要执行的操作:

  1. 您使用上传的临时文件而不是固定文件。

  2. 您使用该文件的 MIME 类型(您可以使用 php 获取它)

  3. 当您需要读取文件时,您只需读取 BLOB 字段并将其写入使用文件名作为名称的文件。

我没有注意到 BLOB 字段有任何问题。如果您无法解决该问题,您可以使用 TEXT 字段并将 base64_encode(file) 存储到其中。

于 2013-10-26T10:43:24.847 回答