在mysql数据库中搜索文本文件(.doc,.docx,.pdf等)

时间:2014-01-27 23:08:04

标签: php mysql pdf docx

我想创建一个在文件中搜索的模块(文件类型:.doc,.docx,.pdf)。通过使用“file_get_contents()”我可以找到文件,但为此我必须指定所有文件所在的位置。在我的情况下,我有许多文件夹中的文件(如:C:\ xampp \ htdocs \ cats1 \ attachments \ site_1 \ 0xxx ..)文件始终存储在“0xxx”文件夹中(由其他应用程序)。我只想指定路径,以便无论“0xxx”文件夹包含多少“文件夹”,它都会在其中进行搜索。我是php的新手,请帮忙。我在这个应用程序的代码如下。

 <?php
 $matched_files =   array();
 if(isset($_POST['submit']))
 {
 $skills    =   $_POST['skills'];
 $experience=   $_POST['experience'];
 $location  =   $_POST['location'];
 $path = 'C:\Docs';
 $dir = dir($path);
 // Get next file/dir name in directory
 while (false !== ($file = $dir->read()))
{   
if ($file != '.' && $file != '..')
{
    // Is this entry a file or directory?
    if (is_file($path . '/' . $file))
    {
        // Its a file, yay! Lets get the file's contents
        $data = file_get_contents($path . '/' . $file);

        // Is the str in the data (case-insensitive search)
        if (stripos($data, $skills) !== false and (stripos($data, $experience)  !==   false and (stripos($data, $location) !== false))) 
        {
    $matched_files[]    =   $file;

            }

    }
            }
            }
          $dir->close();
          $matched_files_unique =   array_unique($matched_files);
          }
          ?>

1 个答案:

答案 0 :(得分:3)

您提及的文件不是文本文件。此外,将这些文件的内容存储在数据库中并不是一个好主意。这是我要采取的方法:

  1. 使用哈希值生成这些文件(从类似的东西生成) sha1())作为将文件存储到文件系统的文件名。

  2. 创建一个表来存储元数据(文件名,数据上传,哈希 名称)文件。

  3. 在上述表格中,创建一个text列进行存储 从文件中提取的文本。每种文件类型都需要一个 不同的工具。例如,对于PDF,您可以使用类似的东西 pdftotext

  4. 通过选择文件名(哈希)在数据库中进行搜索 从表中包含关键字的文本中 列(或您想要的任何搜索条件)。

  5. 打开由返回的哈希命名的文件,并将该文件返回给 用户。