Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

php - PHP从文本文件读取到MySQL(PHP Read from text file to MySQL)

I'm trying to make a script, in PHP, that reads from every txt file in a directory and writes the data to a MySQL database.

(我正在尝试用PHP创建一个脚本,该脚本从目录中的每个txt文件读取并将数据写入MySQL数据库。)

Right now the script runs on a local Wordpress environment however, nothing seems to happen when the script runs.

(现在,脚本在本地Wordpress环境上运行,但是在脚本运行时似乎什么也没发生。)

I know there is a lot of bad variable names etc. in the code, it will be fixed once it's working.

(我知道代码中有很多错误的变量名,等等,一旦工作就会解决。)

Thank you in advance!

(先感谢您!)

function TxtToDB()
{
    $directory = 'C:Usersuser-Local Sitesaxadevapppublicwp-contentuploadswp_dndcf7_uploadswpcf7-files';
    $conn = mysqli_connect('localhost', 'root', 'root', 'mysql');
    mysqli_connect();
    if (!$conn) {
        die(mysqli_error());
    }

    if (!is_dir($directory)) {
        exit('Invalid diretory path');
    }

    $files = array();
    foreach (scandir($directory) as $file) {
        while (!feof($file)) {
            $getTextLine = fgets($file);
            $explodeLine = explode(",", $getTextLine);

            list($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z, $de) = $explodeLine;

            $qry = "insert into test_files (1,2,3,4) values('" . $a . "','" . $b . "','" . $c . "','" . $d . "')";
            mysqli_query($conn, $qry);

        }

        fclose($file);
    }
}
  ask by Mico Boeje translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...