Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for a PHP file to delete itself on execution?

Tags:

php

I have a PHP file "install.php" which handles the installation of other scripts. I want this file to delete itself and its containing folder after it has been run.

Is this possible?

like image 444
Bluemagica Avatar asked Mar 09 '11 07:03

Bluemagica


People also ask

Can PHP delete a file?

To delete a file in PHP, use the unlink function. Let's go through an example to see how it works. The first argument of the unlink function is a filename which you want to delete. The unlink function returns either TRUE or FALSE , depending on whether the delete operation was successful.

Which PHP function is used to delete a file?

The unlink() function deletes a file.

How are PHP documents executed?

The PHP interpreter reads the request, then executes the file by interpreting it top to bottom, line by line. The results are sent back to the web server, which passes the results to the browser.

How can delete upload file in PHP?

In PHP, we can delete any file using unlink() function. The unlink() function accepts one argument only: file name. It is similar to UNIX C unlink() function.


2 Answers

A demo given that the folder only contains your install.php:

mkdir demo
cd demo
echo "<?php unlink(__FILE__); rmdir(__DIR__); " > install.php
php install.php
cd ..
ls

The that ls doesn't show the "demo dir" any more.

The recursive deletion shouldn't be that hard to figure out ether if there are more (sub-)folders you want to remove.

like image 149
edorian Avatar answered Sep 25 '22 23:09

edorian


You can use PHP unlink and rmdir command to delete the file (and its folder) itself. Make sure you forward the viewer to other page after you delete the file itself.

like image 34
Raptor Avatar answered Sep 24 '22 23:09

Raptor