php打开,读取,传入/从mysql数据库传输

php打开,读取,传入/从mysql数据库传输

问题描述:

I've read some posts where a php file opens and writes the info to a database. But it has a delimeter such as | or , or ..

However, I have a txt file that deals with numbers and the delimter is a space between numbers.

For example user 1 with userlevel of site 12 and admin status of 12:

977970 12 12

Another example of multiple users:

977970 password 12 12
100490 password 3  12

Each new line is a new user with their indiviual details.

And the second half to this, is that it needs to update the text file with new users. I'm sure the update will need a cron job running.

我读过一些php文件打开的帖子,并将信息写入数据库。 但它有一个分隔符,如 | code>或, code>或。 code>。 p>

但是,我有一个 处理数字和分隔符的txt文件是数字之间的空格。 p>

例如用户1的用户级别为12,管理状态为12: p> \ n

  977970 12 12 
  code>  pre> 
 
 

多个用户的另一个例子: p> \ n

  977970密码12 12 
100490密码3 12 
  code>  pre> 
 
 

每个新行都是 具有不同细节的新用户。 p>

下半部分是需要用新用户更新文本文件。 我确定更新需要运行cron作业。 p> div>

If i understood the problem right. You are trying to read from a file and store it into a database where your records are delimited by space & I haven't checked the code but it should work or at least give you an idea

<?php
    $data = Array();
    $handle = @fopen("/Your/File.txt", "r");  
    if ($handle) {
       while (($line = fgets($handle)) !== false) {
          $data[]= $line;                    // read each line and store in array
       }
       if (!feof($handle)) {
           echo "Error";
       }
       fclose($handle);
       getDelimitedVals($data);
   }

your function to separate the record

   function getDelimitedVals($data) {
       if(empty($data)) {
           return false;
       }
       foreach($data as $line) {
           $dbData[] = explode(" ", $line);     // this will separate the uname, pwd, id and store in new arry
       }

       return dbData;
   }

To answer the first part, there are quite a number of ways to parse a 'something' delimited file, but here's a pretty simple solution:

$contents = file('filename/goes/here');

foreach($contents AS $line) {
    $values = explode(' ', $line);
    /**
     *  $values = array(
     *      '977970',
     *      'password',
     *      '12',
     *      '12'
     *  )
     */
}