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 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败