ahmad_m1234
Active Member
کلاس کامل کار با ftp که با php نوشتم آماده کردم . کار باهاش راحت می باشد و قابلیت های زیر را دارد
لیست فایل ها و پوشه ها ، ساخت پوشه ، آپلود و دانلود فایل ، تغییر نام فایل ، کپی و انتقال فایل ، حذف فایل و پوشه
مثال :
امیدوارم این کلاس در پروژه های شما مورد استفاده قرار بگیرد
با تشکر از دوستان مجید آنلاینی که کمک بزرگی به من کرده اند
لیست فایل ها و پوشه ها ، ساخت پوشه ، آپلود و دانلود فایل ، تغییر نام فایل ، کپی و انتقال فایل ، حذف فایل و پوشه
PHP:
<?php
/*
* Author : ahmad nemati
* Email : [email protected]
* Mob : 09171687267
* blog : http://anemati.ir
*
* ftp client class
* get more information : http://www.php.net/manual/en/ref.ftp.php
*
*
*/
class ftp
{
protected $_connectid = FALSE;
var $hostname = ''; // Host
var $username = ''; // Username
var $password = ''; // Password
var $port = 21; // Port
var $passive = TRUE;
protected $error = '';
/*
*
* @Connect to Ftp Server
*
* @return boolean
*/
function connect()
{
if($this->hostname != '' && $this->port != '')
{
if(FALSE ===($this->_connectid = @ftp_connect($this->hostname,$this->port)))
{
return false;
}
}else{
return false;
}
$this->_login();
if($this->passive == TRUE)
{
ftp_pasv($this->_connectid,TRUE);
}
}
/*
*
* @Login to Ftp Server
*
* @return boolean
*/
function _login()
{
if($this->_isconnect())
{
if($this->username != '' && $this->password != '')
{
if(FALSE === (@ftp_login($this->_connectid,$this->username,$this->password)))
{
return false;
}else{
return true;
}
}
}else{
return false;
}
}
/*
*
* @Check Is Connected to Ftp Server
*
* @return boolean
*/
function _isconnect()
{
if(is_resource($this->_connectid))
{
return true;
}else{
return false;
}
}
/*
*
* @Get Current dir
*
* @return String
*/
function currentdir()
{
return @ftp_pwd($this->_connectid);
}
/*
*
* @Set Dir
*
* @Param String path
*
* @return boolean
*/
function setdir($path = '')
{
if($path == '' || !$this->_isconnect())
{
return false;
}
if(FALSE === (@ftp_chdir($this->_connectid,$path)))
{
return false;
}
}
/*
*
* @Create Dir
*
* @Param String Path
*
* @Param int Permission
*
* @return boolean
*/
function makedir($path = '',$permission = NULL)
{
if($path == '' || !$this->_isconnect())
{
return false;
}
if(FALSE === (@ftp_mkdir($this->_connectid,$path)))
{
return false;
}
if(!is_null($permission))
{
if(FALSE === (@ftp_chmod($this->_connectid, (int)$permission, $path)))
{
return false;
}
}
}
/*
*
* @Upload File
*
* @param string local path
*
* @param string remote path
*
* @param string transfer mode : FTP_ASCII or FTP_BINARY or auto
*
* @param int permission
*
* @return boolean
*/
function upload($localpath = '',$rempath = '' , $mode = 'auto' , $permission = NULL)
{
if(!$this->_connectid)
{
return false;
}
if($mode == 'auto')
{
$ext = $this->_getext($localpath);
$mode = $this->_setext($ext);
}
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
if(FALSE === (@ftp_put($this->_connectid,$rempath,$localpath,$mode)))
{
return false;
}
if(!is_null($permission))
{
if(FALSE === (@ftp_chmod($this->_connectid, (int)$permission, $rempath)))
{
return false;
}
}
}
/*
*
* @Download File
*
* @param string local path
*
* @param string remote path
*
* @param string transfer mode : FTP_ASCII or FTP_BINARY or auto
*
* @return boolean
*/
function download($localpath = '',$rempath = '' , $mode = 'auto')
{
if(!$this->_connectid)
{
return false;
}
if($mode == 'auto')
{
$ext = $this->_getext($localpath);
$mode = $this->_setext($ext);
}
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
if(FALSE === (@ftp_get($this->_connectid,$localpath,$rempath,$mode)))
{
return false;
}
}
/*
*
* @Rename File
*
* @param string old file name
*
* @param string new file name
*
* @return boolean
*/
function rename($oldfile,$newfile)
{
if(!$this->_connectid)
{
return false;
}
if(FALSE === (@ftp_rename($this->_connectid,$oldfile,$newfile)))
{
return false;
}
}
/*
*
* @move File
*
* @param string old address file
*
* @param string new address file
*
* @return boolean
*/
function move($oldfile,$newfile)
{
$this->rename($oldfile,$newfile);
}
/*
*
* @delete File
*
* @param string address file
*
* @return boolean
*/
function deletefile($filepath)
{
if(!$this->_isconnect())
{
return false;
}
if(FALSE === (@ftp_delete($this->_connectid,$filepath)))
{
return false;
}
}
/*
*
* @delete dir
*
* @param string address dir
*
* @return boolean
*/
function deletedir($dirpath)
{
if(!$this->_isconnect())
{
return false;
}
$list = $this->listfile($dirpath);
if($list !== FALSE && count($list) > 0)
{
foreach($list as $item)
{
if(! @ftp_delete($this->_connectid,$item))
{
$this->deletedir($item);
}
}
}
if(FALSE === (@ftp_rmdir($this->_connectid,$dirpath)))
{
return false;
}
}
/*
*
* @list File
*
* @param string path
*
* @return boolean
*/
function listfile($path)
{
if(!$this->_isconnect())
{
return false;
}
return ftp_nlist($this->_connectid,$path);
}
/*
*
* @close connect ftp server
*/
function close()
{
if(!$this->_isconnect())
{
return false;
}
@ftp_close($this->_connectid);
}
/*
*
* @get extension file
*
* @param string address file
*
* @return string
*/
function _getext($filename)
{
$e = explode('.',$filename);
return end($e);
}
/*
*
* @set transfer FTP_ASCII or FTP_BINARY
*
* @param string extension file
*
* @return string
*/
function _setext($ext)
{
$txt_array = array('txt','text','php','phps','php4','js','css','htm','html','phtml','shtml','log','xml');
return (in_array($ext,$txt_array)) ? FTP_ASCII : FTP_BINARY;
}
}
?>
مثال :
PHP:
<?php
require_once('ftp.lib.php');
$ftp = new ftp;
$ftp->hostname = 'ftp.example.com';
$ftp->username = 'username';
$ftp->password = 'password';
$ftp->port = 21;
$ftp->connect();
$file = $ftp->listfile('/wwwroot');
print_r($file);
?>
امیدوارم این کلاس در پروژه های شما مورد استفاده قرار بگیرد
با تشکر از دوستان مجید آنلاینی که کمک بزرگی به من کرده اند
آخرین ویرایش: