命令行 zip (wamp/windows/php)
Command line zip (wamp/windows/php)
我是 php 和 S.O 社区的新手。
我在:
-Windows
-Wamp
我最近的问题是制作一个受密码保护的 zip 以供下载。
如您所知,Ziparchive 不允许制作密码保护的 zip,函数 setpassword 仅用于解密。
所以我听说我可以通过命令行来做到这一点,但老实说,我对命令行中 exec()
的理解是 php 将执行 zip.exe 和密码保护文件.
所以请问在我的情况下 "command line" 工作的步骤是什么 (windows/wamp)?
这是我的脚本:
<?php
$ZipN='zip.zip';
$Zip = new ZipArchive();
//File path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/PF/';
function zipFilesAndDownload($ZipN,$Zip,$file_path){
if ($Zip->open($ZipN, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$ZipN>\n");
}
$Zip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n");
$Zip->close();
//Password protect Not working !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
exec("zip -P password $file_path.$ZipN $ZipN");
//then send the headers to foce download the Zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$ZipN");
header("Content-length: " . filesize($ZipN));
header("Pragma: no-cache");
header("Expires: 0");
readfile("$ZipN");
// Delete the files from the server, even if the user cancels the download
ignore_user_abort(true);
unlink($file_path.$ZipN);
exit;
}
zipFilesAndDownload($ZipN,$Zip,$file_path);
?>
谢谢
我是 php 和 S.O 社区的新手。
我在: -Windows -Wamp
我最近的问题是制作一个受密码保护的 zip 以供下载。 如您所知,Ziparchive 不允许制作密码保护的 zip,函数 setpassword 仅用于解密。
所以我听说我可以通过命令行来做到这一点,但老实说,我对命令行中 exec()
的理解是 php 将执行 zip.exe 和密码保护文件.
所以请问在我的情况下 "command line" 工作的步骤是什么 (windows/wamp)?
这是我的脚本:
<?php
$ZipN='zip.zip';
$Zip = new ZipArchive();
//File path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/PF/';
function zipFilesAndDownload($ZipN,$Zip,$file_path){
if ($Zip->open($ZipN, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$ZipN>\n");
}
$Zip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n");
$Zip->close();
//Password protect Not working !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
exec("zip -P password $file_path.$ZipN $ZipN");
//then send the headers to foce download the Zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$ZipN");
header("Content-length: " . filesize($ZipN));
header("Pragma: no-cache");
header("Expires: 0");
readfile("$ZipN");
// Delete the files from the server, even if the user cancels the download
ignore_user_abort(true);
unlink($file_path.$ZipN);
exit;
}
zipFilesAndDownload($ZipN,$Zip,$file_path);
?>
谢谢