How To Create All Files And Folders Backup Using PHP

Hello Dear Friends using bellow snippet you can create your all files and folders backup.

# My Backup
$backupRootPath = realpath('../backup-folder-name/');

# Give full path to create backup for using mail or full URL.
$BackupFullPath = "http://".$_SERVER['SERVER_NAME']."/auto-backup/";

# Give short path to create backup
$BackupPath = '../auto-backup/';

# If you can skip folder or file type add here
$NotReqFile = array('zip','rar');


function BackupZip($rootPath,$backupPath,$NotReqFile,$backupFullPath){
ini_set('max_execution_time', 600); //600 seconds = 10 minutes <<open($backupPath, ZipArchive::CREATE | ZipArchive::OVERWRITE);

# Create recursive directory iterator
# @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),

foreach ($files as $name => $file)
# Skip directories (they would be added automatically)
if (!$file->isDir())

$info = new SplFileInfo($file);
$FileType = $info->getExtension();

# Change file extension in lowercase
$FileType = strtolower($FileType);

#$DirList = explode('\\',$file); #it's For Local
$DirList = explode('/',$file); #it's For Live

if(!array_intersect($DirList, $NotReqFile)) {
# If any file are not required live zip,rar Etc.
# 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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s