Move file from FTP to server

Discussion in 'PHP' started by inderpal, Jun 21, 2007.

  1. #1
    Hi All,

    I have a file on FTP (it could be on any FTP server, will have username and password).

    I need to download it from FTP and upload it to my web server (in backend process only).

    I have a button in admin panel. When I click on that, file should be moved from FTP to the pre-defined server location. Is there any direct way to achive this?

    Thanks in advance.......

    Regards,
    Inderpal Singh
     
    inderpal, Jun 21, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    <?php
    define("FTP_SERVER",			'krakjoe.com');
    define("FTP_TIMEOUT",			5 );
    define("FTP_PORT",				21 );
    define("FTP_USERNAME",			'username');
    define("FTP_PASSWORD",			'password');
    define("FTP_FILE",				'/www/index.php');
    define("SCRIPT_SAVE_AS",		'test.php');
    
    class ftp
    {
    	function ftp( )
    	{
    		if( !( $this->con = ftp_connect( FTP_SERVER, FTP_PORT, FTP_TIMEOUT ) ) )
    		{
    			die( sprintf( "Cannot establish ftp connect @ %s on port %d", FTP_SERVER, FTP_PORT ) );
    		}
    		elseif( !ftp_login( $this->con, FTP_USERNAME, FTP_PASSWORD ) ) 
    		{
    			die( sprintf( "Cannot login @ %s with %s and supplied password", FTP_SERVER, FTP_USERNAME ) );
    		}
    	}
    	function get( )
    	{
    		return ftp_get( $this->con, SCRIPT_SAVE_AS, FTP_FILE, FTP_BINARY );
    	}
    	function close( )
    	{
    		return ftp_closE( $this->con );
    	}
    }
    $ftp = new ftp( );
    
    if( $_POST )
    {	
    	if( $ftp->get( ) )
    	{
    		printf("File saved @ %s", SCRIPT_SAVE_AS );
    	}
    	else
    	{
    		printf("Cannot download %s from %s", FTP_FILE, FTP_SERVER );
    	}
    }
    ?>
    <html>
    	<head>
    		<title>Grab file from ftp using button</title>
    	</head>
    	<body>
    	<form action="" method="post">
    		<input type="submit" name="action" value="Submit">
    	</form>
    	</body>
    </html>
    
    PHP:
     
    krakjoe, Jun 21, 2007 IP
  3. inderpal

    inderpal Active Member

    Messages:
    919
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    68
    #3
    Thanks krakjoe for your help. That script of yours works like a charm.

    Thanks again.....
     
    inderpal, Jun 21, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    always a pleasure .... :)
     
    krakjoe, Jun 21, 2007 IP