-2

How to download complete directory which encoded in base64_encode() with sub-files, sub-folders & all files and folders of sub-folders using php but names of files and folders should decoded in base64_decode()?

Anyone here who can give me answer of this impossible question? Thanks.

(Sorry for bad english)

Burhan
  • 21
  • 4

1 Answers1

1

Sample directory:

$ tree directory1/
directory1/
├── directory2
│   └── file3.txt
├── file1.txt
└── file2.txt

Use RecursiveDirectoryIterator and RecursiveIteratorIterator to iterate through the directory and its subdirectories to list all files. Choose if you want paths or just filenames. After that base64 encoding is just calling this function for each of them.

$rdi = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('directory1', FilesystemIterator::SKIP_DOTS));

$entries = [];
foreach ($rdi as $e) {
  $entries[] = $e->getPathName(); // $e->getFileName()
}

var_dump($entries);

$encodedEntries = [];
foreach($entries as $e) {
  $encodedEntries[] = base64_encode($e);
}

var_dump($encodedEntries);

Output:

array(3) {
  [0]=>
  string(20) "directory1/file2.txt"
  [1]=>
  string(31) "directory1/directory2/file3.txt"
  [2]=>
  string(20) "directory1/file1.txt"
}
array(3) {
  [0]=>
  string(28) "ZGlyZWN0b3J5MS9maWxlMi50eHQ="
  [1]=>
  string(44) "ZGlyZWN0b3J5MS9kaXJlY3RvcnkyL2ZpbGUzLnR4dA=="
  [2]=>
  string(28) "ZGlyZWN0b3J5MS9maWxlMS50eHQ="
}

If you also need directories on the list remove FilesystemIterator::SKIP_DOTS flag and parse out directory names from additional entries.

blahy
  • 1,294
  • 1
  • 8
  • 9
  • how to download...? – Burhan Aug 17 '20 at 09:46
  • For example as ZIP like this https://stackoverflow.com/a/29873298/5537425 ? Add more details to your question if this is not what are you looking for because it is not clear. – blahy Aug 17 '20 at 18:25