我有一个 php 脚本,它试图从目录结构中删除所有文件,但将所有内容保留在 svn 中。我在网上找到了这个命令,如果你直接将它插入 shell,它就可以完美地完成工作
find /my/folder/path/ -path \'*/.svn\' -prune -o -type f -exec rm {} +
不幸的是,如果我在 php 中对该命令执行 shell_exec,如下所示:
$cmd = 'find $folderPath -path \'*/.svn\' -prune -o -type f -exec rm {} +';
shell_exec($cmd);
然后,我调用 php 脚本的当前目录中的所有文件也将被删除。
有人可以解释为什么,以及如何解决这个问题,以便我可以修复 php 脚本,使其像预期的那样运行,只删除指定文件夹中的那些文件
完整的源代码如下,以防万一我错过了一个愚蠢的错误:
<?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;
?>