PHP
  Home arrow PHP arrow Page 4 - My FTP Wrapper Class for PHP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

My FTP Wrapper Class for PHP
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2003-02-21

    Table of Contents:
  • My FTP Wrapper Class for PHP
  • Wrapping? Huh?
  • Functions, Functions and More Functions!
  • The Functions (contd.)
  • The Functions (contd.)
  • Examples of Using the MY_FTP Class
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    My FTP Wrapper Class for PHP - The Functions (contd.)


    (Page 4 of 7 )

    The SaveDataToRemoteFile function
    This function will attempt to create a file called $FileName in the $Directory folder on the FTP server and will write $Data to this file. If the file already exists then an error will be raised:

    function SaveDataToRemoteFile($Data, $FileName, $Directory, &$Err)
    {
    // Save HTML to a remote file on the FTP server
    if(!$this->IsStillConnected())
    {
    // Connect is dead, attempt to reconnect
    $err = "";
    @$this->Connect($this->__server, $this->__user, $this->__password, $this->__directory, $err);

    if($err != "")
    {
    $this->HandleError($err);
    die();
    }
    else
    {
    return true;
    }
    }

    // We are now connected, so let's retrieve the file contents.
    // Firstly, we change into the directory
    $chDir = @ftp_chdir($this->__conn, $Directory);

    if(!$chDir)
    {
    $Err = "Couldn't change into directory: $Directory";
    return false;
    }

    // Save the HTML to a file which we then upload
    $fp = @fopen("localfile", "wb");

    if(!$fp)
    {
    $Err = "Couldn't open a local file for temporary output";
    return false;
    }

    // The file was opened OK, let's write to it
    $filePut = @fputs($fp, $Data, strlen($Data));

    if(!$filePut)
    {
    $Err = "Couldn't write to a local file for temporary output";
    return false;
    }
    else
    {
    @fclose($fp);
    }

    // Now we can try to write to the remote file
    $CompleteFileName = $Directory . "/" . $FileName;
    $LocalFileName = "localfile";

    $putFile = @ftp_put($this->__conn, $CompleteFileName, $LocalFileName, FTP_BINARY);

    if(!$putFile)
    {
    $Err = "Couldn't write to $CompleteFileName when trying to save file";
    return false;
    }

    // Everything worked OK
    return true;
    }


    The SwitchDirectory Function
    This function simply changes into the $Directory folder on the FTP server. If a connection or permission error occurs then $Err will contain the error message:

    function SwitchDirectory($Directory, &$Err)
    {
    // Switch to another directory on the web server. If we don't
    // have permissions then an error will occur

    if(!$this->IsStillConnected())
    {
    // Connect is dead, attempt to reconnect
    $err = "";
    @$this->Connect($this->__server, $this->__user, $this->__password, $this->__directory, $err);

    if($err != "")
    {
    $this->HandleError($err);
    die();
    }
    else
    {
    return true;
    }
    }

    // Try and change into another directory
    $chDir = ftp_chdir($this->__conn, $Directory);

    if(!$chDir)
    {
    $Err = "Couldn't change into directory: $Directory";
    return false;
    }
    else
    {
    // Changing directories worked OK
    return true;
    }
    }


    The GetFileListAsArray Function
    This function will change into the $Directory folder and get a list of files and directories contained in that folder. This function still needs a lot of work, but should work in most cases:

    function GetFileListAsArray($Directory, &$Err)
    {
    // This function will attempt to change into the specified
    // directory and retrieve a list of files as an associative
    // array. This list will include file name, size and date last modified

    $err = "";
    $fileArray = array();

    // Can we switch to the desired directory?
    if(!$this->SwitchDirectory($Directory, $err))
    {
    $this->HandleError($err);
    }

    // We are in the directory, let's retrieve a list of files
    $fileList = ftp_rawlist($this->__conn, $Directory);

    // Save the list of files
    if(@is_array($fileList))
    {
    // Interate through the array
    for($i = 0; $i < sizeof($fileList); $i++)
    {
    $itemArray = explode(" ", $fileList[$i]);

    // Are we dealing with a file or directory?
    // If the first letter of the attributes is
    // "d" then we are dealing with a directory
    $attributes = $itemArray[0];

    if(substr($attributes, 0, 1) == "d")
    $fileType = FT_DIRECTORY;
    else
    $fileType = FT_FILE;

    // Get the file/directory name
    $fileName = $itemArray[sizeof($itemArray)-1];

    // Get the size of the file
    $fileSize = $itemArray[sizeof($itemArray)-5];

    if(!is_numeric($fileSize))
    $fileSize = $itemArray[sizeof($itemArray)-6];

    // Get the date last modified
    $fileTimeStamp = $itemArray[sizeof($itemArray)-4] . " " . $itemArray[sizeof($itemArray)-3] . " " . $itemArray[sizeof($itemArray)-2];

    $fileArray[] = array("type" => $fileType,
    "filename" => $fileName,
    "filesize" => $fileSize,
    "filetime" => $fileTimeStamp);
    }

    sort($fileArray);
    return $fileArray;
    }
    else
    {
    $Err = "No files in directory";
    return -1;
    }
    }


    The CreateDirectory Function
    This function tries to make a new directory called $folderName on the FTP server. If it can create the folder, then the folder is given appropriate rights with the CHMOD command:

    function CreateDirectory($folderName, &$Err)
    {
    // Makes a new folder on the web server via FTP

    if(!$this->IsStillConnected())
    {
    // Connect is dead, attempt to reconnect
    $err = "";
    @$this->Connect($this->__server, $this->__user, $this->__password, $this->__directory, $err);

    if($err != "")
    {
    $this->HandleError($err);
    die();
    }
    else
    {
    return true;
    }
    }

    $createResult = @ftp_mkdir($this->__conn, $folderName);

    if($createResult == true)
    {
    // Can we change the files permissions?
    $execResult = @ftp_site($this->__conn, 'chmod 0777 ' . $folderName . '/');

    if($execResult == true)
    {
    return true;
    }
    else
    {
    $Err = "Couldn't set owner permissions on $folderName";
    return false;
    }
    }
    else
    {
    $Err = "Couldn't create new folder $folderName";
    return false;
    }
    }

    More PHP Articles
    More By Mitchell Harper


       · Ive been toying with the idea of writing an FTP class too, so Im glad someone else...
     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway