長時間閱讀器,第一次海報。我對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());
}
}
}
?>
由於提前, 邁克
thany你皮特, 我已經嘗試了一些東西,但不斷收到錯誤 開捕致命錯誤:傳遞給RecursiveIteratorIterator參數1 :: __構造()必須實現接口Traversable的 而且可以」噸通過它 – Mike