-
-
Save kirtan403/1b896be6c1957b146ed2 to your computer and use it in GitHub Desktop.
| <?php | |
| function show($str){ | |
| echo $str . "<br/>\n"; | |
| flush(); | |
| ob_flush(); | |
| } | |
| $archiveDir = "temp"; | |
| $files = array_diff(scandir($archiveDir), array('..', '.')); | |
| show ( "Total files to be extracted : " . sizeof($files) ); | |
| $counter = 1; | |
| foreach($files as $file) { | |
| $zip = new ZipArchive; | |
| $res = $zip->open($archiveDir . "/" . $file); | |
| if ($res === TRUE) { | |
| $zip->extractTo('./'); | |
| $zip->close(); | |
| show( "$counter : File $file extracted.."); | |
| } else { | |
| show( "ERROR: Trouble extracting file : $file"); | |
| } | |
| $counter++; | |
| } | |
| show ("Completed !") | |
| ?> |
| <?php | |
| /* | |
| * | |
| * This script will backup your web site by remotely archiving all files on the root FTP directory. | |
| * It will work even if your web server is memory limited buy splitting zips in several arhive files it they are too many files. | |
| * All zip files will be stored in a directory called temporary which must be writable. | |
| * | |
| * How to use it: | |
| * - Place the script at the root of your FTP. | |
| * - Call http://yoursite.com/zip.php | |
| * - In your FTP client go to temporary folder and download all backup_xxxxxx_x.zip files locally | |
| * - Unzip everything with this command: for i in *.zip; do unzip $i; done; | |
| * - Finally to avoid security issues, remove the script from the FTP. | |
| * | |
| */ | |
| // increase script timeout value | |
| ini_set('max_execution_time', 50000000); | |
| class DirFilter extends RecursiveFilterIterator | |
| { | |
| protected $exclude; | |
| public function __construct($iterator, array $exclude) | |
| { | |
| parent::__construct($iterator); | |
| $this->exclude = $exclude; | |
| } | |
| public function accept() | |
| { | |
| return !($this->isDir() && in_array($this->getFilename(), $this->exclude)); | |
| } | |
| public function getChildren() | |
| { | |
| return new DirFilter($this->getInnerIterator()->getChildren(), $this->exclude); | |
| } | |
| } | |
| function show($str){ | |
| echo $str . "<br/>\n"; | |
| flush(); | |
| ob_flush(); | |
| } | |
| $date = getdate(); | |
| $splitNum = 0; | |
| // Directory where zip files will be created | |
| $archiveDir = "temp"; | |
| if (!file_exists($archiveDir)) { | |
| mkdir($archiveDir, 0777, true); | |
| } | |
| $archive = $archiveDir . "/backup_" . $date[0]; | |
| $currentArchive = $archive . "_" . $splitNum . ".zip"; | |
| // Excludes this directories for ziping | |
| $dirsIgnore = array($archiveDir,"public_html"); | |
| $zip = new ZipArchive(); | |
| if ($zip->open($currentArchive, ZIPARCHIVE::CREATE) !== TRUE) { | |
| die ("Could not open archive"); | |
| } | |
| $numFilesTotal = 0; | |
| $iterator = new RecursiveIteratorIterator(new DirFilter(new RecursiveDirectoryIterator("./"),$dirsIgnore)); | |
| foreach ($iterator as $key=>$value){ | |
| $numFilesTotal += 1; | |
| } | |
| show( "Will backup $numFilesTotal to $archive.zip" ); | |
| $iterator = new RecursiveIteratorIterator(new DirFilter(new RecursiveDirectoryIterator("./"),$dirsIgnore)); | |
| $numFiles = 0; | |
| $counter = 0; | |
| // Use file size to split archives | |
| $maxFilePerArchive = 4000000; // Just less than 4MB | |
| foreach ($iterator as $key=>$value){ | |
| if ((filesize(realpath($key))+$counter) >= $maxFilePerArchive) { | |
| $currentArchive = $archive . "_" . $splitNum++ . ".zip"; | |
| show( "Splitting archive, new archive is $currentArchive" ); | |
| $zip->close(); | |
| ob_flush();flush(); | |
| $zip = null; | |
| $zip = new ZipArchive(); | |
| if ($zip->open($currentArchive, ZIPARCHIVE::CREATE) !== TRUE) { | |
| die ("Could not open archive"); | |
| } | |
| $counter = 0; | |
| } | |
| if (! preg_match('/temporary\/backup_' . $date[0] . '/', $key)){ | |
| $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); | |
| $numFiles += 1; | |
| if ($numFiles % 300 == 0) { | |
| show( number_format(($numFiles/$numFilesTotal)*100,0) . "% completed.."); | |
| } | |
| $counter += filesize(realpath($key)); | |
| } else { | |
| show( "Not backuping this file -> $key" ); | |
| } | |
| } | |
| // close and save archive | |
| $zip->close(); | |
| show( "Archive created successfully with $numFiles files." ); | |
| ?> |
Hello,
your zip.php script works well, thank you very much.
I would have liked to know if it was possible to correct the error obtained during the execution:
Warning: ZipArchive::close(): Read error: Is a directory in /htdocs/zip.php on line 81
In addition, files starting with a dot (ex: .htaccess) are not stored.
Would it be possible to include them in the archive?
Thank you.
Try to create a folder named "temporary" to make it work.
Can someone fix the .htaccess problem, or any file with a 'dot' infront of it please?
Also, can someone who has design experience, make an easy to use interface?
Love it!
P.S - you should make a .php script that will unzip any .zip or .rar files uploaded to it when installed on a website.... that's also what I'm looking for if anyone knows of this script please Tweet me @jaycameron (one that also puts together .r1 .r2 .r3 etc etc) is also what I need. I'm aware there are sites online that do this, but I need a script for my own purposes on my server.
Does not work..