php - 从目录中获取所有文件的名称并对其执行“for”
问题描述
我有一个读取加密文件并将其解密的 php 代码。什么是整体质量?也就是说,有一个文件夹只有 4/5 个加密文件,我想全部解密,最好是 win rar。
<?php
$file= "text.txt";
$decrypted = decrypt_file($file,'pass');
header('Content-type:application/txt');
fpassthru($decrypted);
function decrypt_file($file,$passphrase){
$iv = substr(md5("\x18\x3C\x58".$passphrase,true),0,8);
$key = substr(md5("\x2D\xFC\xD8".$passphrase,true).md5("\x2D\xFC\xD8".$passphrase,true),0,24);
$opts = array('iv'=>$iv, 'key'=>$key);
$fp = fopen($file,'rb');
stream_filter_append($fp, 'mdecrypt.tripledes', STREAM_FILTER_READ, $opts);
return $fp;
}
?>
解决方案
该glob()
功能就是这样做的。并制作一个您也可以使用的 zip 文件ZipArchive
。这是一个例子:
<?php
// create a zip file
$zip = new ZipArchive();
$res = $zip->open('test.zip', ZipArchive::CREATE);
$directory = 'path/to/folder';
foreach (glob("$directory/*.txt") as $filename) {
// get the decrypted content
$decrypted = decrypt_file($filename,'pass');
// add it to the zip
$zip->addFromString($filename, $decrypted);
}
$zip->close();
header("Location: test.zip");
因此,我们遍历每个文件,对其进行解密并将该文本添加到 zip 中,作为具有相同文件名的自己的文件。完成后,我们将test.zip
在此示例中留下,因此我们将用户重定向到该文件以下载它。您可以改为为 zip 输出正确的内容类型和相关标头。另请注意,您可能需要整理保存 test.zip 的权限,并查看ZipArchive
'open
方法以提供正确的覆盖标志。祝你好运!
推荐阅读
- r - 尝试创建一个新列,观察响应 2 个单独列中的标准并确定输出
- r - 使用循环从 R 中数据框中的另一列创建多列
- php - 如何在 php 中解码 utf-8
- r - 在 r-highcharter 中更改图表元素的颜色
- c# - EF Add-Migration:使用 app.config 的连接字符串问题
- python - 如何将类的实例分配为类主体中同一类的属性?
- wordpress - 如何在提交按钮下方放置“接受”?Elementor Pro Form + 内联输入问题
- javascript - Promises JS:函数响应不返回到主代码
- c++ - 关于`std::unique_prt
& pBase= make_unique ();` - swift - SwiftUI - 将表单的结果保存到 JSON