import txt file and file name into mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peterhall
Forum Newbie
Posts: 24
Joined: Sat Aug 21, 2010 5:47 pm

import txt file and file name into mysql

Post 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.
User avatar
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

Post 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.
peterhall
Forum Newbie
Posts: 24
Joined: Sat Aug 21, 2010 5:47 pm

Re: import txt file and file name into mysql

Post 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
User avatar
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

Post 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.
peterhall
Forum Newbie
Posts: 24
Joined: Sat Aug 21, 2010 5:47 pm

Re: import txt file and file name into mysql

Post by peterhall »

thank you for the reply. I'll change my code and try the way you mention...
peterhall
Forum Newbie
Posts: 24
Joined: Sat Aug 21, 2010 5:47 pm

Re: import txt file and file name into mysql

Post 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. :o
Post Reply