PDA

View Full Version : PHP script to import textfiles into table


mat
04-18-2004, 11:38 PM
I have a MySQL table which remote users need to be able to add/replace records to by uploading a text file from their machine. (similar to phpmyadmin's 'Insert data from a textfile into table')

I was thinking that I would be able to do this with php by: uploading the file - reading the file line by line adding insert/update statements and then executing the query.

Has anyone done anything like this here before? I haven't been able to find any examples on the web.

mat
04-19-2004, 03:46 AM
"LOAD DATA INFILE" Is what I needed. This seems to work well:


if (isset($_POST['submitted'])){

$file = $_FILES['txtfile']['tmp_name'];
chmod($file, 0664);

$sql = "LOAD DATA INFILE '".$file."' REPLACE INTO TABLE `tablename` FIELDS TERMINATED BY '*' ESCAPED BY '\\\\' LINES TERMINATED BY '\r'";

$update = mysql_query($sql) or die(mysql_error());
}