Home » Php » windows – Moving a file to the Recycle Bin (PHP)

windows – Moving a file to the Recycle Bin (PHP)

Posted by: admin July 12, 2020 Leave a comment


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.

How to&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:


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.


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.


This works for me, using MaDdoGs CmdUtils, under Win7 x64, php 5.4.24. Also with large files, odd filenames, …

function recycle($filename)
    if ( !is_file($filename) )
        return false;
    system( __DIR__.'/third-party/Recycle.exe -f "'.$filename.'"', $r );
    return $r;