使用 password_hash 加载数据本地文件

Load Data Local Infile with password_hash

我正在尝试将 CSV 上传到 table。但是,密码是纯文本,我想在此过程中将其转换为 password_hash。请帮忙

$sql = "LOAD DATA LOCAL INFILE '".$mylfile."'
   INTO TABLE parentstudent
   FIELDS TERMINATED BY ','
   OPTIONALLY ENCLOSED BY '\"' 
   LINES TERMINATED BY '\n' 
   IGNORE 1 LINES 
   (parentstudent_id, @parent_id, student_id, school_id)
   set parent_id =".password_hash."'(parent_id,".PASSWORD_DEFAULT.")";

$con=mysqli_connect("localhost","root","","confapp");

$result = mysqli_query($con, $sql);

假设有一个名为 clearpwords.csv 的文件,其内容如下:

1,test,1,1
2,testtest,2,1

以下程序将散列第二列。

<?php
// this is needed for any significant number of inputs to password_hash()
set_time_limit(0);

$infile = "clearpwords.csv";
$myfile = "hashedpwords.csv";

$reader = fopen($infile, 'r');
$writer = fopen($myfile, 'w');
$buffer = '';

while ($line = fgetcsv($reader)) {
    $line[1] = password_hash($line[1], PASSWORD_DEFAULT);

    $buffer .= implode(',', $line) . "\n";

    if (strlen($buffer) > 1024) {
        fwrite($writer, $buffer);
        $buffer = '';
    }
}

fwrite($writer, $buffer);
fclose($reader);
fclose($writer);

它将创建一个名为 myfile.csv 的文件,如下所示。

1,y$KwG1S4w7T4ov71bFSsKhlOW2CpFrMurtZRz3az94o7BX70pmohCb.,1,1
2,y$zQkH5vDIYLCqkUxxaqH6nuZ67fXj71XiBVjxztvst.dtvSlFqjDou,2,1

您现在可以 运行 您的脚本 myfile.csv

$sql = "LOAD DATA LOCAL INFILE '".$mylfile."'
        INTO TABLE parentstudent
        FIELDS TERMINATED BY ','
        OPTIONALLY ENCLOSED BY '\"' 
        LINES TERMINATED BY '\n' 
        IGNORE 1 LINES 
        (parentstudent_id, @parent_id, student_id, school_id)";

$con = mysqli_connect("localhost", "root", "", "confapp");
$result = mysqli_query($con, $sql);

请注意 password_hash is only available in PHP 5.5 and above. If you are running a version prior to this and upgrading is not an option use the crypt 函数。

不要试图使用 SHA-*MD-*