Backing up files to a zip(archive) file

I have this script which backs up a folder full of files, albeit I really I really intend to use it for a folder which has only images in the folder, nothing else

$timeZone = date_default_timezone_set('Europe/London');
$now = new DateTime();
$dateTimeNow = $now->format("Y-m-d H:i:s");
$_dateBack = date('d-m-Y', strtotime($dateTimeNow));

<!DOCTYPE html>
        <meta charset="UTF-8">
        <form action="backup_files.php" method="POST" enctype="multipart/form-data">
            Name for archive: <input type="text" name="name" id="name" value="Backup"><?php echo "-".$_dateBack; ?><br/>
            <div>Enter a different name for the archive if necessary</div>
            <input type="file" name="files[]" multiple="true" >
            <input type="submit" name="submit" value="Add Files/Folder to zip" >
        if (isset($_POST['submit'])){
            $filesArray = $_FILES['files'];
            for ($num = 0; $num < count($filesArray['name']); $num++){
                $fileName = $filesArray['name'][$num];
                $tempName = $filesArray['tmp_name'][$num];
                move_uploaded_file($tempName, 'tmp/'.$fileName);
            $archiveName = $_POST['name']."-".$_dateBack.".zip";
            $filesArrayNames = $_FILES['files']['name'];

            $zipDir = scandir('zips/');

            $error = false;
            foreach ($zipDir as $zipDirFile){
                if($zipDirFile == $archiveName){
                    $error = true;
                if($error == false){
                    $tmpDir = scandir('tmp/');
                    $zip = new ZipArchive();
                    $zip->open('zips/'.$archiveName, ZipArchive::CREATE);
                for ($num = 0; $num < count($filesArray['name']); $num++){
                    $fileName = $filesArray['name'][$num];
                    foreach ($tmpDir as $tmpDirFile){
                        if($tmpDirFile == $fileName){
                            echo "Adding $fileName <br />";
                for ($num = 0; $num < count($filesArray['name']); $num++){
                    $fileName = $filesArray['name'][$num];
                    foreach ($tmpDir as $tmpDirFile){
                        if($tmpDirFile == $fileName){
                } else {
                    echo "Name already exists";

All I want is to script this so all I do if I don’t change the name of the archive of the file, is press the submit button and it backs up the files in the remote folder, rather than use the browse button, because I want to back up the folder where the images and subfolder are stored on the remote because I will be backing up one folder (and subfolders) where the images are stored, therefore will find this (the browse button) un-necessary since this will be set in stone where the folder is

This post has been reformatted by enclosing the code block in 3 backticks
on their own lines.

The question is how to get list of only images in folder?

You can use this function instead of scandir:

function getDirectoryListing($path){ 
    // allowed extensions
    $exts = array('png', 'jpg', 'jpeg', 'gif');

    // files and subfolders to ignore
    $ignore = array( '.', '..' ); 

    $results = array();
    $dh = @opendir($path); 

    while( false !== ($file = readdir($dh)) ){ 
        if(in_array($file, $ignore) ){ continue; }
	$ext = pathinfo($path.'/'.$file, PATHINFO_EXTENSION);		
	if (!in_array($ext, $exts)) { continue; }
	$results[] = $path . '/' . $file;		

   return $results;



$tmpDir = getDirectoryListing('tmp');

Sample output:

    [0] => tmp/figures.png
    [1] => tmp/bg.png
    [2] => tmp/heart.png
    [3] => tmp/crowns.png
    [4] => tmp/bg1.png
    [5] => tmp/ground.png
    [6] => tmp/blocks.png

No, the question isn’t to list of image in folder, it’;s to back up the folder and subfolder where images are stored

So no one knows how this is done?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.