I need to create a rar file with password protection. The following is my code.
use Zend\Filter\Compress\Rar;
class TestController {
function testAction() {
$rar = new Rar();
$parent_dir = sys_get_temp_dir()."/test".time();
if(!is_dir($parent_dir)){
mkdir($parent_dir, true);
chmod($parent_dir, 0777);
}
foreach($arr as $id) {
$dir = $parent_dir."/test_$id/";
$filename = "test_$id.xml";
if(!is_dir($dir)){
mkdir($dir, true);
chmod($dir, 0777);
}
$content = 'some content goes here';
$f2 = fopen($dir.$filename, "w");
fwrite($f2, $content);
fclose($f2);
}
$rar_dir = sys_get_temp_dir()."/";
$rar_name = "test.rar";
$rar->setArchive($rar_dir.$rar_name);
$rar->setCallback($this->rarCallback($rar_dir,$rar_name));
$rar->setPassword('123');
$rar->setTarget($parent_dir);
$rar->compress($parent_dir);
exit;
}
public function rarCallback($rar_dir, $rar_name)
{
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$rar_name");
header("Content-Type: application/download");
header("Content-Transfer-Encoding: binary");
readfile($rar_dir.$rar_name);
}
}
From this i got a rar file but that is not password protected. I don’t understand what i am missing.