/ Published in: PHP
A recursive function is a function that has the ability to call itself (recursion). I ran into this problem while trying to delete a directory containing files and/or other directories. I was using FTP at the time so, the function will be written as such. It can be easily ported to using filesystem functions by following the same logic/flow.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php # server credentials $host = "ftp server"; $user = "username"; $pass = "password"; # connect to ftp server # login using credentials function recursiveDelete($directory) { # here we attempt to delete the file/directory { # if the attempt to delete fails, get the file listing # loop through the file list and recursively delete the FILE in the list foreach($filelist as $file) { recursiveDelete($file); } #if the file list is empty, delete the DIRECTORY we passed recursiveDelete($directory); } } ?>
URL: http://jrtashjian.com/2008/08/27/code-snippet-recursive-delete-with-ftp-2/