1

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

怎么做这样的事情?

4

1 回答 1

2

你可以使用PHP ZipArchive 库来达到你的目的。

此外,您可能会发现文档中的代码示例很有用。

<?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 内置函数 chmod。

例如

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

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

例如,

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

我强烈建议您阅读官方 PHP 文档。

于 2010-05-10T15:16:51.487 回答