KiNgPiTo Posted December 21, 2009 at 06:28 PM Report Share #301710 Posted December 21, 2009 at 06:28 PM Boas pessoal 🙂 Alguem me dá uma ajuda nisto? Preciso de criar um ciclo que elimine todos os ficheiros que estejam numa pasta excepto aqueles cujo nome estão num array... não estou mesmo a ver como fazer isto... ? Cumprimentos e muito obrigado 🙂 Link to comment Share on other sites More sharing options...
IceBrain Posted December 21, 2009 at 08:17 PM Report Share #301735 Posted December 21, 2009 at 08:17 PM //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 More sharing options...
KiNgPiTo Posted December 22, 2009 at 12:41 PM Author Report Share #301813 Posted December 22, 2009 at 12:41 PM 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 More sharing options...
IceBrain Posted December 22, 2009 at 01:20 PM Report Share #301816 Posted December 22, 2009 at 01:20 PM 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 More sharing options...
KiNgPiTo Posted December 22, 2009 at 02:50 PM Author Report Share #301826 Posted December 22, 2009 at 02:50 PM Sim... O que interessa é que ficou a dar e como é para usar numa função não interessa muito.. Muito obrigado pela ajuda 😉 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now