dqf2015 2017-12-25 23:15
浏览 91
已采纳

php,exec,shell exec,rm不能lstat

I am on WIN7, using PHP and Gnu CoreUtils 5.3.0 I need to delete recursively from a folder with mask, in instance all *.php and *.txt files.

exec(
    'rm -rf ../root_compressed/*.php *.txt'
    //.' '.$SILENT
    , $out, $ret);

if($ret)
    die("
ERROR!!!
");

This one fails with rm: cannot lstat `*.txt': Invalid argument

Now using

shell_exec(
    'rm -rf ../root_compressed/*.php *.txt'
    //.' '.$SILENT
);

This works.

I need exec() since I'd like to know if success or not. What is the problem with rm?

  • 写回答

1条回答 默认 最新

  • dsdtszi0520538 2017-12-26 00:00
    关注

    If the *.txt files are also located in the root_compressed folder, you need to include that as well in your rm command:

    exec(
        'rm -rf ../root_compressed/*.php ../root_compressed/*.txt'
        //.' '.$SILENT
        , $out, $ret);
    

    You can also use curly brace expansion if your server supports it:

    exec(
        'rm -rf ../root_compressed/*.{php,txt}'
        , $out, $ret);
    

    Note that the recursive flag (-r) on the rm command won't do much in this case, since you're only looking explicitly for *.php and *.txt entries. So unless your subfolders are named like that, this will not recursively delete all *.php and *.txt from every subfolder in the root_compressed folder.

    I tried getting a one-liner command to do that using find, but I couldn't get it to accept two file types in one argument. You'd have to use something like find ../root_compressed -type f -name "*.php" -delete multiple times for each file type you want to delete.

    However, it's quite easy to script it yourself using the SPL library:

    $dir_iterator = new RecursiveDirectoryIterator("../root_compressed");
    $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
    
    foreach ($iterator as $file) {
        if ($file->isFile() && preg_match("~\.(php|txt)$~i", $file->getFilename())) {
            unlink($file->getRealpath());
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器