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?
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.
The unlink() function deletes a file.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With