Is there is any safe way to create a zip archive, or tar.gz from php without using exec command ?
thanks
Is there is any safe way to create a zip archive, or tar.gz from php without using exec command ?
thanks
If you want to create tar.gz and you are using PHP 5.3+, you can use PharData class:
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; }
You can use PHP's Zip class to create zip files, and ZLib to create gzip files.
Creating a .zip
file:
$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();
Creating a .gz
file:
$file = "test.txt"; $gzfile = "test.gz"; $fp = gzopen($gzfile, 'w9'); // w == write, 9 == highest compression gzwrite($fp, file_get_contents($file)); gzclose($fp);
Use the system command instead
http://php.net/manual/en/function.system.php
Pay attention to the notes section on how to protect from malicious input.
EDIT: Use the ZipArchive class to create a new zip via the open() command http://www.php.net/manual/en/class.ziparchive.php
There is a very simple way to create Tar-archives directly within PHP - you can find a CLASS at sourceforge from Dennis Wronka under LGPL so you can use also in commercial scripts.
*/ /** * @package PHPClassCollection * @subpackage Tar * @link classes * @author Dennis Wronka * @version 1.1 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html LGPL 2.1 */ class tar { /** * The name of the tar-file to create. * * @var string */ var $filename; /** * The list of files to add to the archive. * * @var array */ var $filelist=array(); /** * Constructor * * @param string $filename */ function tar($filename) { $this->filename=$filename; } /** * Add a file. * * @param string $filename */ function add($filename) { if ((file_exists($filename)) && (is_readable($filename))) { $this->filelist[]=$filename; } } /** * Write the tar-file. * * @return bool */ function write() { sort($this->filelist); $tarfile=@fopen($this->filename,'w'); if ($tarfile==false) { return false; } for ($x=0;$xfilelist);$x++) { $filename=$this->filelist[$x]; if ((is_dir($this->filelist[$x])) && (substr($this->filelist[$x],-1)!='/')) { $filename.='/'; } while (strlen($filename)filelist[$x])).chr(0); while (strlen($permissions)filelist[$x])).chr(0); while (strlen($userid)filelist[$x])).chr(0); while (strlen($groupid)filelist[$x])) { $filesize='0'.chr(0); } else { $filesize=sprintf('%o',filesize($this->filelist[$x])).chr(0); } while (strlen($filesize)filelist[$x])).chr(0); $checksum=' '; if (is_dir($this->filelist[$x])) { $indicator=5; } else { $indicator=0; } $linkname=''; while (strlen($linkname)filelist[$x],'r'); $data=fread($contentfile,filesize($this->filelist[$x])); while (strlen($data)%512!=0) { $data.=chr(0); } fwrite($tarfile,$data); } } fclose($tarfile); return true; } } ?>