完成脚本后删除脚本创建的所有文件 php

Deleting all files created by script after finsihing script php

有没有办法删除 运行 脚本创建的所有文件。我写了一个代码,创建了很多 xml 个文件。它们必须通过所有 xml 文件存储到脚本 运行。脚本完成后,不再需要这些文件。是否有可能编写一个代码来删除同一脚本中的这些文件?

我通过在本地保存 xml 文件在我的循环中使用了 unlike(filname)。问题是它在我可以在代码的下一部分中使用之前删除了我的文件。

foreach ($links as $lin){
unlike($filename);
}

在脚本文件末尾添加如下代码:

$files = glob('path/to/files/dir/*');
foreach($files as $file){
  if(is_file($file)) {
    unlink($file); 
  }
}

添加以下脚本

$files = glob('path/to/temp/*'); // get all file names
 foreach($files as $file){ // iterate files
  if(is_file($file))
    unlink($file); // delete file
 }

如果要删除 'hidden' 文件,如 .htaccess,则必须使用

$files = glob('path/to/temp/{,.}*', GLOB_BRACE);