Thursday 14 September 2017

Create Zip file of all php files from your server using PHP file

Create Zip file of all php files from your server using PHP file. If you have not cpanel of the server so you can use this php file and compress files as zip file.

Just you need put this file into root directory of the server (ex.public_html/createzip.php)

And run this file from browser eg. http://domain.com/createzip.php

Create one file name : createzip.php and put following code into the file. and run file from browser.

<?php
// Get real path for our folder
$realpath = getcwd();
$rootPath = realpath($realpath);
// Initialize archive object
$zip = new ZipArchive();
$filename = 'bk_'.date('YmdHis').'.zip';
$zip->open($filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath),
    RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
    // Skip directories (they would be added automatically)
    if (!$file->isDir())
    {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);
        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    }
}
// Zip archive will be created only after closing object
$zip->close();

No comments:

Post a Comment