Welcome Guest, Not a member yet? Register   Sign In
FTP Class: delete_dir bug
#1

[eluser]Unknown[/eluser]
Hello everyone,

This is my first forum post. I did research about this topic and only thing I found was 5 years old topic (http://ellislab.com/forums/viewthread/73334/#390497) so...

Right now I am working on project which goal is to remotely backup 100+ Websites. For that purpose I used native CI FTP library and, until now, I didn't have any troubles. But, when I tried to delete folder on remote server (delete_dir method) I end up with infinitive loop.

After short investigation, I found following code problematic (/system/libraries/Ftp.php - Line 422):

Code:
$list = $this->list_files($filepath);

        if ($list !== FALSE AND count($list) > 0) {
            foreach ($list as $item) {
                // If we can't delete the item it's probaly a folder so
                // we'll recursively call delete_dir()
                if (!@ftp_delete($this->conn_id, $item)) {
                    $this->delete_dir($item);
                }
            }
        }

First of all, $list variable contains relative names of files/folders, so this will not work when you have sub folders.
Another problem is that, inside foreach loop, there is no condition which will check if $item is '.' or '..', so (at least on Linux system), this script will fail into infinitive loop.

I fixed it simply, by extending CI native class and slight change inside that loop:

Code:
foreach ($list as $item) {

                if (!in_array($item, array('.', '..'))) {

                    // If we can't delete the item it's probaly a folder so
                    // we'll recursively call delete_dir()
                    if (!@ftp_delete($this->conn_id, $filepath . $item)) {
                        $this->delete_dir($filepath . $item);
                    }
                }
            }

Full code:

Code:
class MY_FTP extends CI_FTP {

    public function __construct($config = array()) {
        parent::__construct($config);
    }

    function delete_dir($filepath) {
        if (!$this->_is_conn()) {
            return FALSE;
        }

        // Add a trailing slash to the file path if needed
        $filepath = preg_replace("/(.+?)\/*$/", "\\1/", $filepath);

        $list = $this->list_files($filepath);
        if ($list !== FALSE AND count($list) > 0) {
            foreach ($list as $item) {

                if (!in_array($item, array('.', '..'))) {

                    // If we can't delete the item it's probaly a folder so
                    // we'll recursively call delete_dir()
                    if (!@ftp_delete($this->conn_id, $filepath . $item)) {
                        $this->delete_dir($filepath . $item);
                    }
                }
            }
        }

        $result = @ftp_rmdir($this->conn_id, $filepath);
        if ($result === FALSE) {
            if ($this->debug == TRUE) {
                $this->_error('ftp_unable_to_delete');
            }
            return FALSE;
        }

        return TRUE;
    }

}
#2

[eluser]Aken[/eluser]
You should submit a pull request.




Theme © iAndrew 2016 - Forum software by © MyBB