tags:

views:

309

answers:

2

Hi,

This question is related to a Windows install of PHP5.

Doing a file unlink() makes it difficult for recovery.

Instead I would like to move the file to the Recycle Bin (without doing an exec()).

Do you have any idea?

Thanks for your help.

+7  A: 

why dont you just create one folder and name it "Recycle Bin" .. then instead of doing an unlink() .. just move the files to this "Recycle Bin" folder??

If you wish to move a file, use the rename() php function.

Then later you can run a cron script which checks the time of the files and then you can delete files, say, older than 10 days etc.

I hope this helps.

Wbdvlpr
Indeed, it's the same principle. Recycle Bin is nothing more than a map in which files can't be executed. "Deleted" files still exists and still take up the same space in the Recycle Bin.
WebDevHobo
No restore from the Recyble Bin though..
Alix Axel
In my specific needs, the env. is Windows only. If the files are moved to the recycle bin, I can better interact with them with others binary tools (WinSCP, Explorer, etc.) as they are recognized as recycled files. A "Recycle Bin" in my application would not provide me all these functions and I would have to code functions which are already in the OS. :)
Toto
+3  A: 

This is the only solution that works and it's portable in all drives.

function Recycle($filename)
{
    if (is_file($filename) === true)
    {
     $filename = realpath($filename);
     $recycle = glob(current(explode('\\', $filename, 2)) . '\\RECYCLER\\*', GLOB_ONLYDIR);

     if (is_array($recycle) === true)
     {
      return rename($filename, current($recycle) '\\' . basename($filename));
     }
    }

    return false;
}

Deleted files are correctly moved to for instance:

O:\RECYCLER\S-1-5-21-1715567821-1390067357-1417001333-1003

Restore from the Recycle Bin should be possible, however I've not tested it.

EDIT: I just updated this function to work with files that have relative paths.

Alix Axel
Hi, thanks for the function. Unfortunately, it does not work for me (The file disappears but I do not know where. It is not in the system folder [DRIVE]:\RECYCLER\).) :( It seems (correct me if I wrong) that the OS is writing some data in a system file when we move the file to the "recycle bin". Maybe I should do an exec() with some DOS command. :) BTW I am on XP.
Toto
I'm also on XP and the this function works fine for me, the file shows up on Recycle Bin and I can also see it. For instance, if you are deleting a file located in C: make sure you enable "Show Hidden Folders" then browse to C:\RECYCLER\__SOMETHING__\ the file should be there, the same also works for other drives (default Windows behavior). It should also appear at the Recycle Bin.
Alix Axel
It needed some work on relative paths, should work fine now. Tell me if it still doesn't work.
Alix Axel
This one looks good, but I have to do further investigation. (It does not work for me). I suspect something wrong with my OS. Thanks. :)
Toto
That's weird, what value does the variable $recycle hold? Do var_dump($recycle); and post here the result.
Alix Axel