Jump to content


Photo

Help with Logic


  • Please log in to reply
2 replies to this topic

#1 Moon-Man.net

Moon-Man.net
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts

Posted 06 September 2006 - 03:14 AM

Could someone please help me with some logic with this one?
the script is ment to remove banned charactures from FILE names, within all directories in and below $dir.
It worked when i had it before without calling itsself from in the Function. and successfully removed all bad ones :D

I think i have created an infinent loop though :S
A little help?
#!/usr/bin/php
<?PHP
$dir = '/tmp/TESTING';
$cnt=0;

function remove_bar_char($file){
	$handle = opendir($file);
	while(($file = readdir($handle)) !== false){
		echo "File found: " .$file ."\n";
	    if (preg_match('/[' .preg_quote('\/ : * ? " < > |', '/') .']/', $file)){
			echo "Bad file found: " .$file ."  " ;
			$cnt++ ;
        	$path = $dir . DIRECTORY_SEPARATOR . $file;
        	if (!in_array($file, array('..', '.')) && is_file($path)){
	            $contents = file_get_contents($path);
            	$newPath = $dir . DIRECTORY_SEPARATOR . preg_replace('/[' .preg_quote('\/ : * ? " < > |', '/') .']/', '', $file);
            	echo "Replacing with " .$newPath ."\n" ;
	            if (file_put_contents($newPath, $contents) !== false){
	               unlink($path); //delete old file only if able to create new
	          }
	        }
    	}elseif(is_dir($file)){
	    	remove_bar_char($file) ;
	    }
	}
}

echo "<------------Starting Replace!------------>\n" ;
echo "\n";
remove_bar_char($dir);
echo "\n";
echo "\n";
echo "<------------Replace Finnished------------>\n" ;
echo "There were " .$cnt ." Replacements Made\n" ;
?>


#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 06 September 2006 - 05:15 AM

Welcome back moon-man :)

I think your problem is with variable names.  At the top you have $dir = '/tmp/TESTING', and at the bottom you call remove_bar_char($dir).  So far so good.

But inside remove_bar_char(), your variable names are all mixed up.  I will have a go at fixing it:

#!/usr/bin/php
<?PHP
$start_dir = '/tmp/TESTING';
$cnt=0;

# The input for remove_bar_char() is always a directory, so I will call it $dir.
function remove_bar_char($dir){
        global $cnt; # So we can keep a global count

	$handle = opendir($dir);
	while(($file = readdir($handle)) !== false){
		echo "File found: " .$file ."\n";
	    if (preg_match('/[' .preg_quote('\/ : * ? " < > |', '/') .']/', $file)){
			echo "Bad file found: " .$file ."  " ;
			$cnt++ ;
        	$path = $dir . DIRECTORY_SEPARATOR . $file;
        	if (!in_array($file, array('..', '.')) && is_file($path)){
	            $contents = file_get_contents($path);
            	$newPath = $dir . DIRECTORY_SEPARATOR . preg_replace('/[' .preg_quote('\/ : * ? " < > |', '/') .']/', '', $file);
            	echo "Replacing with " .$newPath ."\n" ;
	            if (file_put_contents($newPath, $contents) !== false){
	               unlink($path); //delete old file only if able to create new
	          }
	        }
    	      }elseif(is_dir($file)){
	    	remove_bar_char($file) ;
	    }
	}
}

echo "<------------Starting Replace!------------>\n" ;
echo "\n";
remove_bar_char($start_dir);
echo "\n";
echo "\n";
echo "<------------Replace Finnished------------>\n" ;
echo "There were " .$cnt ." Replacements Made\n" ;
?>

With functions, you can use a different variable name when you call it.  That variable will be renamed within the function.  For example:

$other_dir = '/tmp/OTHER';
remove_bar_char($other_dir);

Even though you called it with $other_dir, you must use just "$dir" within the function, because the function is declared as "function remove_bar_char($dir)".

Hope that helps :)

#3 Moon-Man.net

Moon-Man.net
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts

Posted 06 September 2006 - 07:10 AM

Ok, i have progressed i think :S
I have this now:

#!/usr/bin/php
<?PHP
$dir = '/tmp/TESTING';

function remove_bar_char($file){
	global $dir ;
	$handle = opendir($file);
	echo "OPENING: " .$file ."\n" ;
	while(($file = readdir($handle)) !== false){
		$path = $dir . DIRECTORY_SEPARATOR . $file;
		echo "PATH: " .$path ."\n" ;
		if (is_file($file) && !in_array($file, array('..', '.'))){
			echo "File found: " .$file ."\n";
			if (preg_match('/[' .preg_quote('\/ : * ? " < > |', '/') .']/', $file)){
				echo "Bad file found: " .$path ."  \n" ;
	           	if (is_file($path) && !in_array($file, array('..', '.'))){
		            $contents = file_get_contents($path);
	            	$newPath = $dir . DIRECTORY_SEPARATOR . preg_replace('/[' .preg_quote('\/ : * ? " < > |', '/') .']/', '', $file);
	            	echo "\t Replacing with " .$newPath ."\n" ;
		            if (file_put_contents($newPath, $contents) !== false){
		               unlink($path); //delete old file only if able to create new
		          	}
		        }elseif(is_dir($path) && !in_array($file, array('..', '.'))){
		    		remove_bar_char($path) ;
	    		}
	    	}
		}elseif(is_dir($file) && !in_array($file, array('..', '.'))){
			echo "Dir found: " .$path ."\n";
     		remove_bar_char($path) ;
    	}
	}
}

echo "<------------Starting Replace!------------>\n" ;
echo "\n";
remove_bar_char($dir);
echo "\n";
echo "\n";
echo "<------------Replace Finnished------------>\n" ;
?>
And it outputs:

<------------Starting Replace!------------>

OPENING: /tmp/TESTING
PATH: /tmp/TESTING/TESTINGtouchTESTINGSSSD{}@@
File found: TESTINGtouchTESTINGSSSD{}@@
PATH: /tmp/TESTING/ERROR
File found: ERROR
PATH: /tmp/TESTING/TESTING2
Dir found: /tmp/TESTING/TESTING2
OPENING: /tmp/TESTING/TESTING2
PATH: /tmp/TESTING/INSITERERROR::S:S?"
PATH: /tmp/TESTING/.
PATH: /tmp/TESTING/INSITERERROR\!\@\@\@\#\#2
PATH: /tmp/TESTING/..
PATH: /tmp/TESTING/errer
File found: errer
PATH: /tmp/TESTING/.
PATH: /tmp/TESTING/ERROR??
File found: ERROR??
Bad file found: /tmp/TESTING/ERROR??
        Replacing with /tmp/TESTING/ERROR
PATH: /tmp/TESTING/..
PATH: /tmp/TESTING/TESTINGSSSD{}
File found: TESTINGSSSD{}


<------------Replace Finnished------------>


But doesnot actually read the files in the sub directory :S
Both me and my boss are stumped :S
Thanks :)
Nathan




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users