...

View Full Version : Resolved list_files function that sees .htaccess files?



Coastal Web
03-23-2010, 04:30 AM
EDIT:
Duh l was overthinking the whole thing ... a simple:


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
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).


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,

MattF
03-23-2010, 04:36 AM
Try this:



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;
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum