如何在php的zip文件中保护Excel密码

时间:2019-05-22 08:09:53

标签: php codeigniter phpexcel

我想在zip文件夹中下载excel。之后,应使用密码保护。但这不起作用。

public function download (){

    $header = array('id');

    require_once APPPATH."/third_party/PHPExcel.php";
    $sheet = new PHPExcel();
    $file = $this->appmodel->Data();
    // echo "<pre>"; print_r($file); die;
    $filename = $file->id;
    $this->load->helper('date');
    $date = date('Y-m-d'); 
    //1st Sheet
    $sheet->setActiveSheetIndex(0);
    $activeSheet = $sheet->getActiveSheet();
    $activeSheet->fromArray($header, null);

    $objWriter = PHPExcel_IOFactory::createWriter($sheet, 'Excel2007');  
    // echo "<pre>"; print_r($objWriter); die;
    $excel_file_tmp = tempnam("/tmp", 'your_prefix');
    $objWriter->save($excel_file_tmp);

    //zip
    $zip_file_tmp = tempnam("/tmp", 'your_prefix');
    $zip        = new ZipArchive();
    $zip->open($zip_file_tmp, ZipArchive::OVERWRITE);
    $zip->addFile($excel_file_tmp, 'your_name.xlsx');
    $zip->close();

    //download
    $password = "22";
    $download_filename = 'your_name.zip'; 
    header("Content-Type: application/octet-stream");
    header("Content-Length: " . filesize($zip_file_tmp));
    header("Content-Disposition: attachment; filename=\"" . $download_filename . "\"");
    @system("zip -P $password $excel_file_tmp $zip_file_tmp ");
    readfile($zip_file_tmp);
    // unlink($excel_file_tmp);
    // unlink($zip_file_tmp);
    @unlink($zip_file_tmp);

}

1 个答案:

答案 0 :(得分:0)

由于 PHP> 7.2 ,您可以使用 setEncryptionName 来使用密码来处理ZIP存档。

@IBOutlet weak var approvedButton: UIButton!

@IBOutlet weak var rejectedButton: UIButton!

@IBOutlet weak var searchBar: UISearchBar!

@IBOutlet weak var approvedDownLabel: UILabel!



override func viewDidLoad() {
    super.viewDidLoad()
    approvedDownLabel.isHidden = true
    rejectedDownLbl.isHidden = true
    addNewTblview.dataSource = self
    addNewTblview.delegate = self
    newButton.isSelected = true
    self.addNewTblview.register(UINib(nibName: "AddInstructorNewCell", bundle: nil), forCellReuseIdentifier: "AddInstructorNewCell")
    self.addNewTblview.register(UINib(nibName: "ApprovedCell", bundle: nil), forCellReuseIdentifier: "ApprovedCell")
    self.addNewTblview.register(UINib(nibName: "RejectedCell", bundle: nil), forCellReuseIdentifier: "RejectedCell")
       }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return 4
       }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if newButton.isSelected {
        let cell = self.addNewTblview.dequeueReusableCell(withIdentifier: "AddInstructorNewCell", for: indexPath) as! AddInstructorNewCell
        return cell

    } else if approvedButton.isSelected {
        let cell = self.addNewTblview.dequeueReusableCell(withIdentifier: "ApprovedCell", for: indexPath) as! ApprovedCell
        return cell
    } else {
         let cell = self.addNewTblview.dequeueReusableCell(withIdentifier: "RejectedCell", for: indexPath) as! RejectedCell
        return cell
            }
  //    return UITableViewCell()
          }
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    if newButton.isSelected {
        return 170
    } else if approvedButton.isSelected{
        return 165
    }else {
        return 178
    }
   // return 170
        }
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

}

Protect ZIP Archive with password上查看更多信息