моя PHP-программа должна записывать некоторые данные в текстовый файл и шифровать файл с помощью gnupg. Когда пользователь нажимает «Загрузить», программа должна загрузить зашифрованный файл с расширением .GPG.
Я могу успешно зашифровать, а также расшифровать файл. Но я вижу html-код и ошибку в моем зашифрованном файле. Но он исчез сразу после того, как я расшифровал файл. Но все же это считается ошибкой, не так ли? Может кто-нибудь помочь?
Обратите внимание на этот скриншот
Часть моего HTML-кода:
<div id = "layoutSidenav_content">
<main>
<div class = "container-fluid">
<h1 class = "mt-4">Muat Turun Rekod</h1>
<div class = "card mb-4">
<div class = "card-header">
<i class = "fa fa-table" aria-hidden = "true" style = "font-size:18px"></i>
Rekod untuk Dimuat Turun
</div>
<div class = "card-body-tbm">
<form method = "post" action = "tryExport.php">
<div class = " table-responsive">
<table id = "example" class = "table table-bordered" style = "width:100%">
<thead>
<tr>
<th>Bil.</th>
<th>ID </th>
<th>Nama</th>
<th>Jabatan</th>
<th>PTJ</th>
<th>Peringkat</th>
<th>Peranan</th>
<th>Modul</th>
<th>Capaian</th>
<th>Kelulusan Pegawai Pengawal</th>
<th>Catatan Pegawai Pengawal</th>
<th>Kelulusan BN Pengesah</th>
<th>Catatan BN Pengesah</th>
<th>Muat Turun <input type = "checkbox"
id = "cbgroup1_master" onchange = "togglecheckboxes(this,'chkbox[]')">
</th>
</tr>
</thead>
<tbody>
<?php
$j=0;
$j=0;
$result= query("SELECT NAME, NRIC, sc_jabatan.PERIHALJABATAN AS JABATAN, sc_ptj.PERIHALPTJ AS PTJ, sc_module.ABBRV AS MODULE, SC_PROGRAM,
ID_CAPAIAN_D, gd1.PERIHALCODE AS PERINGKAT, gd2.PERIHALCODE AS PERANAN, REMARK_APPROVE, REMARK_VERIFY
FROM user_main
JOIN user_capaian ON user_main.NRIC=user_capaian.USER_MAIN_NRIC
JOIN user_capaian_d ON user_main.NRIC=user_capaian_d.USER_MAIN_NRIC AND user_capaian.ID_CAPAIAN=user_capaian_d.ID_CAPAIAN
JOIN gc_d gd1 ON gd1.ID=user_capaian.GC_LEVEL
JOIN gc_d gd2 ON gd2.ID=user_capaian.GC_ROLE
JOIN sc_module ON sc_module.ID=user_capaian_d.SC_MODULE
JOIN user_history ON user_main.NRIC=user_history.USER_MAIN_NRIC AND
user_capaian.USER_HISTORY_ID=user_history.ID AND user_capaian_d.USER_HISTORY_ID=user_history.ID
JOIN sc_jabatan ON sc_jabatan.ID=user_capaian.SC_JABATAN
JOIN sc_ptj ON sc_ptj.ID=user_capaian.SC_PTJ
WHERE user_capaian_d.F_FILESTATUS='N' AND
user_history.F_APPROVE=1 AND
user_history.F_VERIFY=1 AND
GC_NEGERI = '{$_SESSION['STATE']}'
ORDER BY user_capaian.ID_CAPAIAN");
$count = mysqli_num_rows($result);
while($row = fetch_array($result))
{
echo "<tr><td>";
echo $j+1;
echo "</td>";
echo "<td>";
echo $row["NRIC"];
echo "</td>";
echo "<td>";
echo $row["NAME"];
echo "</td>";
echo "<td>";
echo $row["JABATAN"];
echo "</td>";
echo "<td>";
echo $row["PTJ"];
echo "</td>";
echo "<td>";
echo $row["PERINGKAT"];
echo "</td>";
echo "<td>";
echo $row["PERANAN"];
echo "</td>";
echo "<td>";
echo $row["MODULE"];
echo "</td>";
echo "<td>";
echo $row["SC_PROGRAM"];
echo "</td>";
echo "<td>";
echo '<input type = "checkbox" name = "chkboxapp[]" disabled = "disabled" checked>';
echo "</td>";
echo "<td>";
echo $row["REMARK_APPROVE"];
echo "</td>";
echo "<td>";
echo '<input type = "checkbox" name = "chkboverr[]" disabled = "disabled" checked>';
echo "</td>";
echo "<td>";
echo $row["REMARK_VERIFY"];
echo "</td>";
echo "<td>";
echo '<input type = "checkbox" name = "chkbox[]" class = "cbgroup1" value = "'.$row['ID_CAPAIAN_D'].'">';
echo "</td>";
$j++;
}
?>
</tbody>
<tfoot>
<tr>
<th>Bil.</th>
<th>ID </th>
<th>Nama</th>
<th>Jabatan</th>
<th>PTJ</th>
<th>Peringkat</th>
<th>Peranan</th>
<th>Modul</th>
<th>Capaian</th>
<th>Kelulusan Pegawai Pengawal</th>
<th>Catatan Pegawai Pengawal</th>
<th>Kelulusan BN Pengesah</th>
<th>Catatan BN Pengesah</th>
<th>Muat Turun <input class = "form-check-input-insideHeader" type = "checkbox"
id = "cbgroup1_master" onchange = "togglecheckboxes(this,'chkbox[]')">
</th>
</tr>
</tfoot>
</table>
<input type = "submit" name = "export" value = "Muat Turun" class = "btn btn-info float-right" />
</div>
</form>
</div>
</div>
<div class = "card mb-4">
<div class = "card-header">
<i class = "fa fa-table" aria-hidden = "true" style = "font-size:18px"></i>
Rekod Telah Dimuat Turun
</div>
<div class = "card-body-tbm">
<div class = " table-responsive">
<table id = "example1" class = "table table-bordered" style = "width:100%">
<thead>
<tr>
<th>Bil.</th>
<th>ID </th>
<th>Nama</th>
<th>Jabatan</th>
<th>PTJ</th>
<th>Peringkat</th>
<th>Peranan</th>
<th>Modul</th>
<th>Capaian</th>
<th>Kelulusan Pegawai Pengawal</th>
<th>Catatan Pegawai Pengawal</th>
<th>Kelulusan BN Pengesah</th>
<th>Catatan BN Pengesah</th>
</tr>
</thead>
<tbody>
<?php displayExportedRecord() ?>
</tbody>
<tfoot>
<tr>
<th>Bil.</th>
<th>ID </th>
<th>Nama</th>
<th>Jabatan</th>
<th>PTJ</th>
<th>Peringkat</th>
<th>Peranan</th>
<th>Modul</th>
<th>Capaian</th>
<th>Kelulusan Pegawai Pengawal</th>
<th>Catatan Pegawai Pengawal</th>
<th>Kelulusan BN Pengesah</th>
<th>Catatan BN Pengesah</th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</main>
</div>
Моя программа экспорта и шифрования:
if (isset($_POST['export'])){
if (!empty($_POST['chkbox'])){
$data=implode(',', $_POST['chkbox']);
$result=query("SELECT DISTINCT NRIC, NAME, g1.ISPEKSCODE AS GENDER , g2.ISPEKSCODE AS RACE, g3.ISPEKSCODE AS RELIGION, g4.ISPEKSCODE AS TITLE, g5.ISPEKSCODE AS REASON,
SC_POSITION, ADDRESS1, ADDRESS2, ADDRESS3, CITY, POSTCODE, GC_NEGERI, GC_DAERAH, PHONEHOME, PHONEMOBILE, PHONEOFFICE, EMAIL, F_PTJADMIN, user_main.STATUS, STATUSREASON, ID_CAPAIAN_D
FROM user_main
JOIN user_capaian ON user_main.NRIC=user_capaian.USER_MAIN_NRIC
JOIN user_capaian_d ON user_main.NRIC=user_capaian_d.USER_MAIN_NRIC AND user_capaian.ID_CAPAIAN=user_capaian_d.ID_CAPAIAN
JOIN gc_d g1 ON g1.ID=user_main.GC_GENDER
JOIN gc_d g2 ON g2.ID=user_main.GC_RACE
JOIN gc_d g3 ON g3.ID=user_main.GC_RELIGION
JOIN gc_d g4 ON g4.ID=user_main.GC_PERSONTITLE
JOIN gc_d g5 ON g5.ID=user_main.STATUSREASON
WHERE ID_CAPAIAN_D IN ($data)
ORDER BY ID_CAPAIAN_D");
//File naming
date_default_timezone_set('Asia/Kuala_Lumpur');
$date=date("Ymd");
$timestamp=date("His");
$agencycode='60002';
$statecode=$_SESSION['STATECODE']; //Later read from SESSION
$trancode='SPI';
$filename= './uploads/'. $statecode. '_'. $agencycode. '_'. $trancode. '_'. $date. $timestamp;
if (ob_get_contents()) ob_end_clean();
$handle = fopen($filename, "w");
while($row = fetch_array($result))
{
//Header
fwrite($handle, str_pad (0, 1, " " ));
fwrite($handle, "|");
/*Later read from DB flag*/
fwrite($handle, str_pad (1, 1, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['NRIC'], 12, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['NAME'], 80, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['GENDER'], 1, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['RACE'], 2, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['RELIGION'], 10, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['TITLE'], 2, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['SC_POSITION'], 20, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['ADDRESS1'], 50, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['ADDRESS2'], 50, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['ADDRESS3'], 50, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['CITY'], 30, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['POSTCODE'], 5, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['GC_NEGERI'], 3, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['GC_DAERAH'], 3, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['PHONEHOME'], 12, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['PHONEMOBILE'], 12, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['PHONEOFFICE'], 12, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['EMAIL'], 50, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['F_PTJADMIN'], 1, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['STATUS'], 1, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['REASON'], 2, " " ));
fwrite($handle, "|".PHP_EOL);
}
$result1=query("SELECT NRIC, user_capaian.ID_CAPAIAN, g7.PERIHALCODE AS ROLE, g6.ISPEKSCODE AS LEVEL, sc_jabatan.JABATANCODE AS JABATAN, sc_ptj.PTJCODE AS PTJ, MAXAPPROVE, user_capaian_d.STATUS AS STATUS_CAPAIAN, user_history.TRAN_TYPE AS TRAN_TYPE
FROM user_main
JOIN user_capaian ON user_main.NRIC=user_capaian.USER_MAIN_NRIC
JOIN user_capaian_d ON user_main.NRIC=user_capaian_d.USER_MAIN_NRIC AND user_capaian.ID_CAPAIAN=user_capaian_d.ID_CAPAIAN
JOIN gc_d g6 ON g6.ID=user_capaian.GC_LEVEL
JOIN gc_d g7 ON g7.ID=user_capaian.GC_ROLE
JOIN sc_jabatan ON sc_jabatan.ID=user_capaian.SC_JABATAN
JOIN sc_ptj ON sc_ptj.ID=user_capaian.SC_PTJ
JOIN user_history ON user_capaian_d.USER_HISTORY_ID=user_history.ID
JOIN sc_maxapprove ON user_capaian.SC_MAXAPPROVE=sc_maxapprove.ID
WHERE ID_CAPAIAN_D IN ($data)
ORDER BY user_capaian.ID_CAPAIAN");
$j=0;
while($row = fetch_array($result1))
{
//Details Section 1
fwrite($handle, str_pad (1, 1, " " ));
fwrite($handle, "|");
if ($row['TRAN_TYPE'] == 'N' && $row['STATUS_CAPAIAN'] == '1'){
fwrite($handle, str_pad (1, 1, " " ));
fwrite($handle, "|");
}
else if ($row['TRAN_TYPE'] == 'K' && $row['STATUS_CAPAIAN'] == '1') {
fwrite($handle, str_pad (1, 1, " " ));
fwrite($handle, "|");
}
else if ($row['TRAN_TYPE'] == 'K' && $row['STATUS_CAPAIAN'] == '0') {
fwrite($handle, str_pad (2, 1, " " ));
fwrite($handle, "|");
}
fwrite($handle, str_pad ($row['NRIC'], 12, " " ));
fwrite($handle, "|");
$i=$j+1;
fwrite($handle, str_pad ($j+1, 2, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad (strtoupper($row['ROLE']), 15, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['LEVEL'], 1, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['JABATAN'], 6, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['PTJ'], 8, " " ));
fwrite($handle, "|");
/*Betulkan data type at DB so takde decimal*/
fwrite($handle, str_pad ($row['MAXAPPROVE']*100, 15, "0", STR_PAD_LEFT));
fwrite($handle, "|".PHP_EOL);
$j++;
}
$result2=query("SELECT *
FROM user_main JOIN user_capaian ON user_main.NRIC=user_capaian.USER_MAIN_NRIC JOIN user_capaian_d
ON user_main.NRIC=user_capaian_d.USER_MAIN_NRIC AND user_capaian.ID_CAPAIAN=user_capaian_d.ID_CAPAIAN
WHERE ID_CAPAIAN_D IN ($data)
ORDER BY user_capaian.ID_CAPAIAN");
$i=0;
while($row = fetch_array($result2))
{
//Details Section 2
fwrite($handle, str_pad (2, 1, " " ));
fwrite($handle, "|");
/*Later read from DB flag*/
fwrite($handle, str_pad (1, 1, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['NRIC'], 12, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($i+1, 2, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['SC_MODULE'], 2, " " ));
fwrite($handle, "|");
fwrite($handle, str_pad ($row['SC_PROGRAM'], 9, " " ));
fwrite($handle, "|".PHP_EOL);
$i++;
}
$update=query ("UPDATE user_capaian, user_capaian_d, user_history
SET user_capaian.F_FILESTATUS = 'D', user_capaian_d.F_FILESTATUS = 'D', user_history.F_FILESTATUS = 'D', user_history.FILE_DATE=CURDATE()
WHERE user_capaian.ID_CAPAIAN=user_capaian_d.ID_CAPAIAN
AND user_capaian_d.USER_HISTORY_ID=user_history.ID
AND user_capaian_d.ID_CAPAIAN_D IN ($data)");
fclose($handle);
if ($_SESSION['STATE'] == "MLK"){
$fingerprint = "62C1347DC784934E5AE45EC3530C818C80CB1CC9";
}
elseif ($_SESSION['STATE'] == "SGR"){
$fingerprint = "F7DC65FC269D38B97B06D4F3AB5E8847530D8DD9";
}
elseif ($_SESSION['STATET'] == "TRG"){
$fingerprint = "CB416DC6B06BF74D1C8DA888A52533206F67E0CD";
}
elseif ($_SESSION['STATE'] == "PNG") {
$fingerprint = "EC8CE0BE7B23EA3D2969858A1D24DD9716B64005";
}
elseif ($_SESSION['STATE'] == "PLS"){
$fingerprint = "75CA4D4E525241D14B714D9DE706513A46C8C7D7";
}
elseif ($_SESSION['STATE'] == "PRK"){
$fingerprint = "C7C098D390E79AA626DC8E8D012D3E9F85D3C63A";
}
elseif ($_SESSION['STATE'] == "PHG"){
$fingerprint = "46F266E23F41EE94DA1377554A6D7FEF04B4DCE6";
}
elseif ($_SESSION['STATE'] == "NSN"){
$fingerprint = "30025FDBC0E79E8C2E1148D0125D82F17DE6C2E8";
}
elseif ($_SESSION['STATE'] == "KTN"){
$fingerprint = "5776099FFF84A5473FD8B5FAB00E884573A9E8A9";
}
elseif ($_SESSION['STATE'] == "KDH"){
$fingerprint = "747CB3F813740163AED4F44B7B16B2FABD9ECDC2";
}
elseif ($_SESSION['STATE'] == "JHR"){
$fingerprint = "C87E98DAC9FD48E6A372C1399C0E3DA66F220C2D";
}
putenv('GNUPGHOME=/home/vagrant/.gnupg');
$gpg = new gnupg();
$gpg -> addencryptkey($fingerprint);
$plain=file_get_contents($filename);
$enc=$gpg->encrypt($plain);
file_put_contents( $filename.'.gpg', $enc);
echo $enc;
$file='./uploads/' . $filename . '.gpg';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
else {
alertBNAdmin("Sila masukkan pilihan fail yang ingin dimuat turun!");
}
}
И ошибка, которую я получаю:
Warning: filesize(): stat failed for ./uploads/./uploads/10_60002_SPI_20201215112335.gpg in /var/www/html/isams/tryExport.php on line <i>277</i></th></tr>
Warning: readfile(./uploads/./uploads/10_60002_SPI_20201215112335.gpg): failed to open stream: No such file or directory in /var/www/html/isams/tryExport.php on line <i>278</i></th></tr>
@rrd я отредактировал свой пост. Это из-за неправильного пути к папке? В любом случае заранее спасибо!
Почти уверен, что ./uploads/./uploads/
это неправильно, и должно быть только одно ./uploads/
… верно?
@CBroe, я только что понял это. Большое спасибо!
Проблема решена. Только что понял, что путь к папке из ошибки неверный. Я удаляю путь к папке в коде загрузки из:
$file='./uploads/' . $filename . '.gpg';
К:
$file= $filename . '.gpg';
Всем спасибо! :)
Вставьте соответствующий код и точное сообщение об ошибке здесь.