/*
Class that connects to a FTP server to work with files and directores.
It features:
- connect to ftp server
- change current working dir
- create new empty file
- create new dir
- change access right to object
- send SITE command
- copy file
- move and rename file or dirs
- move uploaded file from TMP into CWD
- delete file or empty directory
- write into file
*/
class php_ftp_class{
var $user;
var $pw;
var $host;
var $root_dir;//root ftp dir of server
var $con_id;//descriptor on ftp connection
var $cwd;//current working dir
var $FTP_MODE=FTP_BYNARY;// FTP_ASCII | FTP_BYNARY
var $ERR=true;//must object display ftp errors
//constr.
function my_ftp($user="guest",$pw="guest",$host="localhost",$root=""){
$this->host=$host;
$this->user=$user;
$this->pw=$pw;
$this->root_dir=$root;
if($this->connect()){
$this->cwd=ftp_pwd($this->con_id);
}
}
//connect to ftp server
function connect(){
if($this->con_id=ftp_connect($this->host)){
if(ftp_login($this->con_id,$this->user,$this->pw))return true;
else $this->error("User "".$this->user."" cannot login to host "".$this->host.""");
}else $this->error("Connection with host "".$this->host."" not create!");
return false;
}
//close ftp connection
function close(){
ftp_quit($this->con_id);
}
//print error messages
function error($err_str=""){
if($this->ERR)echo "[".$err_str."]
\n";
}
//change current working dir
function cd($dir){
if(!@ftp_chdir($this->con_id,$dir)){
$this->error("Cannot view directory "".$this->root_dir."/".$dir.""!");
return false;
}
$this->cwd=@ftp_pwd($this->con_id);
return true;
}
//create new empty file
function mk_file($name){
if(file_exists($this->root_dir."/".$this->cwd."/".$name)){
$this->error("File "".$this->root_dir."/".$this->cwd."/".$name."" already exists!");
return false;
}else{
if(!$tmpfile=tempnam("/tmp","phpftptmp")){
$this->error("Can't create temp file?");
return false;
}elseif(!ftp_put($this->con_id,$name,$tmpfile,$this->FTP_MODE)){
$this->error("Can't create file "".$this->root_dir."/".$this->cwd."/".$name.""");
unlink($tmpfile);
return false;
}
}
unlink($tmpfile);
return true;
}
//create new dir
function mk_dir($name){
if(file_exists($this->root_dir."/".$this->cwd."/".$name)){
$this->error("Directory "".$this->root_dir."/".$this->cwd."/".$name."" already exists!");
return false;
}elseif(!ftp_mkdir($this->con_id,$name)){
$this->error("Cannot create directory "".$this->root_dir."/".$this->cwd."/".$name.""");
return false;
}
return true;
}
//change access right to object
function set_perm($obj,$num){
//CHMOD 444 ftp.php3
if(!$this->site("CHMOD ".$num." ".$obj)){
$this->error("Cannot change permitions of object "".$this->root_dir."/".$this->cwd."/".$obj.""");
return false;
}
return true;
}
//send SITE command
function site($cmd){
if(!ftp_site($this->con_id, $cmd)){
$this->error("Cannot send site command "".$cmd.""");
return false;
}
return true;
}
//copy file
function copy($from,$to){
if(file_exists($this->root_dir."/".$to)){
$this->error("Object "".$this->root_dir."/".$to."" already exists!");
return false;
}
srand((double)microtime()*1000000);
$tmpfile=dirname(tempnam('',''))."/phpftptmp.".rand();
if(!copy($this->root_dir."/".$from,$tmpfile)){
$this->error("Can't create temp file?");
return false;
}elseif(!ftp_put($this->con_id,$to,$tmpfile,$this->FTP_MODE)){
$this->error("File "".$this->root_dir."/".$from."" can not copied to "".$this->root_dir."/".$to.""!");
return false;
}
return true;
}
//move object
function move($from,$to){
if(file_exists($this->root_dir."/".$to)){
$this->error("Object "".$this->root_dir."/".$to."" already exists!");
return false;
}
if(!ftp_rename($this->con_id,$from,$to)){
$this->error("Cannot move object "".$this->root_dir."/".$from."" to "".$this->root_dir."/".$to.""");
return false;
}
return true;
}
//rename object
function rename($from,$to){
if(file_exists($this->root_dir."/".$this->cwd."/".$to)){
$this->error("Object "".$this->root_dir."/".$this->cwd."/".$to."" already exists!");
return false;
}elseif(!ftp_rename($this->con_id,$from,$to)){
$this->error("Cannot rename object "".$this->root_dir."/".$this->cwd."/".$to.""");
return false;
}
return true;
}
//kill file or empty directory
function del($obj){
if(is_dir($this->root_dir."/".$this->cwd."/".$obj)){
if(!ftp_rmdir($this->con_id, $obj)){
$this->error("Cannot delete directory "".$this->root_dir."/".$this->cwd."/".$obj.""");
return false;
}
}elseif(is_file($this->root_dir."/".$this->cwd."/".$obj)){
if(!ftp_delete($this->con_id, $obj)){
$this->error("Cannot delete file "".$this->root_dir."/".$this->cwd."/".$obj.""");
return false;
}
}else{
$this->error("Removing object "".$this->root_dir."/".$this->cwd."/".$obj."" canceled!");
return false;
}
return true;
}
//write into file
function write($dest,$FILEDATA){
if(!WIN){
$old_perm=$this->get_perm($dest,'i');
$this->set_perm($dest,"666");
}
$fd=fopen($this->root_dir."/".$this->cwd."/".$dest,"w");
if(!fwrite($fd,$FILEDATA)){
$this->error("Cannot write file "".$this->root_dir."/".$this->cwd."/".$dest.""");
fclose($fd);
if(!WIN)$this->set_perm($dest,"644");
return false;
}
fclose($fd);
if(!WIN)$this->set_perm($dest,"644");
return true;
}
//move uploaded file from TMP into CWD
function move_uploaded_file($file_to_move,$file_name){
srand((double)microtime()*1000000);
if(!$tmp_dir=get_cfg_var('upload_tmp_dir'))$tmp_dir=dirname(tempnam('',''));
$tmpfile=$tmp_dir."/phpftptmp.".rand();
if(!copy($file_to_move,$tmpfile)){
$this->error("Can't create temp file?");
unlink($file_to_move);
return false;
}elseif(!ftp_put($this->con_id,$this->cwd."/".$file_name,$tmpfile,$this->FTP_MODE)){
$this->error("Can't write file "".$this->root_dir."/".$this->cwd."/".$file_name.""");
unlink($file_to_move);
unlink($tmpfile);
return false;
}
unlink($file_to_move);
unlink($tmpfile);
return true;
}
//return access right of an object, at various formats
function get_perm($obj,$type='i'){
$num=fileperms($obj);
$s=array(07=>'rwx',06=>'rw-',05=>'r-x',04=>'r--',03=>'-wx',02=>'-w-',01=>'--x',00=>'---');
$i=array(07=>'7',06=>'6',05=>'5',04=>'4',03=>'3',02=>'2',01=>'1',00=>'0');
$b=array(
07=>array(1,1,1),
06=>array(1,1,0),
05=>array(1,0,1),
04=>array(1,0,0),
03=>array(0,1,1),
02=>array(0,1,0),
01=>array(0,0,1),
00=>array(0,0,0)
);
switch($type){
case 'b':
$ret['o']=$b[($num & 0700)>>6];
$ret['g']=$b[($num & 070)>>3];
$ret['a']=$b[($num & 07) ];
break;
case 's':
if($num & 0x1000) $ret ='p';//FIFO pipe
elseif($num & 0x2000) $ret.='c';//Character special
elseif($num & 0x4000) $ret.='d';//Directory
elseif($num & 0x6000) $ret.='b';//Block special
elseif($num & 0x8000) $ret.='-';//Regular
elseif($num & 0xA000) $ret.='l';//Symbolic Link
elseif($num & 0xC000) $ret.='s';//Socket
else $str.='?'; //UNKNOWN
$ret.=$s[($num & 0700)>>6];
$ret.=$s[($num & 070)>>3];
$ret.=$s[($num & 07) ];
break;
case 'i':
$ret =$i[($num & 0700)>>6];
$ret.=$i[($num & 070)>>3];
$ret.=$i[($num & 07) ];
break;
}
return $ret;
}
//print dir file list
function dir_list(){
//ftp_nlist — Returns a list of files in the given directory.
//ftp_rawlist — Returns a detailed list of files in the given directory.
?>Directories | Files |
$contents=ftp_nlist($this->con_id, $this->cwd);
$d_i=0;
$f_i=0;
sort($contents);
for($i=0;$icon_id,$contents[$i]);
if(is_dir($this->root_dir.$contents[$i])){
$nlist_dirs[$d_i]=$contents[$i];
$d_i++;
}else{
$nlist_files[$f_i]=$contents[$i];
$nlist_filesize[$f_i]=$file_size;
$f_i++;
}
}
?>
for($i=0;$i";
?> |
for($i=0;$i";
?> |
}
}//end class
$FTP_HOST="localhost";
$FTP_USER="";
$FTP_PW="";
$FTP_ROOT_DIR="";
$obj->ftp=new php_ftp_class($FTP_USER,$FTP_PW,$FTP_HOST,$FTP_ROOT_DIR);
$obj->dir_list();
?>