Hello again. I have this backup script to grab files/folders rar them and save them. What i have been tring to do is ignore certain files. ignore test.php within folder.
Thank you

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>



    <script type="text/javascript">
    function submitform(){
        $('#myForm').post("backup.php", $("#myForm").serialize());
    }
    </script> </head>

<form action="backup.php" method="post" id"myForm">
<input type="hidden" name="txt"/>
<input type="submit" value="Backup" />
</form>
<?php
    if($_SERVER['REQUEST_METHOD'] == "POST")  {



$directory = '../data';



// the name of your zip archive to be created
$zipfile = 'backup.rar';


$filenames = array();

// function that browse the directory and all subdirectories inside

function browse($dir) {
global $filenames;
    if ($handle = opendir($dir)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && is_file($dir.'/'.$file)) {
                $filenames[] = $dir.'/'.$file;
            }
            else if ($file != "." && $file != ".." && is_dir($dir.'/'.$file)) {
                browse($dir.'/'.$file);
            }
              }
        closedir($handle);
    }
    return $filenames;
}

browse($directory);

// creating zip archive, adding browsed files

$zip = new ZipArchive();

if ($zip->open($zipfile, ZIPARCHIVE::CREATE)!==TRUE) {
    exit("cannot open <$zipfile>\n");
}

foreach ($filenames as $filename) {
    echo "Adding " . $filename . "<br/>";
    $zip->addFile($filename,$filename);
}

echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "<br/>";
$zip->close();
echo 'Backup Complete. <a href="./backup.rar">Download Backup</a>'. "<br/>";
 } else {

    }
?>

Recommended Answers

All 7 Replies

Change this:

function browse($dir) {
    global $filenames;
    if ($handle = opendir($dir)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && is_file($dir.'/'.$file)) {
                $filenames[] = $dir.'/'.$file;
            }
            else if ($file != "." && $file != ".." && is_dir($dir.'/'.$file)) {
                browse($dir.'/'.$file);
            }
        }
        closedir($handle);
    }
    return $filenames;
}

to this:

function browse($dir) 
{
    global $filenames;

    // Fill with names of files to exclude from your return array.
    $files_to_exclude = array(
        'file1.php'
    );

    if($handle = opendir($dir)) {
        while (false !== ($file = readdir($handle))) {
            if($file == '.' || $file == '..') {
                //* Skip currentt and previous directory.
                continue;
            }
            elseif(in_array($file, $files_to_exclude)) {
                //* The current file is to be excluded.
                continue;
            }

            if(is_file($dir . '/' . $file)) {
                $filenames[] = $dir . '/' . $file;
            }
            elseif(is_dir($dir . '/' . $file)) {
                browse($dir . '/' . $file);
            }
        }

        closedir($handle);
    }

    return $filenames;
}

And see if it works :).

hell ya minitauros thank you so much.

I know my original question is solved but on the same note is there a way to add a password to the zip file..
Is this even possible?

There is a way to add a password to it but I've never done it with PHP. A short Google search resulted in this command to be used:

exec('zip -P pass file.zip file.txt');

pass = password
file.zip = target zip
file.txt = file to zip (maybe you can specify a folder if you want to zip multiple files? I have no idea, you'd have to try :))

Unzipping would go as follows:

exec('unzip -P password file.zip');

(From: http://stackoverflow.com/questions/7712960/opening-and-creating-password-protected-zip-files-with-php and http://www.talkphp.com/general/3380-exec-zip-php-not-working-expected.html)

Great thanks for the replys. ill look at this later today.

okay well i could not get everything in the folder to zip, so i just zipped the zip file with a pass and unlink the original zip file. A bit messy but seems to work.

$zip = new ZipArchive();


if ($zip->open($zipfile, ZIPARCHIVE::CREATE )!==TRUE)
{
    exit("cannot open <$zipfile>\n");
}

foreach ($filenames as $filename) {
    echo "Adding " . $filename . "<br/>";
    $zip->addFile($filename,$filename);
}

echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "<br/>";

$zip->close();
echo 'Backup Complete. <a href="backup/backup.rar">Download Backup</a>'. "<br/>";
 } else {

    }

?>

</div>
<?php
exec('zip -P pass backup/backup.rar backup/site_backup.rar');
?>

<script type="text/javascript">
    function submitform(){
<?php
unlink ($zipfile);
?>
}
</script>

I really appreciate your guys help
thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.