Jump to content

Recommended Posts

Is there a way to store data in array that is in a function and keep it?  it seems that everytime it leaves the function the data is gone.  I almost need a session array, can this be done?  What I am trying to do is scan my own website and store the full path to all directories in an array so I can go back to that directory later.  here is what I tried and failed $file is simple coming from an opendir command:

 

 

$_SESSION['all_dirs'][] = array(realpath(getcwd()).'/'.$file);

Link to comment
https://forums.phpfreaks.com/topic/206949-storing-data-in-arrays/
Share on other sites

ok here it is.  Like I said in my previous post, I am trying to scan my dir for files and other directories to see if something was modified in the same day.  My server has been hacked a couple of times now and would like to know when my files change asap to fix the website with a clean backup. What I am trying to do is scan the root dir first, then store all files in one array, store all dir in another array.  Go back into the dir array and change dir to whatever the 1st element directory is.  Then scan the new directory for more files and directories until all directories in the directory array have been scanned.  In a since, I want this program to be smart enough to crawl through my entire website looking for changed files in every single directory reguardless on how deep the directories get.  Here is my code, it works well in the root directory but when it scans other directories, it looses its count, im assuming because of a session thing.  Maybe there is a better way to do what I am doing anyways.. I am open to sugguestions.

 

<?php
// start up your PHP session! 
session_start();
// get full patch 
$root_path = realpath(getcwd());

$hour = date("H");
$today = date("m-d-Y");

$error_monitor_file = $root_path.'/file_modify.txt';

$count = 0;
$dir_loop_count = 0;


$current_dir = getcwd();
echo "current directory is from start: ".$current_dir;
echo "<br>";
$handle1 = opendir($current_dir);



	   			while (false !== ($file = readdir($handle1)))
	   			{			
		              // if result is a file and not a dir.. and not an image or image dir
					if($file != "." && $file != ".." && !is_dir($file) && $file['extension'] != "jpg" && $file['extension'] != "JPG" )  
	  		 		{ 
					  $all_files[] = realpath(getcwd()).'/'.$file;
					}
					  // if result is a dir and not a file and not image dir..
					if(is_dir($file) && $file != "." && $file != ".." && $file != "image" && $file != "images" && $file != "logos" && $file != "slideshow_images")  
					{
					   //$all_dirs[] = realpath(getcwd()).'/'.$file;
					   $_SESSION['all_dirs'] = array(realpath(getcwd()).'/'.$file);
					}
				}

				$count_files = count($all_files);

				$count_dir   = count($all_dirs);


				while($dir_loop_count < $count_dir )
				{
				  // see if new directories were added...
				  $count_dir   = count($all_dirs);
				  
				 // $new_dir = $all_dirs[$dir_loop_count];
				  $new_dir = $_SESSION['all_dirs'][$dir_loop_count];
				  chdir($new_dir);
				  scan_new_dir();
				  
				  chdir($root_path); // go back to root dir;
				  $dir_loop_count ++;
				 }


function scan_new_dir()
{
    $current_dir = getcwd();
    $handle2 = opendir($current_dir);				
	   			while (false !== ($file = readdir($handle2)))
	   			{
					  // if result is a file and not a dir.. and not an image or image dir
					if($file != "." && $file != ".." && !is_dir($file) && $file['extension'] != "jpg" && $file['extension'] != "JPG" )  
	  		 		{ 
					  $all_files[] = realpath(getcwd()).'/'.$file;
					}
					  // if result is a dir and not a file and not image dir..
					if(is_dir($file) && $file != "." && $file != ".." && $file != "image" && $file != "images" && $file != "logos" && $file != "slideshow_images")  
					{
					  // $all_dirs[] = realpath(getcwd()).'/'.$file;
					  $_SESSION[all_dirs][] = realpath(getcwd()).'/'.$file;
					}
					 	 $count_dir2   = count($_SESSION['all_dirs']);
 						$count_files2 = count($all_files);

						 echo "all files: ". $count_files2." amd all dir: ". $count_dir2;
 						 echo "<br>".$all_dirs;
				 }					 
}	

if (file_exists($error_monitor_file))
{
   echo "<br>send email...";
}		

if( ($hour == "00") && (file_exists($error_monitor_file) ))
{
  unlink($error_monitor_file);
}
exit;
?>

try

<?php
function scan_dir($path ){
$out = array();
$files = opendir($path);
while ($f = readdir($files)){
	if ($f=='.' or $f=='..' or $f['ektension']=='jpg' or $f['extension']=='JPG') continue;
	if (is_dir($path.'/'.$f)) $out = array_merge($out, scan_dir($path.'/'.$f)); 
	else $out[] = $path. '/'. $f;
}
return $out;
}
$a = scan_dir(getcwd());
print_r($a);
?>

thanks sasa, that was almost what I wanted.  I had to make a few changes but it put me on the right path.  Is there a reason why it is still pulling the .jpg files though?  I have looked at the code for a while now and still cant figure out why it is still looking and storing .jpg files.  I want to exclude all image files as they change often..  Thanks in advance. its not the misspelling on extension, i fixed that already.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.