hello i have text file names.txt inside it many lines ( names ) can i creat php code to read the file and put all the names in database ?
Thats relatively easy: Code: $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); mysql_select_db('mydb', $link); $lines = file("/path/to/your.txt"); foreach($lines as $line) { $name = trim($line); mysql_query("INSERT INTO mytable (myname) VALUES ('$name')",$link); } mysql_close($link);
thank you very much but if the file to big how can i make the insert in the database on parts and how i know if the process done without error
How big? If its not larger then say 100 or 100 MB, this should work. You will just have to set the memory limit in PHP to a higher value. Otherwise youz will have to take a look in the PHP documentation, you will find there als file functions to read a file line by line.
just 2 gb i did but i don't find any thing but i searched in google and i found that PHP: $file = "file.txt"; $fh = fopen($file, "rb"); //feof = end of file while(!feof($fh)) { $line = fgets($fh); //fgets reads one line at a time //do something } PHP: $read_lim = 8192; //size in bytes to load into memory $file = "test.txt"; $fh = fopen($file, "rb"); $size = filesize($file); //total file size while ($size > 0) { $rlen = ($size > $read_lim) ? $read_lim : $size; //read length $buffer = fread($fh, $rlen); //do something $size -= $rlen; } what the best ?