Welcome Guest, Not a member yet? Register   Sign In
FTP Extension: Download, Reverse Mirror
#1

[eluser]caleblloyd[/eluser]
Hello,

I have created a simple extension for the FTP library that includes the option to download a file from remote to local or mirror a directory from remote to local. It is my personal recommendation that this be included in CI's next release.

Code:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/**
* FTP Extension
*
* Adapted from the CodeIgniter Core Classes
* @link    http://codeigniter.com
*
* Description:
* This library extends the CodeIgniter FTP class.
*
* Install this file as application/libraries/MY_Ftp.php
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
**/
class MY_Ftp extends CI_Ftp
{
     /**
     * Download a file from the server
     *
     * @access    public
     * @param    string
     * @param    string
     * @param    string
     * @return    bool
     */
    function download($rempath, $locpath, $mode = 'auto', $permissions = NULL)
    {
        if ( ! $this->_is_conn())
        {
            return FALSE;
        }

        // Set the mode if not specified
        if ($mode == 'auto')
        {
            // Get the file extension so we can set the upload type
            $ext = $this->_getext($rempath);
            $mode = $this->_settype($ext);
        }

        $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;

        $result = @ftp_get($this->conn_id, $locpath, $rempath, $mode);

        if ($result === FALSE)
        {
            if ($this->debug == TRUE)
            {
                $this->_error('ftp_unable_to_download');
            }
            return FALSE;
        }

        // Set file permissions if needed
        if ( ! is_null($permissions))
        {
            chmod($locpath, (int)$permissions);
        }

        return TRUE;
    }
    
    function mirror_download($rempath, $locpath)
    {
        if ( ! $this->_is_conn())
        {
            return FALSE;
        }

        // Open the remote file path
        if ($this->changedir($rempath, TRUE))
        {
            // Attempt to open the local file path.
            if ( ! is_dir($locpath))
            {
                if ( ! mkdir($locpath) )
                {
                    return FALSE;
                }
            }

            // Recursively read the local directory
            $files_in_directory = $this->list_files();
            foreach ($files_in_directory as $file)
            {
                if ($this->changedir($rempath.$file."/", TRUE))
                {
                    $this->changedir($rempath, TRUE);
                    $this->mirror_download($rempath.$file."/", $locpath.$file."/");
                }
                 elseif ( (substr($file, 0, 1) != ".") OR ((substr($file, 0, 2) == "./") && (substr($file, 2, 1) != ".")) )
                {
                    // Get the file extension so we can set the download type
                    $ext = $this->_getext($file);
                    $mode = $this->_settype($ext);

                    $this->download($rempath.$file, $locpath.$file, $mode);
                }
            }
            return TRUE;
        }

        return FALSE;
    }

}

/* End of file MY_Ftp.php */
/* Location: ./system/applications/libraries/MY_Ftp.php */

And add this line to your language file:

Code:
$lang['ftp_unable_to_download']        = "Unable to download the specified file.  Please check your path.";
#2

[eluser]Phil Sturgeon[/eluser]
I wrote this method up about 3 years ago, and finally it has been accepted into the core for 2.0. You really should google before you write code like this. :-)
#3

[eluser]caleblloyd[/eluser]
Phil-

I did study your code before creating this extension. Your code that I looked over is hosted here:

http://styledna.pastebin.com/f77873a3

The fundamental problem was your code did not make provisions for mirroring a remote directory into the local directory, it merely provided downloading of one file at a time.

This extension does not make use of multiple configurations like your extension, however, it does have function that deals with the mirroring of a remote folder into a local one.

I hope that CI 2.0 will include the mirroring functionality provided by this extension as well as the download functionality provided by both this extension and your original extension.

One thing to think about including in the FTP download function is a retry mechanism that allows a config parameter for number of retries on downloading a file before the function outright fails and throws an error message. Also, an option for silent fail would be nice...

I would be glad to develop these and send them into CI. I just am not sure how to do that.

-Caleb




Theme © iAndrew 2016 - Forum software by © MyBB