2

I have a php script that attempts to remove all files from a directory structure, but preserve everything in svn. I found this command online which does the job perfectly if you plug it directly in a shell

find /my/folder/path/ -path \'*/.svn\' -prune -o -type f -exec rm {} +

Unfortunately if I perform a shell_exec in php on that command like so:

$cmd = 'find $folderPath -path \'*/.svn\' -prune -o -type f -exec rm {} +';
shell_exec($cmd);

Then all the files in my current directory that I call the php script from are deleted as well.

Can someone explain why, and how to fix the issue so that I can fix the php script so it acts like expected, removing only those files in the specified folder

The full source code is below, just in case there is perhaps a silly mistake in there that I have missed:

<?php

# This script simply removes all files from a specified folder, that aren't directories or .svn 
# files. It will see if a folder path was given as a cli parameter, and if not, ask the user if they 
# want to remove the files in their current directory.

$execute = false;

if (isset($argv[1]))
{
    $folderPath = $argv[1];
    $execute = true;
}
else
{
    $folderPath = getcwd();
    $answer = readline("Remove all files but not folders or svn files in $folderPath (y/n)?" . PHP_EOL);

    if ($answer == 'Y' || $answer == 'y')
    {
        $execute = true;
    }
}

if ($execute)
{
    # Strip out the last / if it was given by accident as this can cause deletion of wrong files
    if (substr($folderPath, -1) != '/')
    {
        $folderPath .= "/";
    }

    print "Removing files from $folderPath" . PHP_EOL;
    $cmd = 'find $folderPath -path \'*/.svn\' -prune -o -type f -exec rm {} +';
    shell_exec($cmd);
}
else
{
    print "Ok not bothering." . PHP_EOL;
}

print "Done" . PHP_EOL;

?>
3
  • As always, superfluous phrases and greetings are: superfluous. If you were not looking for help we would not expect you posting here. Commented Dec 24, 2012 at 12:39
  • Apologies, I am looking for help. Can someone explain why, and how to fix the issue so that I can fix the php script so it acts like expected, removing only those files in the specified folder. Commented Dec 24, 2012 at 12:43
  • All fine, this just needs basic troubleshooting, for that you need to look that your code does what you expect. You do that by dumping variables in between and check if they contain what you think should be in there. Commented Dec 24, 2012 at 12:49

2 Answers 2

2

Your command looks okay. At least in shell. If you would actually troubleshoot your issue in PHP with a simple

var_dump($cmd);

You would see where your error lies:

$cmd = 'find $folderPath -path \'*/.svn\' -prune -o -type f -exec rm {} +';

Look closely. Hint: A single can't make a double for a dollar.

Sign up to request clarification or add additional context in comments.

1 Comment

sorry for giving the awnser to your hint:)
1

It all comes down to:

$cmd = 'find $folderPath -path \'*/.svn\' -prune -o -type f -exec rm {} +';
shell_exec($cmd);

Since you are using single quotes the variable $folderPath is not changed. So you are executing

find $folderPath -path '*/.svn' -prune -o -type f -exec rm {} +

instead of

find /my/folder/path/ -path \'*/.svn\' -prune -o -type f -exec rm {} +

use double quotes or $cmd = 'find '.$folderPath.' -path \'*/.svn\' -prune -o -type f -exec rm {} +';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.