Miscellaneous Code

  Home arrow Miscellaneous Code arrow PHP_FTP
MISCELLANEOUS CODE

PHP_FTP
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-01-18

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    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.

    By : ryanflynn

    <?php

    /*
    PHP_FTP
    Version 0.5
    Ryan Flynn (ryan@ryanflynn || DALnet->#php->pizza_milkshake)
    Tuesday, June 26 2001

    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();
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap