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.
import txt file and file name into mysql
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: import txt file and file name into mysql
You will need to clarify what you are talking about, perhaps with some examples.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.
Re: import txt file and file name into mysql
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:You will need to clarify what you are talking about, perhaps with some examples.
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
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: import txt file and file name into mysql
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
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
Now that I have the time, I can share what I have done to complete this post:
If you want to remove part of the name, just need to change the query for 'left' or 'right' and characters number. 
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>";
?>