PHP
  Home arrow PHP arrow Page 5 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 / 25
    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 5 of 7 )

    The RenameFile Function
    This function attempts to rename a file on the FTP server from $OldFileName to $NewFileName:

    function RenameFile($OldFileName, $NewFileName, $IsFolder, &$Err)
    {
    // Rename a file/directory on the FTP server
    global $php_errormsg;

    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;
    }
    }

    $renameResult = @ftp_rename($this->__conn, $OldFileName, $NewFileName);

    if($renameResult == true)
    {
    // The file/folder was renamed successfully
    return true;
    }
    else
    {
    // Couldn't rename the file/folder
    if($IsFolder == 0)
    $Err = "Couldn't rename the selected folder: " . @$php_errormsg;
    else
    $Err = "Couldn't rename the selected file: " . @$php_errormsg;

    return false;
    }
    }


    The DeleteFile Function
    This function attempts to delete a file called $FileName from the FTP server:

    function DeleteFile($FileName, &$Err)
    {
    // Remove the specified file from 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;
    }
    }

    $deleteResult = @ftp_delete($this->__conn, $FileName);

    if($deleteResult == true)
    {
    // The file/folder was renamed successfully
    return true;
    }
    else
    {
    // Couldn't delete the selected file
    $Err = "Couldn't delete the selected file: " . @$php_errormsg;
    return false;
    }
    }


    The DeleteFolder Function
    This function was one of the hardest to write. It recursively deletes all files and folders from a directory called $FolderName:

    function DeleteFolder($FolderName, &$Err)
    {
    // Remove the specified folder and all subdirectories/files from the FTP server
    global $php_errormsg;

    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;
    }
    }

    @ftp_chdir($this->__conn, $FolderName);
    $location = @ftp_pwd($this->__conn);

    $directories = array();
    $files = array();
    $dir_counter = 0;
    $file_counter = 0;
    $content = @ftp_nlist($this->__conn, ".");

    for ($i = 0; $i < sizeof($content); $i++)
    {
    // If we can change into this then it's a directory.
    // If not, it's a file
    if($content[$i] != "." && $content[$i] != "..")
    {
    if(@ftp_chdir($this->__conn, $content[$i]))
    {
    // We have a directory
    $directories[] = $content[$i];
    $dir_counter++;
    @ftp_cdup($this->__conn);
    }
    else
    {
    // We have a file
    $files[] = $content[$i];
    $file_counter++;
    }
    }
    }

    for ($j = 0; $j < $file_counter; $j++)
    {
    @ftp_delete($this->__conn, $files[$j]);
    }

    for ($j = 0; $j < $dir_counter; $j++)
    {
    if($directories[$j] != "." OR $directories[$j] != "..")
    {
    $location = ftp_pwd ($this->__conn);
    $this->DeleteFolder($directories[$j], $this->__dummyError);
    @ftp_cdup ($this->__conn);
    @ftp_rmdir($this->__conn,$directories[$j]);
    }
    }

    // Lastly, we change into the directory that we want to delete and see
    // if we can cdup. If we can, we delete it.
    @ftp_chdir($this->__conn, $FolderName);
    @ftp_cdup($this->__conn);
    @ftp_rmdir($this->__conn, $FolderName);

    // Did the recursive folder/file deletion work?
    return true;

    /*
    if(@$php_errormsg == "")
    {
    return true;
    }
    else
    {
    $Err = "Couldn't recursive delete folder: " . @$php_errormsg;
    return false;
    }
    */
    }


    The DoesFileExist Function
    This function checks if a file called $FileName exists in the directory called $FolderName on the FTP server. It returns true if the file exists, and false if it doesn’t:

    function DoesFileExist($FolderName, $FileName, &$Err)
    {
    // Does the specified file exist on the remote FTP server?
    // Returns false on error, true on file exists and 2 if it doesn't exist

    global $php_errormsg;

    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;
    }
    }

    if(!@ftp_chdir($this->__conn, $FolderName))
    {
    $Err = $php_errormsg;
    return false;
    }
    else
    {
    // We have changed into the directory, let's get a list
    // of files using ftp_nlist and compare it to see if it exists
    $fileArray = @ftp_nlist($this->__conn, $FolderName);

    if(!is_array($fileArray))
    {
    $Err = $php_errormsg;
    return false;
    }
    else
    {
    // Loop through each file and check it if exists
    for($i = 0; $i < sizeof($fileArray); $i++)
    {
    if($fileArray[$i] == "$FolderName/$FileName")
    {
    return true;
    }
    }

    // The file wasn't found, return 2 for not found
    return 2;
    }
    }
    }
    }

    More PHP Articles
    More By Mitchell Harper


       · Ive been toying with the idea of writing an FTP class too, so Im glad someone else...
       · Wow this is exactly what I was looking for... great job... however, I got one...
     

    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-2010 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek