Jump to content

PHP adding content of multiple files to a MySQL Database Problems.


supamastadan

Recommended Posts

Hello.  My name is Dan and I have yet another question for all the PHP code gurus out there.  What i'm trying to do is write a script to look into a directory filled with text files.  Then it will read each file, save the content into a variable and then send that information along with some others into a table on a MySQL database.  Here's what I have.

 

<?php 

//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","user","password"); 

//select which database you want to edit
mysql_select_db("funksand_archive");

$dirname = ".";
$dh = opendir( $dirname ) or die("couldn't open directory");

while (!(( $file = readdir( $dh ) ) === false ) ) {
     if ( is_dir( "$dirname/$file" ) )
          print "";
     $new[] = $file;
}

foreach($new as $filename){
     $fp=fopen($filename, 'r') or die("Cannot Open $file");
          while ( ! feof($fp)){
          $keywords=fread($fp,filesize($filename)); 
     $date = substr_replace ($filename, "", 10, 4);   
     $type="Rant";
     $title="Dan Rant $date";
     $number="--";
   if($filename!="." && $filename!="..")

$result=MYSQL_QUERY("INSERT INTO archive (id,date,type,number,title,keywords)".
   "VALUES ('NULL', '$date', '$type', '$number', '$title', '$keywords')");
}

}

?>

 

It connects just fine (Obviously i didn't include my username and password) and when I have it just print the values instead of query, the pages shows all the conent.

 

The problem I'm having is that despite every other variable entered in, the keywords only seem to happan sparaticly, i.e. out of about 70 text files, only about 17 show up with content in the keywords collum.  I'm fairly new at this, so there might be something really obvious that I'm missing, and fread might not even be the right command I want.  Any quick way to fix this or should I try something new completely?  Thanks ahead of time for you help.  Have a good one and take care.

 

- Dan

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.