Jump to content

Help with Logic


Moon-Man.net

Recommended Posts

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?
[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]
Link to comment
Share on other sites

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 :)
Link to comment
Share on other sites

Ok, i have progressed i think :S
I 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/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------------>
[/quote]

But doesnot actually read the files in the sub directory :S
Both me and my boss are stumped :S
Thanks :)
Nathan
Link to comment
Share on other sites

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.