duanli0162 2013-06-04 22:35
浏览 6

调用文件以及其父目录来销毁自己?

I have a little project I'm working on, and have a little bit of an issue. I have a directory called "actions", and inside this folder I have a PHP file named "remove.php".

I'm trying to make it so when I call the "remove.php" file from outside the "actions" folder, the folder "actions" along with all contents are deleted. But can't seem to get it working since the file I'm calling to do the deletion is inside that "actions" directory.

Pretty much I'm wanting to call upon a file to destroy itself along with its parent directory.

Is this possible? I'm programming in PHP BTW.

Thanks in advance for any help.

This is my "remove.php" files code:

<?php
// Get parent folder
$parent = basename(dirname($_SERVER['PHP_SELF']));

// Loop through all files and folders, and remove them
foreach(glob($parent . '/*') as $file)
{ 
  if(is_dir($file))
  {
    rmdir($file);
  } else
  {
    unlink($file);
  }
}

// Remove parent folder after all files have been deleted
rmdir($parent); 

// Inform that folder has been deleted
echo "actions folder deleted";
?>
  • 写回答

2条回答 默认 最新

  • dongzhuan1185 2013-06-04 22:40
    关注

    On a unix-based system: definitely. Use rmdir and unlink. Windows systems tend to lock files that are currently open, so this may not work (although I can't be certain).

    Of course, your webserver needs to have full write permissions to the folder, as well as to its parent-folder.

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)