Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts

    list_files function that sees .htaccess files?

    EDIT:
    Duh l was overthinking the whole thing ... a simple:
    Code:
    if(file_exists($dir . '/.htaccess')){
         array_push($files, $dir . '/.htaccess');
    }
    Get's it done...
    Here's the completed code that will create a backup of your entire site (or just specified directories) if anyone wants:
    PHP Code:
    <?php
    set_time_limit
    (0);
    ini_set('memory_limit''0');

    /* creates a compressed zip file */
    function create_zip($files = array(),$destination '',$overwrite false) {
        
    //if the zip file already exists and overwrite is false, return false
        
    if(file_exists($destination) && !$overwrite) { return false; }
        
    //vars
        
    $valid_files = array();
        
    //if files were passed in...
        
    if(is_array($files)) {
            
    //cycle through each file
            
    foreach($files as $file) {
                
    //make sure the file exists
                
    if(file_exists($file)) {
                    
    $valid_files[] = $file;
                }
            }
        }
        
    //if we have good files...
        
    if(count($valid_files)) {
            
    //create the archive
            
    $zip = new ZipArchive();
            if(
    $zip->open($destination,$overwrite ZIPARCHIVE::OVERWRITE ZIPARCHIVE::CREATE) !== true) {
                return 
    false;
            }
            
    //add the files
            
    foreach($valid_files as $file) {
                
    $zip->addFile($file,$file);
            }
            
    //debug
            //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

            //close the zip -- done!
            
    $zip->close();

            
    //check to make sure the file exists
            
    return file_exists($destination);
        }
        else
        {
            return 
    false;
        }
    }

    function 
    list_files($dir) {
        if(
    $dh opendir($dir)) {
            
    $files = Array();
            
    $inner_files = Array();
            while(
    $file readdir($dh)) {
                if(
    $file != "." && $file != ".." && $file[0] != '.') {
                    if(
    is_dir($dir "/" $file)) {
                        
    //chmod($dir . "/" . $file, 0777);
                        
    $inner_files list_files($dir "/" $file);
                        if(
    is_array($inner_files)) $files array_merge($files$inner_files); 
                    } else {
                        if(
    file_exists($dir '/.htaccess')){
                            
    array_push($files$dir '/.htaccess');
                        }
                        
    array_push($files$dir "/" $file);
                    }
                }
            }
            
    closedir($dh);
            return 
    $files;
        }
    }

    create_zip(list_files($_SERVER[DOCUMENT_ROOT]), $_SERVER[DOCUMENT_ROOT] . '/backup_' date('m-d-Y') . '.zip'true);
    echo 
    '<a href="/backup.zip">done</a>';

    ?>
    -----------------------------
    ORIGINAL POST:

    Hi people, l'm using the following function along with a create_zip function in PHP to create backups of my site on the fly. Here's the code that recrusivily goes through my directories and creates an array of all files in the directory l point it at (IE my root directory).

    PHP Code:
    function list_files($dir) {
        if(
    $dh opendir($dir)) {
            
    $files = Array();
            
    $inner_files = Array();
            while(
    $file readdir($dh)) {
                if(
    $file != "." && $file != ".." && $file[0] != '.') {
                    if(
    is_dir($dir "/" $file)) {
                        
    //chmod($dir . "/" . $file, 0777);
                        
    $inner_files list_files($dir "/" $file);
                        if(
    is_array($inner_files)) $files array_merge($files$inner_files); 
                    } else {
                        
    array_push($files$dir "/" $file);
                    }
                }
            }
            
    closedir($dh);
            return 
    $files;
        }

    My Problem is that it doesn't collect my .htaccess files & add them to the array as well; can anyone tell me how l could tweak this function so that it'd add the .htaccess files to the returned array?

    Thanks to anyone that can help!

    Regards,
    Last edited by Coastal Web; 03-23-2010 at 03:35 AM.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Try this:

    Code:
    function list_files($dir) {
        $excluded = array('.', '..');
    
        if($dh = opendir($dir)) {
            $files = Array();
            $inner_files = Array();
            while($file = readdir($dh)) {
                if(!in_array($file, $excluded)) {
                    if(is_dir($dir . "/" . $file)) {
                        //chmod($dir . "/" . $file, 0777);
                        $inner_files = list_files($dir . "/" . $file);
                        if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
                    } else {
                        array_push($files, $dir . "/" . $file);
                    }
                }
            }
            closedir($dh);
            return $files;
        }
    }
    Last edited by MattF; 03-23-2010 at 03:38 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •