Link to ftp Program

Discussion in 'PHP' started by locpicker, Jun 22, 2011.

  1. #1
    I found this code online and I would like to change it so that it can get a download from a url and then transfer it to a server using ftp. Can anybody help? I have some of it done but I am not sure if it is correct or not.

    PHP_FTP
    
    <?php 
    
    /* 
        Alot of people have asked for VERY simple FTP functionality with PHP, so here it is. 
        This class was designed with very simplistic transfers in mind. You simple create the class, 
        get/send a file and then 'kill()' the object. It's that simple. I you find it useful. 
    
        Example: 
    
        //Get a file 
            require("class.ftp.php"); //include library 
            $f=new PHP_FTP('ftp.somesite.com', 'username', 'password'); //specify connect info 
            $f->get('html/test.txt', 'c:/php/ftp/blah.txt'); //yes, tested on Windows 
            $f->kill(); //optional destroy class method 
    
        //Send a file 
            require("class.ftp.php"); 
            $f=new PHP_FTP('ftp.somesite.com', 'username', 'password', 21); //optional port as 4th arg 
            $f->send('c:/php/ftp/blah.txt', 'html/test.txt'); //yes, tested on Windows 
            $f->kill(); 
    
         //how to test for completion 
            if(!$f->send('c:/php/ftp/blah.txt', 'html/test.txt')){ 
                echo "File sent successfully!"; 
            }else{ 
                echo "Error sending file."; 
            } 
    
         Notes: 
            Remember to have all permissions set to their appropriate settings before using 
                this class 
    */ 
    
    
        class PHP_FTP{ 
    
            var $server=''; 
            var $username=''; 
            var $password=''; 
            var $port=21; 
            var $remote_dir=''; 
    
                function PHP_FTP($server, $username='anonymous', $password='e@mail.com', $port=21){ 
                    $this->server=$server; 
                    $this->username=$username; 
                    $this->password=$password; 
                    $this->port=$port; 
                } 
                  
            //exterior 
    
                function send($filename='', $save_as='', $passive=TRUE){ 
                    $conn=$this->return_connection() or die; 
                    @ftp_pasv($conn, $passive); 
                    $this->set_remote_dir(ftp_pwd($conn)); 
                        if(!ftp_put($conn, $save_as, $filename, FTP_BINARY)){ 
                            @ftp_quit($this->conn); 
                            return false; 
                        }else{ 
                            @ftp_quit($this->conn); 
                            return true; 
                        } 
                    return true; 
                } 
    
                function get($filename='', $save_as='', $passive=TRUE){ 
                    $conn=$this->return_connection() or die; 
                    @ftp_pasv($conn, $passive); 
                    $this->set_remote_dir(ftp_pwd($conn)); 
                        if(!ftp_get($conn, $save_as, $this->remote_dir.$filename, FTP_BINARY)){ 
                            @ftp_quit($this->conn); 
                            return false; 
                        }else{ 
                            @ftp_quit($this->conn); 
                            return true; 
                        } 
                } 
    
                function kill(){ 
                        if($this->conn) 
                            $this->disconnect(); 
                    unset($this); 
                } 
    
            //interior 
                function return_connection(){ 
                    $conn_id = @ftp_connect($this->server, $this->port) or die("Could not connect to FTP"); 
                    $login_result = @ftp_login($conn_id, $this->username, $this->password) or die("Could not login to FTP"); 
                    return $conn_id; 
                } 
    
                function set_remote_dir($dir){ 
                    $x = substr($dir, (strlen($dir)-1)); 
                        if($x != "/" && $x != "\\") 
                            $dir.="/"; 
                    $this->remote_dir=$dir; 
                } 
        } 
    ?> 
    
    /* example */ 
    
    require("class.ftp.php"); 
    $f=new PHP_FTP('ftp.somesite.com', 'username', 'password', 21); //optional port as 4th arg 
    $f->send('c:/php/ftp/blah.txt', 'html/test.txt'); //yes, tested on Windows 
    $f->kill();
    
    Code (markup):
    Here is what I have done so far.

    <p>Download URL: <input type="text" name="url" method="post"/></p>
    <p>Program Name: <input type="text" name="program" method="post"/></p>
    <p>Ftp Username: <input type="text" name="user" method="post" hidden=""/></p>
    <p>Ftp Password: <input type="text" name="pass" method="post"/></p>
    <p>Ftp Server URL: <input type="text" name="server" method="post"/></p>
    <p><input type="submit" /></p>
    
    
    <?php
    
    $url=$_POST[url];
    $program=$_POST[program];
    $user=$_POST[user];
    $pass=$_POST[pass];
    $server=$_POST[server];
    
    
    
     //Get a file 
            //require("class.ftp.php"); //include library 
            //$f=new PHP_FTP($url); //specify connect info 
            $f->get($url); //yes, tested on Windows 
            $f->kill(); //optional destroy class method 
    
        //Send a file 
            require("class.ftp.php"); 
            $f=new PHP_FTP($server, $user, $pass, 21); //optional port as 4th arg 
            $f->send($url, $program); //yes, tested on Windows 
            $f->kill();
    		
    		//how to test for completion 
            //if(!$f->send('c:/php/ftp/blah.txt', 'html/test.txt')){ 
               // echo "File sent successfully!"; 
    		   
    		   $size = round((filesize($_POST[to])/1000000), 3); 
    print "transfer complete.<br> 
    <a><a href=\"$_POST[from]\">$_POST[url]</a><br> 
    <a><a href=\"$_POST[to]\">$_POST[program]</a> : $size MB";
    
            else{ 
                echo "Error sending file."; 
            } 
    		
    		 class PHP_FTP{ 
    
            var $server=''; 
            var $username=''; 
            var $password=''; 
            var $port=21; 
            var $remote_dir=''; 
    
                function PHP_FTP($server, $username='anonymous', $password='e@mail.com', $port=21){ 
                    $this->server=$server; 
                    $this->username=$username; 
                    $this->password=$password; 
                    $this->port=$port; 
                } 
                  
            //exterior 
    
                function send($filename='', $save_as='', $passive=TRUE){ 
                    $conn=$this->return_connection() or die; 
                    @ftp_pasv($conn, $passive); 
                    $this->set_remote_dir(ftp_pwd($conn)); 
                        if(!ftp_put($conn, $save_as, $filename, FTP_BINARY)){ 
                            @ftp_quit($this->conn); 
                            return false; 
                        }else{ 
                            @ftp_quit($this->conn); 
                            return true; 
                        } 
                    return true; 
                } 
    
                function get($filename='', $save_as='', $passive=TRUE){ 
                    $conn=$this->return_connection() or die; 
                    @ftp_pasv($conn, $passive); 
                    $this->set_remote_dir(ftp_pwd($conn)); 
                        if(!ftp_get($conn, $save_as, $this->remote_dir.$filename, FTP_BINARY)){ 
                            @ftp_quit($this->conn); 
                            return false; 
                        }else{ 
                            @ftp_quit($this->conn); 
                            return true; 
                        } 
                } 
    
                function kill(){ 
                        if($this->conn) 
                            $this->disconnect(); 
                    unset($this); 
                } 
    
            //interior 
                function return_connection(){ 
                    $conn_id = @ftp_connect($this->server, $this->port) or die("Could not connect to FTP"); 
                    $login_result = @ftp_login($conn_id, $this->username, $this->password) or die("Could not login to FTP"); 
                    return $conn_id; 
                } 
    
                function set_remote_dir($dir){ 
                    $x = substr($dir, (strlen($dir)-1)); 
                        if($x != "/" && $x != "\\") 
                            $dir.="/"; 
                    $this->remote_dir=$dir; 
                } 
        } 
    ?> 
    Code (markup):
    I have commented part of it out.

    I have not started fooling with the class part yet. It looks to me like it could stand alone.
     
    locpicker, Jun 22, 2011 IP
  2. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If the remote file is a pulic url then why use ftp to get it. You could just use php copy command:

     
    mallorcahp, Jun 23, 2011 IP