Moving a file to the trash (PHP)

This question is related to installing Windows for PHP5.

File execution unlink()makes recovery difficult.

Instead, I would like to move the file to the Recycle Bin (without execution exec()).

Do you have any ideas?

Thank you for your help.

+5
source share
3 answers

This is the only solution that works and is portable on 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 move correctly, for example:

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

Recovering from the trash should be possible, but I have not tested it.

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

+7
source

""... , unlink().. ""?

, rename() php function.

cron script, , , , 10 ..

, .

+7

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

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

All Articles