简体   繁体   English

在php中读取文本文件时出错

[英]Error reading text file in php

I have a file I need to import into a database. 我有一个文件,我需要导入到数据库中。 (My database is good, I can connect and I can add). (我的数据库很好,我可以连接,我可以添加)。 Now my problem is for some reason nothing gets inserted. 现在我的问题是由于某种原因没有插入任何内容。

I have a file schooldatabase.txt of users/password I need to add to a database. 我有一个文件schooldatabase.txt用户/密码我需要添加到数据库。 The file has 200 lines. 该文件有200行。

Here's a sample: 这是一个示例:

test|098f6bcd4621d373cade4e832627b4f6
test2|ad0234829205b9033196ba818f7a872b

Now for each of these line (student username and password) I have to insert them in a database. 现在,对于这些行(学生用户名和密码)中的每一行,我必须将它们插入数据库中。

Here's my code: 这是我的代码:

function addUser($user,$pass) {
// this code is good
}

function processUser($user,$pass) {
  $pass=md5($pass);
  $myFile = "schooldatabase.txt";
  $fh = fopen($myFile, 'r');
  $theData = fread($fh, 5);
  $login = "$user|$pass";
  if(stristr($theData,$login) !== false){
      $result = "rejected";
  }
  elseif(stristr($theData,$login) !== true){
      addUser($user,$pass); // this work I manuall tested
      $result = "accepted";
   }
   fclose($fh);
   return $result;
}
var_dump(processUser('invaliduser','test2'));

Why it return "accepted" if that user is not in the file? 如果该用户不在文件中,为什么返回“已接受”?

I think here you should re-think about your process. 我想在这里你应该重新考虑一下你的过程。 I assume you "processUser" more than one time therefore you will open/read/close the same file over and over without altering that file. 我假设您“processUser”不止一次,因此您将反复打开/读取/关闭同一文件而不更改该文件。

Because the file is not huge (and I assume it's a one-time-script), Just open the file in memory when you start your script then you can compare all the value you are testing with that file. 因为文件不是很大(我认为它是一次性脚本),所以只需在启动脚本时在内存中打开文件,然后就可以将正在测试的所有值与该文件进行比较。

You can use the function file to do so. 您可以使用函数文件来执行此操作。 Then you can check if the user exists using in_array . 然后,您可以使用in_array检查用户是否存在。

Here's the script: 这是脚本:

function addUser($user,$pass) {
// this code is good
}

$file = file("schooldatabase.txt", FILE_IGNORE_NEW_LINES ^ FILE_SKIP_EMPTY_LINES);

function processUser($user,$pass, array &$file) {
  $pass = md5($pass);
  if(in_array("$user|$pass", $file)) {
    addUser($user,$pass); // do you check if the query is good?
    return 'accepted';
  } 
  return "rejected";
}

var_dump(processUser('invaliduser','test2', $file));

I think you're overcomplicating the if a bit -- it's either true or false, so no need to check that stristr twice! 我想你的过于复杂if一点-这是真或假,所以没必要检查stristr两次! Also, you might have your true/false mixed up. 此外,您可能将您的真/假混淆了。

Edit: Also, it should probably be stripos, which will return the position or false. 编辑:此外,它应该是stripos,它将返回位置或false。

Try... 尝试...

if(stripos($theData,$login) === false){
    $result = "rejected";
} else {
    addUser($user,$pass); // this work I manuall tested
    $result = "accepted";
}

...does that work? ...那样有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM