首页 > 解决方案 > 从目录中获取所有文件的名称并对其执行“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;
  }

?>

标签: php

解决方案


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方法以提供正确的覆盖标志。祝你好运!


推荐阅读