Jump to content

Eliminar tudo excepto.....


KiNgPiTo
 Share

Recommended Posts

//crias o array com os ficheiros
$nao_apagar = array('image.jpg', 'documento.pdf');
//primeiro abres a pasta
if ($handle = opendir('/path/to/files')) {
    //Depois, lês cada ficheiro até acabarem
    while (false !== ($file = readdir($handle))) {
        //agora, confirmas que é um ficheiro (e não uma pasta) e que o nome do ficheiro não está no array
        if( is_file($file) && !in_array($file, $nao_apagar) ) {
            unlink($file);
        }
    }
    //finalmente, fechas a pasta
    closedir($handle);
}

opendir()

readdir()

is_file()

in_array()

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

Muito obrigado!! Ajudaste imenso...

Uma pequena correcção... o método readdir retorna apenas o nome do ficheiro e não o caminho para o ficheiro, por isso nos métodos is_file e unlink que precisam do caminho para o ficheiro e assim só têm o nome (pelo menos sem adicionar isso a mim não me deu...). Foi assim que deixei e a funcionar:

//crias o array com os ficheiros
$nao_apagar = array('doc1.pdf', 'doc2.pdf');
//primeiro abres a pasta
if ($handle = opendir('docs/pdfs')) {
    //Depois, lês cada ficheiro até acabarem
    while (false !== ($file = readdir($handle))) {
        //agora, confirmas que é um ficheiro (e não uma pasta) e que o nome do ficheiro não está no array
        if( is_file('docs/pdfs'.$file) && !in_array($file, $nao_apagar) ) {
            unlink('docs/pdfs'.$file);
        }
    }
    //finalmente, fechas a pasta
    closedir($handle);
}

Mais uma vez Muito Obrigado 😉

Link to comment
Share on other sites

Ah, sim, nem considerei isso. Também poderias usar

chdir('docs');

antes e

chdir('..');

depois do código, porque assim o PHP "entra" dentro da pasta antes de fazer as operações. Mas é opcional.

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.