有没有什么安全的方法可以在不使用exec命令的情况下从php创建zip压缩文件或tar.gz?
谢谢
发布于 2013-11-19 11:32:25
如果你想创建tar.gz并且你正在使用PHP 5.3+,你可以使用PharData类:
try
{
$a = new PharData('archive.tar');
// ADD FILES TO archive.tar FILE
$a->addFile('data.xls');
$a->addFile('index.php');
// COMPRESS archive.tar FILE. COMPRESSED FILE WILL BE archive.tar.gz
$a->compress(Phar::GZ);
// NOTE THAT BOTH FILES WILL EXISTS. SO IF YOU WANT YOU CAN UNLINK archive.tar
unlink('archive.tar');
}
catch (Exception $e)
{
echo "Exception : " . $e;
}发布于 2011-08-10 09:53:12
您可以使用PHP类创建压缩文件,也可以使用ZLib创建Zip文件。
创建.zip文件:
$zip = new ZipArchive();
$res = $zip->open('test.zip', ZipArchive::CREATE);
$zip->addFromString('test.txt', 'file content goes here');
$zip->addFile('data.txt', 'entryname.txt');
$zip->close();创建.gz文件:
$file = "test.txt";
$gzfile = "test.gz";
$fp = gzopen($gzfile, 'w9'); // w == write, 9 == highest compression
gzwrite($fp, file_get_contents($file));
gzclose($fp);发布于 2011-08-10 09:37:42
请改用system命令
http://php.net/manual/en/function.system.php
编辑:通过ZipArchive ()命令http://www.php.net/manual/en/class.ziparchive.php,使用open类创建一个新的压缩包
https://stackoverflow.com/questions/7004989
复制相似问题