drv54591 2013-10-26 08:54
浏览 27
已采纳

使用一个表将不同的MIME类型上载到DataBase

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

  • 写回答

1条回答 默认 最新

  • dousi8931 2013-10-26 10:43
    关注

    Here is a small example I just tested

    Table: files

    id (primary key, auto increment)

    filename (varchar 255)

    type (varchar 255)

    file (largeblob)

    encoding: 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();
    }
    ?>
    

    For what you are trying to do:

    1. Instead of fixed file you use the temporary file that was uploaded.

    2. You use the MIME type of that file (you can get it with php)

    3. When you need to read the file you simply read the BLOB field and write it to a file using the filename as the name.

    I did not notice any problems with the BLOB fields. If you can't resolve that problem you could use a TEXT field and store base64_encode(file) into that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题