Page 1 of 1
import txt file and file name into mysql
Posted: Sun Sep 26, 2010 2:04 pm
by peterhall
hi guys. is it possible to import a txt file to mysql using php script and, at the same time, increase the file name in the same table? the idea is to have something that makes the difference in the table, because the files are always the same, where the only thing that change is the date in the file name.
thanks.
Re: import txt file and file name into mysql
Posted: Sun Sep 26, 2010 2:07 pm
by John Cartwright
peterhall wrote:hi guys. is it possible to import a txt file to mysql using php script and, at the same time, increase the file name in the same table? the idea is to have something that makes the difference in the table, because the files are always the same, where the only thing that change is the date in the file name.
thanks.
You will need to clarify what you are talking about, perhaps with some examples.
Re: import txt file and file name into mysql
Posted: Mon Sep 27, 2010 5:47 pm
by peterhall
You will need to clarify what you are talking about, perhaps with some examples.
I'm importing a txt file into mysql with php everyday. the txt files have 5 primary fields and 40 columns with different data. in the primary keys, there's no date or other field that could make a diference between each file, but, the file names area different, eg:
fileA_20100925.txt
fileB_20100926.txt
fileC_20100927.txt
etc...
Now, what I'm trying to do, when the php script is importing the file, is that the name of the file be inserted into the same table, eg:
fileA_20100925.txt
field1 field2 field3 field4 field5 field6 field7 field8 --> in the txt file
mysql table
fileA_20100925 field1 field2 field3 field4 field5 field6 field7 field8 --> after importing
---------------------------------------------------------//---------------------------------------------------------------
or, if there's a way to have an multiple autoincrement to when I'm importing the files, the script or in mysql add the same number to all the lines imported from that file.
was clear?
thank you for your help
Re: import txt file and file name into mysql
Posted: Mon Sep 27, 2010 6:20 pm
by Jonah Bron
Loop through the directory with opendir. That gives you the file name to insert into MySQL. Use file_get_contents to get the contents.
Re: import txt file and file name into mysql
Posted: Mon Sep 27, 2010 7:59 pm
by peterhall
thank you for the reply. I'll change my code and try the way you mention...
Re: import txt file and file name into mysql
Posted: Thu Oct 21, 2010 7:57 am
by peterhall
Now that I have the time, I can share what I have done to complete this post:
Code: Select all
<?php
// connect to data base
$con = mysql_connect('localhost','root','');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('database', $con);
// search for all the files with this pattern in dir
foreach (glob("C:/dir1/dir2/file*.txt") as $file) {
echo "file size de $file " . filesize($file) . "<br/>";
// insert file names in table
$query = "INSERT INTO tabel_name (file ) ".
"VALUES ('$file')";
mysql_query($query) or die('Error, query failed');
}
echo "<br>File $file uploaded<br>";
?>
If you want to remove part of the name, just need to change the query for 'left' or 'right' and characters number.
