【发布时间】:2018-11-10 09:03:56
【问题描述】:
我有一个 PHP 页面,它从我的 SQL 表中选择记录并将记录的加密版本发送到 CSV 文件。我的问题是:当我选择记录而不使用 openssl 加密时,它会成功导出,但是,如果我包含加密它会打开一个空白屏幕。
我希望能在这里得到帮助。非常感谢提前
This is my code:
<?php
// Database Connection
require("xxx.php");
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'))
$encryption_key = 'bRuD5WYw5wd0rdHR9yLlM6wt2vteuiniQBqE70nAuhU=';
$result='select *
from encrypt';
$file='grade/file'.date('YmdDH_i_s').'.csv';
$qry = "select * from gb_centre where use_flag=1";
$rs = mysqli_query($con,$qry);
$getRowAssoc = mysqli_fetch_assoc($rs);
$query = (
openssl_encrypt($result->uniqueid,'AES-256-CBC',$encryption_key,0,$iv),
openssl_encrypt($result->username,'AES-256-CBC',$encryption_key,0,$iv),
openssl_encrypt($result->shortname,'AES-256-CBC',$encryption_key,0,$iv),
openssl_encrypt($result->rawgrade,'AES-256-CBC',$encryption_key,0,$iv),
openssl_encrypt($result->finalgrade,'AES-256-CBC',$encryption_key,0,$iv),
openssl_encrypt($result->cid,'AES-256-CBC',$encryption_key,0,$iv),
openssl_encrypt($result->id,'AES-256-CBC',$encryption_key,0,$iv)
);
if (!$result = mysqli_query($con, $query)) {
exit(mysqli_error($con));
}
$users = array($uniqueid,$username,$shortname,$rawgrade,$finalgrade,$cid,$id);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$users[] = $row;
}
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='.$getRowAssoc['cid'].date('_Y-m-d_Hi').'.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('a1', 'a2', 'a3', 'a4', 'a5','a6','a7'));
if (count($users) > 0) {
foreach ($users as $row) {
fputcsv($output, $row);
}
}
?>
**
编辑:
** 我已经编辑了代码部分,它现在可以使用 PHP OpenSSL 将记录导出到 csv 并且工作正常,问题是,它在 CSV 文件中添加了额外的列。我无法弄清楚错误在哪里。 这是编辑后的代码:
<?php
// Database Connection
require("config.php");
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$key = 'bRuD5WYw5wd0rdHR9yLlM6wt2vteuiniQBqE70nAuhU=';
$encryption_key = 'bRuD5WYw5wd0rdHR9yLlM6wt2vteuiniQBqE70nAuhU=';
$string='select *
from encrypt'
$qry = "select * from gb_centre where use_flag=1";
$rs = mysqli_query($con,$qry);
$getRowAssoc = mysqli_fetch_assoc($rs);
if (!$result = mysqli_query($con, $string)) {
exit(mysqli_error($con));
}
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$uniqueid[] = openssl_encrypt($row->uniqueid,'AES-256-CBC',$encryption_key,0,$iv);
$username[] = openssl_encrypt($row->username,'AES-256-CBC',$encryption_key,0,$iv);
$shortname[] = openssl_encrypt($row->shortname,'AES-256-CBC',$encryption_key,0,$iv);
$rawgrade[] = openssl_encrypt($row->rawgrade,'AES-256-CBC',$encryption_key,0,$iv);
$finalgrade[] = openssl_encrypt($row->finalgrade,'AES-256-CBC',$encryption_key,0,$iv);
$cid[] = openssl_encrypt($row->cid,'AES-256-CBC',$encryption_key,0,$iv);
$id[] = openssl_encrypt($row->id,'AES-256-CBC',$encryption_key,0,$iv);
}
}
$users = array($uniqueid,$username,$shortname,$rawgrade,$finalgrade,$cid,$i);
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='.$getRowAssoc['cid'].date('_Y-m-d_Hi').'.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('a1', 'a2', 'a3', 'a4', 'a5','a6','a7'));
if (count($users) > 0) {
foreach ($users as $row1) {
fputcsv($output, $row1);
}
}
?>
【问题讨论】:
-
$query似乎只是一个值列表,而不是实际查询。您还缺少;的$iv。我会得到一个有 linter 的代码编辑器。我推荐“原子”。它是免费的,而且做得很好。 -
感谢您的回复,我正在尝试在 $query 中引用上面的 $result 查询。请问,我做错了什么?非常感谢
标签: php mysql php-openssl