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

Php 如何将默认类型设置为ZIP或RAR,php,zip,Php,Zip,我在PHP中使用touch变量创建了一个文件 如何将文件而不是文件夹的默认类型设置为ZIP或RAR(PHP创建ZIP文件夹并将文件放入其中,但我想创建ZIP或RAR文件) 但是该函数不能在参数中输入“TYPE”。该touch函数不是专门为创建文件而设计的,它是为更新文件的时间戳而设计的。作为时间戳更新的副作用,如果文件不是预先存在的,则将创建它 如果您想使用zip存档做任何事情,最好使用PHP的ZipArchive类 $zip = new ZipArchive; if ($zip->ope

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

如何将文件而不是文件夹的默认类型设置为
ZIP
RAR
(PHP创建ZIP文件夹并将文件放入其中,但我想创建
ZIP
RAR
文件)


但是该函数不能在参数中输入“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存档的顶层,这样它就不会被包含在文件夹中,即:

$ touch verylargetextfile.txt
$ php -fziptest.php
Zip archive Created!
$ unzip test.zip               # Will create whatItWillBeCalledInTheZip.txt in the working directory
有关更多信息,请参阅

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