2014-01-27 23 views
-2

我想創建一個在文件(文件類型:.doc,.docx,.pdf)中搜索的模塊。通過使用「file_get_contents()」我可以找到文件,但爲此我必須指定所有文件的位置。在我的情況下,我有許多文件夾中的文件(如:C:\ xampp \ htdocs \ cats1 \ attachments \ site_1 \ 0xxx ..)文件始終存儲在「0xxx」文件夾中(由其他應用程序)。我只想指定路徑,以便無論「0xxx」文件夾包含多少個「文件夾」,它都會在其中進行搜索。我對PHP很陌生,請幫忙。我的這個應用程序的代碼如下。在mysql數據庫的文本文件(.doc,.docx,.pdf等)中搜索

<?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); 
      } 
      ?> 
+0

不是文本文件,你就必須提取文本搜索他們 – 2014-01-27 23:13:08

+0

您的數據庫實際存儲的文件的文本,或者它存儲的文件的名稱和位置? – miyasudokoro

回答

3

您提到的文件不是文本文件。另外,將這些文件的內容存儲在數據庫中並不是一個好主意。這裏的方法我想借此:

  1. 商店使用他們的哈希(從產生的東西像 sha1())作爲文件名這些文件複製到文件存儲到文件系統。

  2. 創建一個表來存儲文件的元數據(文件名,上傳數據,散列號 名稱)。

  3. 在上面的表格中,創建一個text列來存儲 從文件中提取的文本。每種文件類型都需要一個 不同的工具。例如,對於PDF,您可以使用類似 pdftotext

  4. 通過選擇文本(散列) 從文本 列(或任何您想要的搜索條件)中包含關鍵字的表中進行數據庫搜索。

  5. 打開由返回散列命名的文件並將該文件返回給 用戶。