This (untested) should read the contents of all .txt
files in a given directory and put them into a MySQL table.
This assumes you have created $tablename in
$database_name with at least 1 field $fieldname.
Code:
<?
//this reads the files//
$handle=opendir("path_to_file_directory");
while ($file = readdir($handle)) {
if (substr($file,-3,3)=="txt"){
$temp=fopen("$file","r");
$contents[$x]=fread($temp,filesize($file));
fclose($temp);
$x++;
}
}
closedir($handle);
//connect to db//
mysql_connect("localhost",$username,$password);mysql_select_db($database_name);
//enter records into db//
for($x=0;$x<count($contents);$x++){
$SQL=mysql_query("INSERT INTO $tablename ($fieldname) VALUES ('$contents[$x]')");
$errors[$x]=mysql_error();
}
?>
Should work out of the box ?
assuming the script appears to run - you may want to check
the $errors[] array for anywhere a problem may have occured.
You may also depedent on your system need to addslashes($contents[$x])
before you stick them in the DB and/or str_replace any special characters.
Bookmarks