2013-08-04 44 views
1

我在將數據從.txt文件傳輸到數據庫時遇到問題。我有10個.txt文件,並希望將其所有數據傳輸到我的數據庫。下面的代碼是我到目前爲止只想要一個.txt文件。它給錯誤。當用戶點擊上傳時,我會上傳文件夾中的zip文件到服務器。代碼是這樣的,它的工作原理如下:如何將數據從多個.txt文件傳輸到mysql

if(isset($_FILES['zip'])){ 
    $errors = array(); 

    $zip = new ZipArchive(); 

    if(strtolower(end(explode('.', $_FILES['zip']['name'])))!=='zip'){ 
     $errors[] = 'That does not look like a .zip file.'; 
    } 

    if($_FILES['zip']['size']>104857600){ 
     $errors[] = 'There is a size limit of 100MB'; 
    } 

    if($zip->open($_FILES['zip']['tmp_name'])==false){ 
     $errors[]='Failed to open zip file.'; 
    } 

    if(empty($errors)){ 
     $extracted_files = array(); 

     for($i=0;$i<$zip->numFiles;$i++){ 
      $entry_info = $zip->statIndex($i); 

      $extracted_files[] = $entry_info['name']; 
     } 

     print_r($extracted_files); 

     $zip->extractTo('./uploads'); 
     $zip->close(); 
    } 
} 

上傳zip文件並提取它們。現在我想從.txt文件讀取數據並填充我的數據庫。下面的代碼是我有,但我得到的錯誤,特別是文件的路徑。如果任何人可以請幫我下面的代碼,以及幫助文件路徑或建議我把文件放在另一個地方。代碼如下:

$string = file_get_contents("set_1.txt","r"); 
$myFile = "/Applications/MAMP/bin/mamp/uploads"; 
$fh = fOpen($myFile,'w') or die("could not open: " . mysql_error()); 
fwrite($fh, $string); 
fclose($fh); 

$sql = mysql_connect("localhost", "root","root"); 
if(!$sql){ 
    die("could not connect: " . mysql_error()); 
} 

mysql_select_db("Tweet_Corpora"); 
$result = mysql_query("LOAD DATA INFILE '$myfile'" . " INTO TABLE Display FIELDS TERMINATED BY '/\s+/'"); 

if(!$result){ 
    die("could not load. " . mysql_error()); 
} 

我的表看起來像這樣:

| ID | tweet_id | raw_tweet | normalized_tweet |

我只需要填寫tweet_id列和raw_tweet

我的數據看起來在每個文件如下:

57587072533413889 @NAYDIVAA1 thought I saw u today u know 
57743998223265792 The art of sleeping in the early morning and waking up at tea time. 
57817604059959296 RT @dilleeeey: I'm very very very hungry. But I'm lazy to eat$ 

請幫助。會真的很感激它。

+2

請問你能分享一個確切的錯誤信息嗎? – Sumurai8

+0

第一個錯誤:嚴格的標準:只有變量應該通過/ 12/:無法打開流:無法打開第43行 上的/Applications/MAMP/bin/mamp/home.php中的目錄: – user2650237

+0

第12行是什麼?第二個錯誤指​​向第二個代碼塊的第三行。在第二行你定義一個目錄而不是文件。 – Sumurai8

回答

0
$string = file_get_contents("set_1.txt","r"); 
$myFile = "/Applications/MAMP/bin/mamp/uploads"; 
$fh = fOpen($myFile,'w') or die("could not open: " . mysql_error()); 
fwrite($fh, $string); 
fclose($fh); 

在這部分中,由於沒有定義路徑,因此您可能會讀取根路徑中可能存在的文件的文件內容。

接下來,您在目錄上運行fopen並寫入一些內容。這不是它的工作原理。如果你想寫一些文件,你應該打開文件。我甚至不明白你爲什麼要寫入一個文件,而你已經有了一個包含內容的文件。更奇怪的是,在文件打開失敗的時刻顯示了mysql_error()。這兩個與彼此無關。你甚至沒有連接數據庫。

我不熟悉的LOAD DATA INFILE,但可能你的第一部分需要看起來像這樣:

$string = file_get_contents("/path/to/file/set_1.txt","r"); 

$sql = mysql_connect("localhost", "root","root"); 
if(!$sql){ 
    die("could not connect: " . mysql_error()); 
} 

mysql_select_db("Tweet_Corpora"); 
$result = mysql_query("LOAD DATA INFILE '$myfile'" . " INTO TABLE Display FIELDS TERMINATED BY '/\s+/'"); 

if(!$result){ 
    die("could not load. " . mysql_error()); 
} 

如果你有大的文件,你也許更好使用的fopen和閱讀的唯一部分文件。也許這個問題也可以幫助你: Read a text file and transfer contents to mysql database

+0

我做了您建議的更改,但已經有所幫助,但現在只出現一個錯誤: 嚴格標準:只有變量應通過/Applications/MAMP/bin/mamp/home.php在第12行參考傳遞 無法加載。你的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以找到正確的語法,以便在't find you -___- lol 57587072533413889 \t @ NAYDIVAA1'附近使用'以爲我今天看到你我知道5'在第1行 我很努力地理解它的含義。 – user2650237

相關問題