怎么用php向服务器已存在的zip文件中增加一个文件

怎么用php向服务器已存在的zip文件中增加一个文件,第1张

加一个zip模块进去

使用哪锋迅方式如下:

<?php

$zip = new ZipArchive()

$filename = "基银./test112.zip"

if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {

exit("cannot open <$filename>")

}

$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt. ")

$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt. ")

$zip->addFile($thisdir . "/too.php","/testfromfile.php")

echo "numfiles: " . $zip->numFiles . " "

echo "status:" . $zip->status . "李此 "

$zip->close()

?>

php将文件夹打包成zip文件,参考代码如下:

function addFileToZip($path,$zip){

    $handler=opendir($path) //打开当前文件夹由$path指定。

    while(($filename=readdir($handler))!==false){

        if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..’,不要对卜穗宏他们进行 *** 作

          族迟  if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归

                addFileToZip($path."/".$filename, $zip)

            }else{ //将文件加入zip对象

                $zip->addFile($path."/".$filename)

            }

  型册      }

    }

    @closedir($path)

}

$zip=new ZipArchive()

if($zip->open('images.zip', ZipArchive::OVERWRITE)=== TRUE){

    addFileToZip('images/', $zip) //调用方法,对要打包的根目录进行 *** 作,并将ZipArchive的对象传递给方法

    $zip->close() //关闭处理的zip文件

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/bake/11980266.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存