SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2000
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!

    I've got about 400 text files that I want to insert into a database.

    I've created the database and it looks something like this..
    DBname:texts
    and the fields are: ID,Name,Text and Date.

    I've thought about doing like #ls -la > textfiles.txt
    so i then have all the names of the files in one big file.
    I guess i would have to do a php-script that reads the textfiles.txt and insert each filename into an array...Then I'm stuck
    Anyone got a slightest guess how I would perform this?

    CHEERS!
    Magnus

  2. #2
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What format are the text files in? is it one record per text file , or does each file contain serveral records, if so how are they delimited?

    this would help us help you.

  3. #3
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,939
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes more information is needed. But it should be pretty easy.

    I made a script today actually that did just this (for my specific text files). I used it to insert over 8000 quotes from text files into my db.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2000
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The text files are log files from an IRC channel so it's just one record per file. I guess they're in ASCII format.

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are we taking a MySQL database here?

    If so check out LOAD DATA INFILE http://www.mysql.com/doc/L/O/LOAD_DATA.html
    That will allow you to construct a mysql sql query that will insert the contents of a file into a table in your database.

    In your PHP script you can loop through all the files and inserting them into the database one-by-one.

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2000
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That was exactly what I had in mind Sid But how do I create the script that reads the file with all names and inserts them into an array? and furthermore how do I do the loop?
    Does php set a number to each array in a variable? like
    $arraysample[blah.txt] = $arraysample[0]
    $arraysample[hehe.txt] = $arraysample[1]
    $arraysample[hoho.txt] = $arraysample[2] and so on..

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2000
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't really figure out is how to use the arrays indexed number to insert the file with that number...
    let's pretend $var[textfile.txt] gets the indexed value 0.
    how can I use the $var[0] to insert $var[textfile.txt] into the database?

  8. #8
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This (untested) should read the contents of all .txt
    files in a given directory and put them into a MySQL table.
    This assumes you have created $tablename in
    $database_name with at least 1 field $fieldname.

    Code:
    <?
    //this reads the files//
    $handle=opendir("path_to_file_directory"); 
    while ($file = readdir($handle)) { 
        if (substr($file,-3,3)=="txt"){
    			$temp=fopen("$file","r");
    			$contents[$x]=fread($temp,filesize($file));
    			fclose($temp);
    			$x++;
    		    } 
    }
    closedir($handle); 
    
    //connect to db//
    mysql_connect("localhost",$username,$password);mysql_select_db($database_name);
    
    //enter records into db//
    for($x=0;$x<count($contents);$x++){
      $SQL=mysql_query("INSERT INTO $tablename ($fieldname) VALUES ('$contents[$x]')");
      $errors[$x]=mysql_error();
    }
    ?>
    Should work out of the box ?

    assuming the script appears to run - you may want to check
    the $errors[] array for anywhere a problem may have occured.

    You may also depedent on your system need to addslashes($contents[$x])
    before you stick them in the DB and/or str_replace any special characters.

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2000
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fire...uhmm...I can't see any code

    Well, i've made to create the lines that reads the content of the file that contains the filenames and insert them into an array and echoes all names.

    it looks something like this...
    <?
    $fcontents = file ('/www/servers/monge/logs/logs.txt');
    while (list ($line_num, $line) = each ($fcontents))
    {
    echo "$line <br>";
    }
    ?>
    so what's left is the loop that inserts both the name of the file and its content into a table with ID Content and Name fields.

  10. #10
    SitePoint Enthusiast
    Join Date
    Oct 2000
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what I have come up with so far.. the problem is that I keep getting theese errors :
    Warning: fopen("#indiepop_20010214.log ","r") - No such file or directory in
    /c/www/server/loggarna/fileupload.php on line 7
    and
    Warning: Supplied argument is not a valid File-Handle resource in
    /c/www/server/loggarna/fileupload.php on line 8
    (this is repeated for all files)

    <?
    $fcontents = file ('logs.txt');
    while (list ($line_num, $line) = each ($fcontents)) {

    $filename = "$line";
    $fd = fopen ($filename, "r");
    $contents = fread ($fd, filesize ($filename));
    fclose ($fd);

    $login="xxxxx";
    $pass="xxxxx";
    $db=mysql_connect("localhost",$login,$pass);
    mysql_select_db("Loggarna",$db);
    $sql= "insert into Logs (Name, Content) values ('$filename','$contents')";
    $result=mysql_query($sql,$db);

    }
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •