2014-06-26 43 views
1

長時間閱讀器,第一次海報。我對PHP的認識足夠危險,這是我第一個使用它的BIG項目。使用html2text設置目錄以包含子目錄

一些背景資料:

我有一個從舊新聞採集程序生成的超過100萬(是的,百萬).html文件。這些.html文件包含需要每天搜索的重要檔案信息。我還沒有找到其他服務器,這可能會有更多的2-3百萬+是不可能的。

我正在使用這些.html文件並將它們傳輸到mysql數據庫中。至少,到目前爲止,代碼已經在數百個測試文件中運行得非常好。我會在最後附上代碼。

當.html文件被歸檔時,問題就開始了,它是生成無法更改歸檔的框的功能,是文件進入文件夾。它們分爲這樣

archives>year>month>file.html

這樣的例子是

archives>2002>05may>lots and lots of files.html 
archives>2002>06june>lots and lots of files.html 
archives>2002>07july>lots and lots of files.html 

的幫助和研究,我寫的代碼剝離標記,包括html2text和simple_html_dom的文件,並從每個標籤把信息在我的數據庫中的適當的領域,這很好。但是所有的文件都需要移動到相同的目錄才能正常工作。再次,超過一百萬甚至更多的其他服務器需要很長時間才能移動。我正在使用批處理文件來立即複製文件。

我的問題是這樣的:

我可以使用某種形式的通配符來定義所有的子目錄,所以我沒有將所有的文件,他們可以在統計各自的目錄?

頂我的代碼:

// Enter absolute path of folder with HTML files in it here (include trailing slash): 
$directory = "C:\\wamp1\\www\\name\\search\\files\\"; 

子目錄是files目錄下。

在我搜索答案時,我看到了「你爲什麼要這麼做?」或其他問題詢問有關目錄中的.exe文件或.bat文件,以及它可能如何危險,所以不要這樣做。我的問題是這些HTML文件,所以沒有任何被稱爲或運行,沒有危險。

這裏是我剝離html到數據庫中的代碼。再一次,工作很好,但我想跳過必須將所有文件移動到一個目錄的步驟。

<?php 

// Enter absolute path of folder with HTML files in it here (include trailing slash): 
$directory = "C:\\wamp1\\www\\wdaf\\search\\files\\"; 

// Enter MySQL database variables here: 
$db_hostname = "localhost"; 
$db_username = "root"; 
$db_password = "password"; 
$db_name = "dbname"; 
$db_tablename = "dbtablename"; 

///////////////////////////////////////////////////////////////////////////////////// 
// Include these files to strip all characters that we don't want 
include_once("simple_html_dom.php"); 
include_once("html2text.php"); 

//Connect to the database 
mysql_connect($db_hostname, $db_username, $db_password) or trigger_error("Unable to connect to the database host: " . mysql_error()); 
mysql_select_db($db_name) or trigger_error("Unable to switch to the database: " . mysql_error()); 

//scan the directory and look for all the htmls files 
$files = scandir($directory); 
for ($filen = 0; $filen < count($files); $filen++) { 
    $html = file_get_html($directory . $files[$filen]); 

    // first check if $html->find exists 
    if (method_exists($html,"find")) { 

     // then check if the html element exists to avoid trying to parse non-html 
     if ($html->find('html')) { 

        //Get the filename of the file from which it will extract 
      $filename = $files[$filen]; 

      //define the path of the files 
      $path = "./files/"; 

      //Combine the patha and filename 
      $fullpath = $path . $filename; 


      // Get our variables from the HTML: Starts with 0 as the title field so use alternate ids starting with 1 for the information 
      $slug = mysql_real_escape_string(convert_html_to_text($html->find('td', 8))); 
      $tape = mysql_real_escape_string(convert_html_to_text($html->find('td', 9))); 
      $format0 = mysql_real_escape_string(convert_html_to_text($html->find('td', 10))); 
      $time0 = mysql_real_escape_string(convert_html_to_text($html->find('td', 11))); 
      $writer = mysql_real_escape_string(convert_html_to_text($html->find('td', 12))); 
      $newscast = mysql_real_escape_string(convert_html_to_text($html->find('td', 13))); 
      $modified = mysql_real_escape_string(convert_html_to_text($html->find('td', 14))); 
      $by0 = mysql_real_escape_string(convert_html_to_text($html->find('td', 15))); 
      $productionCues = mysql_real_escape_string(convert_html_to_text($html->find('td', 16))); 
      $script = mysql_real_escape_string(convert_html_to_text($html->find('td', 18))); 

      // Insert variables into a row in the MySQL table: 
      $sql = "INSERT INTO " . $db_tablename . " (`path`, `fullpath`, `filename`, `slug`, `tape`, `format0`, `time0`, `writer`, `newscast`, `modified`, `by0`, `productionCues`, `script`) VALUES ('" . $path . "', '" . $fullpath . "', '" . $filename . "', '" . $slug . "', '" . $tape . "', '" . $format0 . "', '" . $time0 . "', '" . $writer . "', '" . $newscast . "', '" . $modified . "', '" . $by0 . "', '" . $productionCues . "', '" . $script . "');"; 
      $sql_return = mysql_query($sql) or trigger_error("Query Failed: " . mysql_error()); 
     } 
    } 
} 
?> 

由於提前, 邁克

回答

-1

我不知道需要多長時間之前,你的PHP查詢超時服用,但有一個內置功能RecursiveDirectoryIterator這聽起來像它可能做的伎倆您。

+0

thany你皮特, 我已經嘗試了一些東西,但不斷收到錯誤 開捕致命錯誤:傳遞給RecursiveIteratorIterator參數1 :: __構造()必須實現接口Traversable的 而且可以」噸通過它 – Mike

0

只是想更新這個職位,回答我的問題,工作得很好。藉助一些幫助,我們發現遞歸使用scandir來創建一個數組是可行的。 我以爲我會張貼這個,所以如果其他人正在尋找類似的東西,他們將不會看得很遠!我知道我喜歡看到答案!

的代碼是從第二用戶貢獻筆記這裏有一些修改:http://php.net/manual/en/function.scandir.php

所以在我上面的代碼,我取代

//scan the directory and look for all the htmls files 
$files = scandir($directory); 
for ($filen = 0; $filen < count($files); $filen++) { 
    $html = file_get_html($directory . $files[$filen]); 

function import_dir($directory, $db_tablename) { 

    $cdir = scandir($directory); 
    foreach ($cdir as $key => $value) 
    { 
     if (!in_array($value,array(".",".."))) 
     { 
     if (is_dir($directory . DIRECTORY_SEPARATOR . $value)) 
     { 
      // Item in this directory is sub-directory... 
      import_dir($directory . DIRECTORY_SEPARATOR . $value,$db_tablename); 
     } 
     else 
      // Item in this directory is a file... 
     { 
      $html = file_get_html($directory . DIRECTORY_SEPARATOR . $value); 

,然後替換爲

//Get the filename of the file from which it will extract 
      $filename = $files[$filen]; 

      //define the path of the files 
      $path = "./files/"; 

      //Combine the patha and filename 
      $fullpath = $path . $filename; 

//Get the filename of the file from which it will extract 
        $filename = mysql_real_escape_string($value); 

        //define the path of the files 
        $path = mysql_real_escape_string($directory . DIRECTORY_SEPARATOR); 

        //Combine the patha and filename 
        $fullpath = $path . $value; 

感謝那些誰回答!

邁克

相關問題