我在將數據從.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$
請幫助。會真的很感激它。
請問你能分享一個確切的錯誤信息嗎? – Sumurai8
第一個錯誤:嚴格的標準:只有變量應該通過/ 12/:無法打開流:無法打開第43行 上的/Applications/MAMP/bin/mamp/home.php中的目錄: – user2650237
第12行是什麼?第二個錯誤指向第二個代碼塊的第三行。在第二行你定義一個目錄而不是文件。 – Sumurai8