2010-05-10 191 views
1

我正在爲CMS創建一些簡單的安裝程序\ setupper。當用戶用我的CMS下載.zip並將其解壓到他的php服務器上的某個文件夾中時,他看到一個文件 - install.php和一個名爲 - Contents.zip的zip文件夾。我需要一些php函數從該Contents.zip zip文件中提取文件並刪除該文件。 (如果可能的話,我想給文件夾提供不同的權利,從那裏解壓縮文件夾後立即)如何解壓文件夾並刪除壓縮的原件?

如何做這樣的事情?

回答

2

您可以使用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