Home arrow PHP arrow Page 5 - My FTP Wrapper Class for PHP
PHP

My FTP Wrapper Class for PHP


In this article Mitchell shares his FTP wrapper class which he has written in PHP. It includes error checking and many useful functions.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 33
February 21, 2003
TABLE OF CONTENTS:
  1. · My FTP Wrapper Class for PHP
  2. · Wrapping? Huh?
  3. · Functions, Functions and More Functions!
  4. · The Functions (contd.)
  5. · The Functions (contd.)
  6. · Examples of Using the MY_FTP Class
  7. · Conclusion

print this article
SEARCH DEVARTICLES

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

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials