使用php将多个pdf文件添加到blob

时间:2015-02-17 08:29:16

标签: php mysql

我必须使用PHP将遗留数据库迁移到MySQL。旧系统的一个组件是大型(> 1000)附加文件列表(大多数是.pdf,有些是.doc / .docx)。

我到目前为止做了以下事情:

  1. 创建了mysql表,用blob来包含文件(这个方法有 被选中是有正当理由的)
  2. 导入除附件之外的所有数据
  3. 将所有附件解压缩到一个文件夹中,使用唯一名称命名每个文件
  4. 在mysql中添加并填充了一个包含附件唯一名称的列
  5. 创建了一个文件,其中包含文件夹中所有附件的全名。
  6. 添加单个文件是我在堆栈中找到的唯一解决方案

    但我的要求是RECURSIVELY浏览文件名列表并在mysql中找到匹配的行,然后将文件夹中的文件插入blob

    示例......

    我有一个包含这样的条目的文本文件(文件名,创建日期,大小)..

    024048DE44C-RUE_MA.pdf,05/24/2013,80233 
    024048DE44C-RUE.pdf,06/21/2013,85151 
    ... (1000 more)
    

    我在文件夹中有一个名为024048DE44C-RUE_MA.pdf的文件 我在mysql数据库中有一个包含filename的行列024048DE44C-RUE_MA.pdf

    我认为解决方案是这样的,但在中间位需要帮助。

    $myfile = fopen("filelist.txt", "r") or die("Unable to open file!");
    while(!feof($myfile)) {
    $attfile =fgets($myfile);
    

    ...上传文件

    // Check if a file has been uploaded
        if(isset($_FILES['uploaded_file'])) {
            // Make sure the file was sent without errors
            if($_FILES['uploaded_file']['error'] == 0) {
                // Connect to the database
                $dbLink = new mysqli('localhost', 'root', '', 'dbase');
                if(mysqli_connect_errno()) {
                    die("MySQL connection failed: ". mysqli_connect_error());
                }
    
                // Gather all required data
                $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
                $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
                $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
                $size = intval($_FILES['uploaded_file']['size']);
    
                // Create the SQL query
                $query = "
                    INSERT INTO `deal_attachment` (
                        `deal_att_name`, `deal_att_mime`, `deal_att_size`, `deal_att_content`, `deal_att_created`
                    )
                    VALUES (
                        '{$name}', '{$mime}', {$size}, '{$data}', NOW()
                    )";
    
                // Execute the query
                $result = $dbLink->query($query);
    
                // Check if it was successfull
                if($result) {
                    echo 'Success! Your file was successfully added!';
                }
                else {
                    echo 'Error! Failed to insert the file'
                       . "<pre>{$dbLink->error}</pre>";
                }
            }
            else {
                echo 'An error accured while the file was being uploaded. '
                   . 'Error code: '. intval($_FILES['uploaded_file']['error']);
            }
    
            // Close the mysql connection
            $dbLink->close();
        }
        else {
            echo 'Error! A file was not sent!';
        }
    
        }
    fclose($myfile);
    

2 个答案:

答案 0 :(得分:1)

如果你已经拥有一个带文件名的表,为什么不使用mysql LOAD_FILE函数呢?

 UPDATE mytable SET blobdata=LOAD_FILE(concat("/path/to/", filename));

您的文件需要在您的mysql服务器上才能运行。

如果你唯一的问题是如何阅读CSV文件,你可以这样做:

 list($filename,$date, $size)=explode(",", $attfile);

答案 1 :(得分:0)

Adam的建议对我来说非常合适,因为Windows中的重要附带条件是文件路径必须包含双反斜杠(所以你要像这样构造它         $ path ='c:\\ dir \\ subdir \\'。 trim($ filename);.

以下代码段显示了调用..

    $query = "UPDATE `attachments` SET `att_content`=LOAD_FILE('" .$path . "') WHERE `att_name` = '" . $filename ."'" ; 

    $result = mysqli_query($dbcon,$query) ;

// Check if it was successful (maybe add some more insurance by going back to the database to see if the file is there - it can be that no error was reported but LOAD_FILE didn't work (like if the path is invalid)

    if (false === $result) {
         echo mysqli_error($dbcon);
        die ;
    } else  { echo 'Success! Your file was successfully added!'; }