Skip to content

Instantly share code, notes, and snippets.

@hslatman
Last active March 16, 2019 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hslatman/3933ec59df39e358fa68 to your computer and use it in GitHub Desktop.
Save hslatman/3933ec59df39e358fa68 to your computer and use it in GitHub Desktop.
Create .tar archive of all files in directory
<?php
/******************************************************************************************************************
* Source: http://www.emvee-solutions.com/blog/create-tar-files-archive-files-server-using-php-script/
*
* Please note that the script wil by default tar all files and (sub)folders from the directory you uploaded the script to
* into a file named archive.tar. You can overwrite the directory and archive name by calling the script with your own
* parameters, like this (replace the italic text for your own needs):
*
* http://domain.com/archive.php?targetname=achivename.tar&dir=relative/folder/to/put/in/archive/
******************************************************************************************************************/
try {
//make sure the script has enough time to run (300 seconds = 5 minutes)
ini_set('max_execution_time', '300');
ini_set('set_time_limit', '0');
$target = isset($_GET["targetname"]) ? $_GET["targetname"] : 'archive.tar'; //default to archive.tar
$dir = isset($_GET["dir"]) ? $_GET["dir"] : './.'; //defaults to all in current dir
//setup phar
$phar = new PharData($target);
$phar->buildFromDirectory(dirname(__FILE__) . '/'.$dir);
echo 'Compressing all files done, check your server for the file ' .$target;
} catch (Exception $e) {
// handle errors
echo 'An error has occured, details:';
echo $e->getMessage();
}
?>
@pacmandv
Copy link

how can i exclude some dirs of files? Eg got the error

An error has occured, details:Iterator RecursiveIteratorIterator returned a file that could not be opened "/var/www/public_html/.ftpquota"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment