如何将默认类型设置为ZIP或RAR


How to set default type as ZIP or RAR

我在PHP中使用touch变量创建了一个文件。

如何将文件NOT文件夹的默认类型设置为ZIPRAR(PHP创建zip文件夹并将文件放入其中,但我想制作ZIPRAR文件(?

touch(filename);

但是函数不能在参数中取"TYPE"。

touch函数不是专门为创建文件而设计的,它是为更新文件的时间戳而设计的。作为时间戳更新的副作用,如果文件不是预先存在的,那么它将被创建。

如果您想使用zip归档做任何事情,最好使用PHP的ZipArchive类。

$zip = new ZipArchive;
if ($zip->open('test.zip', ZipArchive::CREATE|ZipArchive::OVERWRITE) === TRUE) {
    $zip->addFile('verylargetextfile.txt', 'whatItWillBeCalledInTheZip.txt');
    $zip->close();
    echo 'Zip archive Created!' . PHP_EOL;
} else {
    echo 'Could not create Zip Archive!' . PHP_EOL;
}

ZipArchive::addFile允许将文件放在Zip Archive的顶层,这样它就不会被放在文件夹中,即:

$ touch verylargetextfile.txt
$ php -fziptest.php
Zip archive Created!
$ unzip test.zip               # Will create whatItWillBeCalledInTheZip.txt in the working directory

请参阅此处了解更多信息。