简体   繁体   中英

Deleting all files of a folder including script itself in php

Suppose I have a folder name 'tempF' now i want to write a script in php inside the folder so that whenever the file is hit it will delete all the files inside 'tempF' including the script itself. What is the best possible way to do it in php. I have tried recursive unlink but it is not deleting the script itself.

Thanks in advance.

我认为这里有一个答案“如何使用 php 删除特定文件夹中的所有文件”

array_map('unlink', glob("some/dir/*.txt"));

I assume you are talking about a PHP script run thru a web-browser http visit.

Assuming that the folder itself and the files (including the PHP script) are write-permitted, then you can delete the files (including the PHP script).

Just use glob to get all the files and then use unlink to delete.

<?php


$files = glob('./*'); // get all file names
foreach($files as $file){ // iterate files
  if(is_file($file)) {
 
echo $file;
echo "<br>";

    unlink($file); // delete file

  }
}

?>

So after running this file (ONCE), if you run again (thru a browser visit), it will return 404 (because it has been deleted) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM