centos 6 安裝 zip 模組
用PECL的方式安裝
步驟1.
yum -y install pcre-devel gcc zlib zlib-devel
步驟2.
pecl install zip
快好了~~步驟3.
請在php.ini中加上
extension_dir = /usr/lib64/php/modules/
extension=zip.so;
最後:
service httpd restart
成功以後phpinfo()會多出這個:
使用 php zip 函數將文件打包成 zip 檔
<?php
$str = "test...test..."; //檔案內容
$filename = "testfile"; //檔案名稱
$filename_txt = $filename . ".txt";
$filename_zip = $filename . ".zip";
$dir = "/var/www/html"; //暫存檔目錄
$pwd_txt = $dir . "/" . $filename_txt;
$pwd_zip = $dir . "/" . $filename_zip;
$res_file = file_put_contents($pwd_txt, $str);
unset($str);
if(FALSE === $res_file){
die("產生檔案失敗");
}else{
$zip = new ZipArchive;
$res_zip = $zip->open($pwd_zip, ZipArchive::CREATE);
if (TRUE === $res_zip) {
$zip->addFile($pwd_txt, $filename_txt);
$zip->close();
unlink($pwd_txt);
}else{
unlink($pwd_txt);
die("產生壓縮檔失敗");
}
}
if (file_exists($pwd_zip)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($pwd_zip));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($pwd_zip));
readfile($pwd_zip);
exit;
}
另外也能將文件加入到 zip 檔中
<?php
$zip=new ZipArchive;
$res=$zip->open('test.zip',ZipArchive::CREATE);
if($res===TRUE){
$zip->addFromString('image.txt','file content goes here');
$zip->close();
echo 'ok';
}else{
echo 'failed';
}
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename=test.zip');
readfile(test.zip);
unlink(test.zip);
exit;
執行時可以利用 shell php指令來執行 php 檔,就可以在目錄底下發現到 xxx.zip 檔案
步驟1.
yum -y install pcre-devel gcc zlib zlib-devel
步驟2.
pecl install zip
快好了~~步驟3.
請在php.ini中加上
extension_dir = /usr/lib64/php/modules/
extension=zip.so;
最後:
service httpd restart
成功以後phpinfo()會多出這個:
使用 php zip 函數將文件打包成 zip 檔
<?php
$str = "test...test..."; //檔案內容
$filename = "testfile"; //檔案名稱
$filename_txt = $filename . ".txt";
$filename_zip = $filename . ".zip";
$dir = "/var/www/html"; //暫存檔目錄
$pwd_txt = $dir . "/" . $filename_txt;
$pwd_zip = $dir . "/" . $filename_zip;
$res_file = file_put_contents($pwd_txt, $str);
unset($str);
if(FALSE === $res_file){
die("產生檔案失敗");
}else{
$zip = new ZipArchive;
$res_zip = $zip->open($pwd_zip, ZipArchive::CREATE);
if (TRUE === $res_zip) {
$zip->addFile($pwd_txt, $filename_txt);
$zip->close();
unlink($pwd_txt);
}else{
unlink($pwd_txt);
die("產生壓縮檔失敗");
}
}
if (file_exists($pwd_zip)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($pwd_zip));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($pwd_zip));
readfile($pwd_zip);
exit;
}
另外也能將文件加入到 zip 檔中
<?php
$zip=new ZipArchive;
$res=$zip->open('test.zip',ZipArchive::CREATE);
if($res===TRUE){
$zip->addFromString('image.txt','file content goes here');
$zip->close();
echo 'ok';
}else{
echo 'failed';
}
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename=test.zip');
readfile(test.zip);
unlink(test.zip);
exit;
執行時可以利用 shell php指令來執行 php 檔,就可以在目錄底下發現到 xxx.zip 檔案
留言
張貼留言