Moon-Man.net Posted September 6, 2006 Share Posted September 6, 2006 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 :DI think i have created an infinent loop though :SA little help?[code]#!/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" ;?>[/code] Quote Link to comment Share on other sites More sharing options...
btherl Posted September 6, 2006 Share Posted September 6, 2006 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:[code]#!/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" ;?>[/code]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 :) Quote Link to comment Share on other sites More sharing options...
Moon-Man.net Posted September 6, 2006 Author Share Posted September 6, 2006 Ok, i have progressed i think :SI have this now:[code]#!/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" ;?>[/code]And it outputs:[quote]<------------Starting Replace!------------>OPENING: /tmp/TESTINGPATH: /tmp/TESTING/TESTINGtouchTESTINGSSSD{}@@File found: TESTINGtouchTESTINGSSSD{}@@PATH: /tmp/TESTING/ERRORFile found: ERRORPATH: /tmp/TESTING/TESTING2Dir found: /tmp/TESTING/TESTING2OPENING: /tmp/TESTING/TESTING2PATH: /tmp/TESTING/INSITERERROR::S:S?"PATH: /tmp/TESTING/.PATH: /tmp/TESTING/INSITERERROR\!\@\@\@\#\#2PATH: /tmp/TESTING/..PATH: /tmp/TESTING/errerFile found: errerPATH: /tmp/TESTING/.PATH: /tmp/TESTING/ERROR??File found: ERROR??Bad file found: /tmp/TESTING/ERROR?? Replacing with /tmp/TESTING/ERRORPATH: /tmp/TESTING/..PATH: /tmp/TESTING/TESTINGSSSD{}File found: TESTINGSSSD{}<------------Replace Finnished------------>[/quote]But doesnot actually read the files in the sub directory :SBoth me and my boss are stumped :SThanks :)Nathan Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.