1

I am trying to create a self-destruct file. Wht I mean is, if a conditional equates to true, the file deletes itself.

Seems to me the following code should do the trick. However, it does nothing. What am I doing wrong?

<?php 
    phpinfo();
    // The following should be activated when the url is 
    // selfdestruct.php?delete=1, correct?
    if ($_GET['delete']==1) {
        $file = 'selfdestruct.php';
        unlink($file);
    }
?>

Thanks for your hep in advance! I appreciate it! :-)

ByronArn
  • 215
  • 1
  • 2
  • 5

3 Answers3

0

Have you checked your web server and php log files? Could be a permissions issue or that the web server is keeping the file open so it can't be deleted.

Also, try to pass the entire local path to the file to unlink. You could probably use unlink(_FILE_)

Also, is this on Windows or Linux? They handle "open" files a bit differently. I tested this on Linux and it works fine with

 unlink(__FILE__);

Information about OS and running web server etc is probably good to add to a question of this nature.

inquam
  • 12,664
  • 15
  • 61
  • 101
  • I am running Fedora 18 and httpd (Which I believe is Apche2?). I changed the above and it does not work still... – ByronArn Apr 25 '13 at 08:04
  • Have you checked that it's not a permission issue? What does the log files say? Do the httpd user have the permissions to delete the file in question? – inquam Apr 25 '13 at 08:09
  • I changed the file permissions so that anyone can read & write the file. I also moved the code to the top s suggested. It still isn't deleting itself. – ByronArn Apr 25 '13 at 08:13
  • Strange... I wonder if SELinux or something is stopping this from happening. If you place another file in the folder and try to delete that from the script, does that work? – inquam Apr 25 '13 at 08:17
  • It might be SELinux. Don't know too much bout it... Anyways, it does not delete another file in the same folder... – ByronArn Apr 25 '13 at 08:23
  • Ok, try to place a script in the folder... run it as root from the terminal and have that script delete a file. Does that work? It SHOULD :)... And if it does it might be some configuration issue of the web server or php. Did you check the logs and enable error reporting? Nothing came out of that? – inquam Apr 25 '13 at 08:33
0

Check these steps:

  1. Put error_reporting(E_ALL); on the first line
  2. Check your permissions
  3. Put the delete code above anything else, after "error_reporting"

You should do the following:

if (isset($_GET['delete']) && $_GET['delete'] == '1') {
    unlink(FILE);
}
machineaddict
  • 3,216
  • 8
  • 37
  • 61
0

I usually do it like this: (using your GET)

if ($_GET['delete']==1) {
    unlink( __FILE__ ) or die("Please delete this file.");
    echo "This file has been deleted.<br />";
}

If the file can't be deleted it exit the script and the last echo won't be shown.

And also, your code should work, a quick test you can do to check if the condition is met, but just unable to delete the file is:

if ($_GET['delete']==1) {
    echo "Works!";
}
Jay
  • 704
  • 6
  • 8
  • Huh, strange. It rechoed the file had been deleted, but the file is still there... – ByronArn Apr 25 '13 at 07:58
  • did you check if the file was still there in ftp/file browser or through the browser? it could be a cached file if you access through the browser. – Jay Apr 25 '13 at 08:04
  • Yes, I used my file manager to check it. Its still there unfortunately. – ByronArn Apr 25 '13 at 08:10