将大文本文件14.62 GB添加到DATABASE

时间:2017-07-13 20:43:10

标签: php mysql

我写了这个PHP代码:

<?php

$host = "localhost";
$username = "user";
$password = "pass";
$db = "database";

mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_close;


$lines = explode("\n", file_get_contents("textfile.txt"));
foreach($lines as $pass => $password)
{
  $string = trim($password);
$md5hash = md5($string);
$sha1hash = sha1($string);
$res = mysql_query("INSERT INTO `md5` VALUES ('', '$password', '$md5hash', '$sha1hash')") or die(mysql_error());

}
?>

但我还是得到了

  

允许的内存大小为734003200字节耗尽

     

致命错误:内存不足(已分配262144)(尝试分配15696126973字节)

我尝试了很多解决方案,但这没有帮助! 有没有办法通过更改或添加PHP代码来解决这个问题..

感谢

1 个答案:

答案 0 :(得分:5)

您正在阅读整个文件,然后在其上运行explode

你的代码试图分配15Gb的内存;)

您需要逐行阅读:

$fh = fopen("textfile.txt", "r");
while ($row = fgets($fh)) {
    // $row is the line. in your case $password
    $password = trim($row);
    $string   = trim($password);
    $md5hash  = md5($string);
    $sha1hash = sha1($string);

    $res = mysql_query("INSERT INTO `md5` VALUES ('', '$password', '$md5hash', '$sha1hash')") or die(mysql_error());
}
fclose($fh);