Source for file FTPHost.class.php
Documentation is available at FTPHost.class.php
* Including various functions to manipulate arrays.
require_once( CLASSPATH. "core/Arrays.class.php" );
include_once( CLASSPATH. "filesystem/Files.class.php" );
* @see http://www.php.net/manual/en/function.ftp-chmod.php#66456
* Sets the permissions on the specified remote file to mode.
* @author rabin at rab dot in
* @param resource $ftp_stream
* @param string $filename
function ftp_chmod($ftp_stream, $mode, $filename)
sprintf('CHMOD %o %s', $mode, $filename)
sprintf('CHMOD %u %s', $mode, $filename)
* @author Daniel Plücken <daniel@debakel.net>
* @license http://www.gnu.org/copyleft/lesser.html
* GNU Lesser General Public License
* @copyright Copyright (C) 2005 Daniel Plücken <daniel@debakel.net>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* The Name of the databasehost.
* The username to log on the ftp server.
* The password to log on the ftp server.
* The connection's state of the ftp host.
* @var boolean $is_connected
* @param string $password
function FTPHost( $host, $user, $password, $port = 21 )
* Attempts to change the mode of the specified file to that given in mode.
* @param string $str_path
* @param string $disconnect
function chmod( $str_path, $str_mod, $disconnect = true )
$mix_out = ftp_chmod( $this->resID, $str_mod, $str_path );
* Renames/moves a given file/directory.
* @param boolean $disconnect
function rename( $oldname, $newname, $disconnect = true )
$bool_tmp = ftp_rename( $this->resID, $oldname, $newname );
* To dissolve a directory means that all containing files will be shifted up
* a level and the directory itself will be removed.
* Beware: It does not function if there are files with the same name in the
* directory which has to be dissolve and the directory one level up.
$str_dir, "", "", false, "name", false
foreach ( $file_arr as $str_file )
$str_to_dir. "/". $str_file["name"], false
$str_dir. "/". $str_file["name"],
$str_to_dir. "/". $str_file["name"],
$bool_no_err = ftp_rmdir( $this->resID, $str_dir );
* Copys a given file/directory.
* @param string $fromname
* @param boolean $disconnect
function copy( $fromname, $toname, $disconnect = true )
$bool_tmp = ftp_fget( $this->resID, $tmp_file, $fromname, FTP_BINARY );
&& ftp_fput( $this->resID, $toname, $tmp_file, FTP_BINARY );
* Checks whether the given file exists.
* @param string $filename
* @param boolean $disconnect
if ( $this->is_dir( $filename, $disconnect ) )
$upper_dir = $tmp_arr[0];
$upper_dir, "", "", false, "name", $disconnect
foreach ( $dir_cont_arr as $value )
if ( $value["name"] == $tmp_arr[1] )
* Checks whether the given file exists.
* @param string $filename
* @param boolean $disconnect
function is_file( $filename, $disconnect = true )
if ( $this->is_dir( $filename, $disconnect ) )
$upper_dir = $tmp_arr[0];
$upper_dir, "", "", false, "name", $disconnect
foreach ( $dir_cont_arr as $value )
if ( $value["name"] == $tmp_arr[1] )
* Checks whether the given directory exists.
* @param boolean $disconnect
function is_dir( $dirname, $disconnect = true )
# remember the current directory
$out_bool = @ftp_chdir( $this->resID, $dirname );
# change to root directory
# change in the right current directory
* Creates a directory with given name.
function mkdir( $dirname )
* Creates a file with given name and content.
* @param string $file_path
function saveFile( $file_path, $content )
$f->setContent( $content );
$out = ftp_fput( $this->resID, $file_path, $f->tmp_fh, FTP_BINARY );
* Puts out the size of a given file.
* @param string $filename Where the file can be found.
* @param boolean $disconnect
function filesize( $filename, $disconnect = true )
$out_int = ftp_size ( $this->resID, $filename );
* Loads the content of a given file.
* @param string $filename Where the file can be found.
* @param boolean $disconnect
$out_str = fread( $f, $this->filesize( $filename, $disconnect ) );
* Reads out a remote folder.
* @see http://www.php.net/manual/en/function.ftp-rawlist.php
* @author tig3r at poczta dot onet dot pl
* @author davidknoll at o2 dot co dot uk
* @author Daniel Plücken <daniel@debakel.net>
* @param string $regExpMatchFiles
* @param string $regExpMatchFolder
* @param boolean $filesize
* @param boolean $order_by
* @param boolean $disconnect
foreach( $list as $current )
"([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):"
. "([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)",
empty( $regExpMatchFolder )
empty( $regExpMatchFiles )
$split[3] += 1900; // 4digit year fix
$parsed[$i]['is_dir'] = $split[7] == "<DIR>";
$parsed[$i]['size'] = $split[7];
$parsed[$i]['month'] = $split[1];
$parsed[$i]['day'] = $split[2];
$parsed[$i]['time/year'] = $split[3];
$parsed[$i]['name'] = $split[8];
foreach ( $list as $current )
$split = preg_split( "[ ]", $current, 9, PREG_SPLIT_NO_EMPTY );
empty( $regExpMatchFolder )
empty( $regExpMatchFiles )
$parsed[$i]['is_dir'] = $split[0]{0} === "d";
$parsed[$i]['perms'] = $split[0];
$parsed[$i]['number'] = $split[1];
$parsed[$i]['owner'] = $split[2];
$parsed[$i]['group'] = $split[3];
$parsed[$i]['size'] = $split[4];
$parsed[$i]['month'] = $split[5];
$parsed[$i]['day'] = $split[6];
$parsed[$i]['time/year'] = $split[7];
$parsed[$i]['name'] = $split[8];
if( count( $parsed ) > 1 )
* @param string $local_file
* @param string $remote_file
* Deletes a file or a folder. Folders will be delete recursively!
* @param string $filename
* @param string $disconnect
function delete( $filename, $disconnect = true )
# it is a file! -> delete it!
for( $i = 0; $i < count( $tmp_arr ); $i++ )
if( $tmp_arr[$i]["is_dir"] )
$this->delete( $tmp_arr[$i]["name"], false );
* Returns the hostname. This method is an alias of the method getHost().
* Returns the specified port to use to connect to the host.
* Returns the specified user who should connect with to the host.
* Returns the given password which should be usedto connect to the host.
* Stets the connection port. Standard is 21.
* Opens a connection to the ftp server.
* Closes the current connection to the ftp server.
} // END of class FTPHost
|