Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving a file to the Recycle Bin (PHP)

Tags:

file

php

windows

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.

like image 334
Toto Avatar asked Aug 13 '09 10:08

Toto


People also ask

Which shortcut key is used to move the files to Recycle Bin?

Ctrl + D Delete the selected item and move it to the Recycle Bin. Ctrl + Esc Open the Start Menu. Ctrl + Shift Switch the keyboard layout. Ctrl + Shift + Esc Open Task Manager.


2 Answers

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.

like image 85
Alix Axel Avatar answered Sep 22 '22 12:09

Alix Axel


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.

like image 26
TigerTiger Avatar answered Sep 20 '22 12:09

TigerTiger