如何解压文件夹并删除压缩的原件?

问题描述:

我正在为CMS创建一些简单的安装程序\ setupper。当用户用我的CMS下载.zip并将其解压到他的php服务器上的某个文件夹中时,他看到一个文件 - install.php和一个名为 - Contents.zip的zip文件夹。我需要一些php函数从该Contents.zip zip文件中提取文件并删除该文件。 (如果可能的话,我想给文件夹提供不同的权利,从那里解压缩文件夹后立即)如何解压文件夹并删除压缩的原件?

如何做这样的事情?

您可以使用PHP ZipArchive library来达到您的目的。

也,code example from the documentation你可能会觉得有用。

<?php 

$zip = new ZipArchive(); 
$filename = "./test112.zip"; 

if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { 
    exit("cannot open <$filename>\n"); 
} 

$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n"); 
$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n"); 
$zip->addFile($thisdir . "/too.php","/testfromfile.php"); 
echo "numfiles: " . $zip->numFiles . "\n"; 
echo "status:" . $zip->status . "\n"; 
$zip->close(); 
?> 

改变文件的权限,你可以使用PHP builtin function chmod.

例如

chmod("/somedir/somefile", 0600); 

删除您可以使用文件,php builtin unlink

例如,

unlink('test.html'); 
unlink('testdir'); 

我强烈建议yuo通过官方的PHP文档。

+0

是官方php 5.2中的“PHP ZipArchive库”的一部分。 – Rella 2010-05-10 15:34:25

+0

和你在哪里删除该zip文件? – Rella 2010-05-10 15:40:03

+0

@ Ole yeah,ZipArchive附带官方发行版;然而它对外部依赖于http://www.zlib.net/ – phoenix24 2010-05-10 16:27:42