maybe101
maybe101

Reputation: 35

How to delete current file on exit (in php?)

I know there is a temp file function, but I would like to use create and delete file function on entering and exit from the user (deletes itself on exit). I tried to use both but none worked,

first:

<?php
$link = $_SERVER['SERVER_NAME'] . dirname(__FILE__);

printf("%s\n", $link);

array_map('unlink', glob("$link/*.php")); 

 ?>

2nd:

 <?php

$filee = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";   


    printf("%s\n", $link);
    unlink(realpath($filee));

     ?>

Upvotes: 3

Views: 1608

Answers (2)

Anto S
Anto S

Reputation: 2449

For first one:

<?php
if(file_exists($filePath)){
    unlink($filePath);
}
?>

For second one, you cant unlink any file which is not in your domain.

More over you cant unlink a http request. You have to use relative path, If you will get unlink request in uniform manner, you have to explode and get the file name, frame relative path then unlink it

Upvotes: 1

Peter Bowers
Peter Bowers

Reputation: 3093

You cannot delete (or unlink) http URLs. Instead you need to delete FILES.

$link = "/tmp/foo.tmp";
unlink($link);

Or, to delete all the php files in the directory of your current FILE (credit to @Deadooshka above as well as the top comment on the php unlink page ):

array_map('unlink', glob(__DIR__ . '/*.php'));

You will notice that we are not prepending the $_SERVER['SERVER_NAME'] onto the beginning of the path.

Upvotes: 4

Related Questions