簡體   English   中英

從目錄中獲取所有文件的名稱,並使用它們執行“for”

[英]Get name of all files from a directory and do a “for” with them

我有一個PHP代碼,讀取加密文件並解密。 什么是整體質量? 也就是說,有一個文件夾只有4/5加密文件,我想要解密所有文件,最好是贏得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的權限,並查看ZipArchiveopen方法以提供正確的覆蓋標記。 祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM